Fixed db things, register checks

This commit is contained in:
Norbi Peti 2016-07-21 12:11:28 +02:00
parent f9cba67116
commit ab4fcf9fd2
5 changed files with 53 additions and 13 deletions

View file

@ -1,6 +1,12 @@
<!DOCTYPE html>
<html>
<h1>Register</h1>
<div class="errormsg">
<errormsg />
</div>
<div class="successmsg">
<successmsg />
</div>
<table>
<form action="register" method="POST">
<tr>

View file

@ -44,17 +44,23 @@ public class IOHelper {
}
public static HashMap<String, String> GetPOST(HttpExchange exchange) throws IOException {
System.out.println(exchange.getRequestBody().available());
if (exchange.getRequestBody().available() == 0)
return new HashMap<>();
String[] content = IOUtils.toString(exchange.getRequestBody(), StandardCharsets.ISO_8859_1).split("\\&");
System.out.println(content);
HashMap<String, String> vars = new HashMap<>();
for (String var : content) {
String[] spl = var.split("\\=");
vars.put(spl[0], spl[1]);
try {
String[] content = IOUtils.toString(exchange.getRequestBody(), StandardCharsets.ISO_8859_1).split("\\&");
HashMap<String, String> vars = new HashMap<>();
for (String var : content) {
String[] spl = var.split("\\=");
if (spl.length == 1)
vars.put(spl[0], "");
else
vars.put(spl[0], spl[1]);
}
return vars;
} catch (Exception e) {
e.printStackTrace();
return new HashMap<>();
}
return vars;
}
public static boolean SendModifiedPage(int code, Page page, String replace, String with, HttpExchange exchange)

View file

@ -2,11 +2,19 @@ package io.github.norbipeti.chat.server.db.domain;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.*;
@Entity
public class Conversation {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Long id;
@ElementCollection
@OneToMany
private List<Message> messsages;
@ElementCollection
@OneToMany
private List<User> users;
public List<Message> getMesssages() {
@ -24,4 +32,12 @@ public class Conversation {
public void setUsers(List<User> users) {
this.users = users;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}

View file

@ -2,13 +2,19 @@ package io.github.norbipeti.chat.server.db.domain;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.*;
@Entity
public class Message {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Long id;
@ManyToOne
private User sender;
private Date time;
private String message;
@ManyToOne
private Conversation conversation;
public User getSender() {
@ -42,4 +48,12 @@ public class Message {
public void setConversation(Conversation conversation) {
this.conversation = conversation;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}

View file

@ -10,15 +10,13 @@ public class RegisterPage extends Page {
@Override
public void handlePage(HttpExchange exchange) throws IOException {
HashMap<String, String> post = IOHelper.GetPOST(exchange);
System.out.println("POST: " + post);
if (post.size() > 0) {
String errormsg = CheckValues(post, "name", "email", "pass", "pass2");
System.out.println(errormsg);
if (errormsg.length() == 0) {
// Process register
String successmsg = "";
IOHelper.SendModifiedPage(200, this, "<successmsg />", successmsg, exchange);
return;
return; // TODO: Only show tag when needed
} else
IOHelper.SendModifiedPage(200, this, "<errormsg />", errormsg, exchange);
return;