Db things and message sending
This commit is contained in:
parent
ea45675d56
commit
eb8eaa3834
5 changed files with 175 additions and 121 deletions
|
@ -1,4 +1,34 @@
|
|||
$('#msginput').keypress(function(e) {
|
||||
if (e.which == '\r'.charCodeAt(0))
|
||||
document.write(document.getElementById("msginput").value);
|
||||
var respfunc = function (result) {
|
||||
if (result.responseText != "Success") {
|
||||
var errormsg = document.getElementById("errormsg");
|
||||
errormsg.innerHTML = result.responseText;
|
||||
errormsg.style = "display: block";
|
||||
}
|
||||
else
|
||||
location.reload(true);
|
||||
}
|
||||
|
||||
var sendmsgonenter = function (e) { //TODO: Detect Enter
|
||||
console.log("A");
|
||||
var code = e.keyCode || e.which;
|
||||
if (code != 13) { //Enter keycode
|
||||
return;
|
||||
}
|
||||
console.log("B");
|
||||
var textarea = event.target;
|
||||
window.convid = 0;
|
||||
var json = JSON.stringify({"message": textarea.value, "conversation": window.convid});
|
||||
$.ajax({
|
||||
url: "/message", data: json, method: "POST", success: respfunc, error: respfunc
|
||||
});
|
||||
};
|
||||
|
||||
$(document).bind("ready", function () {
|
||||
console.log("X");
|
||||
$('#msginput').keypress = sendmsgonenter;
|
||||
});
|
||||
|
||||
console.log(respfunc);
|
||||
console.log(sendmsgonenter);
|
||||
|
||||
$('#msginput').bind("keypress", sendmsgonenter);
|
||||
|
|
|
@ -17,9 +17,6 @@ public class Conversation {
|
|||
@ElementCollection(fetch = FetchType.EAGER)
|
||||
@ManyToMany(cascade = CascadeType.ALL)
|
||||
private List<User> users;
|
||||
@Version
|
||||
@GeneratedValue
|
||||
private int Version;
|
||||
|
||||
public List<Message> getMesssages() {
|
||||
if (messsages == null)
|
||||
|
|
|
@ -11,14 +11,13 @@ public class Message {
|
|||
@Column(name = "id", unique = true, nullable = false)
|
||||
private Long id;
|
||||
@ManyToOne(cascade=CascadeType.ALL)
|
||||
//@JoinTable(name="user_message")
|
||||
private User sender;
|
||||
private Date time;
|
||||
private String message;
|
||||
@ManyToOne(cascade=CascadeType.ALL)
|
||||
//@JoinTable(name="conversation_message")
|
||||
private Conversation conversation;
|
||||
@Version
|
||||
@GeneratedValue
|
||||
private int Version;
|
||||
|
||||
public User getSender() {
|
||||
return sender;
|
||||
|
|
|
@ -2,6 +2,8 @@ package io.github.norbipeti.chat.server.db.domain;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Generated;
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
|
@ -20,9 +22,6 @@ public class User {
|
|||
//@Column(columnDefinition = "CHAR(16) FOR BIT DATA")
|
||||
@Column(columnDefinition="VARCHAR(64)")
|
||||
private String sessionid;
|
||||
@Version
|
||||
@GeneratedValue
|
||||
private int Version;
|
||||
@ElementCollection(fetch = FetchType.EAGER)
|
||||
@ManyToMany(cascade = CascadeType.ALL)
|
||||
public List<Conversation> conversations;
|
||||
|
|
|
@ -1,9 +1,19 @@
|
|||
package io.github.norbipeti.chat.server.page;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
|
||||
import io.github.norbipeti.chat.server.IOHelper;
|
||||
import io.github.norbipeti.chat.server.db.domain.Conversation;
|
||||
import io.github.norbipeti.chat.server.db.domain.Message;
|
||||
import io.github.norbipeti.chat.server.db.domain.User;
|
||||
|
||||
public class MessageAjaxPage extends Page {
|
||||
|
||||
@Override
|
||||
|
@ -13,7 +23,26 @@ public class MessageAjaxPage extends Page {
|
|||
|
||||
@Override
|
||||
public void handlePage(HttpExchange exchange) throws IOException {
|
||||
|
||||
User user = IOHelper.GetLoggedInUser(exchange);
|
||||
if (user == null) {
|
||||
IOHelper.SendResponse(403, "<p>Please log in to send messages</p>", exchange);
|
||||
return; //TODO: Fix sending messages
|
||||
}
|
||||
JSONObject obj = IOHelper.GetPOSTJSON(exchange);
|
||||
if (obj == null) {
|
||||
IOHelper.SendResponse(400, "<h1>400 Bad request</h1><p>Not a JSON string!</p>", exchange);
|
||||
return;
|
||||
}
|
||||
String message = obj.getString("message");
|
||||
int conversation = obj.getInt("conversation");
|
||||
List<Conversation> convos = user.getConversations();
|
||||
Conversation convo = convos.get(conversation);
|
||||
Message msg = new Message();
|
||||
msg.setSender(user);
|
||||
msg.setMessage(message);
|
||||
msg.setTime(new Date());
|
||||
convo.getMesssages().add(msg);
|
||||
IOHelper.SendResponse(200, "Success", exchange);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue