From 74dfecb5aa65e5ee9e0264e9a79c135e8e82fd8f Mon Sep 17 00:00:00 2001 From: bouclyma Date: Wed, 11 Dec 2024 11:19:45 +0100 Subject: [PATCH] =?UTF-8?q?feat(post):=20cr=C3=A9ation=20de=20la=20classe?= =?UTF-8?q?=20post?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/rtgre/chat/ChatController.java | 6 +- chat/src/main/java/rtgre/modeles/Post.java | 77 +++++++++++++++++++ 2 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 chat/src/main/java/rtgre/modeles/Post.java diff --git a/chat/src/main/java/rtgre/chat/ChatController.java b/chat/src/main/java/rtgre/chat/ChatController.java index 006995a..36feb29 100644 --- a/chat/src/main/java/rtgre/chat/ChatController.java +++ b/chat/src/main/java/rtgre/chat/ChatController.java @@ -194,10 +194,12 @@ public class ChatController implements Initializable { } public String getSelectedContactLogin() { + Contact contact; String login; try { - login = contactsListView.getSelectionModel().getSelectedItem().toString(); - } catch (java.lang.NullPointerException e) { + contact = (Contact) contactsListView.getSelectionModel().getSelectedItem(); + login = contact.getLogin(); + } catch (Exception e) { login = null; } LOGGER.info("Selected login: " + login); diff --git a/chat/src/main/java/rtgre/modeles/Post.java b/chat/src/main/java/rtgre/modeles/Post.java new file mode 100644 index 0000000..80c989a --- /dev/null +++ b/chat/src/main/java/rtgre/modeles/Post.java @@ -0,0 +1,77 @@ +package rtgre.modeles; + +import org.json.JSONObject; + +import java.util.Date; +import java.util.UUID; +public class Post extends Message { + + protected UUID id; + protected long timestamp; + protected String from; + + + public Post(UUID id, long timestamp, String from, String to, String body) { + /** + * Crée un objet Post + * @param: UUID id + * @param: long timestamp + * @param: String from + * @param: String to, + * @param: String body + */ + super(to, body); + this.id = id; + this.timestamp = timestamp; + this.from = from; + } + + public Post(String from, String to, String body) { + super(to, body); + this.from = from; + this.id = UUID.randomUUID(); + this.timestamp = new Date().getTime(); + } + + public UUID getId() { + return id; + } + + public long getTimestamp() { + return timestamp; + } + + public String getFrom() { + return from; + } + + @Override + public String toString() { + return "Post{" + + "id=" + id + + ", timestamp=" + timestamp + + ", from=" + from + + ", to=" + to + + ", body=" + body + + '}'; + } + + @Override + public String toJson() { + JSONObject json = toJsonObject() + .put("id", this.id) + .put("timestamp", this.timestamp) + .put("from", this.from); + return json.toString(); + } + + public static Post fromJson(JSONObject jsonObject) { + return new Post( + (UUID) jsonObject.get("id"), + jsonObject.getLong("timestamp"), + jsonObject.getString("from"), + jsonObject.getString("to"), + jsonObject.getString("body") + ); + } +}