From 06990393a47e72b56e62abdc47fb0db3137987b3 Mon Sep 17 00:00:00 2001 From: Jon Sundin Date: Tue, 30 Jan 2024 01:21:46 +0000 Subject: [PATCH] get message by id endpoint --- .../Controller/SocialMediaController.java | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/main/java/Controller/SocialMediaController.java b/src/main/java/Controller/SocialMediaController.java index 6ee30ed..865bdff 100644 --- a/src/main/java/Controller/SocialMediaController.java +++ b/src/main/java/Controller/SocialMediaController.java @@ -45,6 +45,7 @@ public Javalin startAPI() { app.post("/login", this::login); app.get("/messages", this::getAllMessages); + app.get("/messages/{message_id}", this::getMessageById); app.post("/messages", this::messages); return app; @@ -68,7 +69,7 @@ private void register(Context context) throws JsonProcessingException { } else { Account registeredAccount = accountService.addAccount(accountFromBody); if (registeredAccount != null) { - context.json(registeredAccount).status(200); + context.json(registeredAccount); } else { context.result("").status(400); } @@ -86,7 +87,7 @@ private void login(Context context) { if (accountLoggedIn == null) { context.status(401); } else { - context.json(accountLoggedIn).status(200); + context.json(accountLoggedIn); } } } @@ -100,7 +101,7 @@ private void messages(Context context) { if (messageService.messageIsValid(posted_by, message_text)) { Message message = messageService.insertMessage(posted_by, message_text, time_posted_epoch); if (message != null) { - context.json(message).status(200); + context.json(message); } else { } @@ -111,11 +112,21 @@ private void messages(Context context) { private void getAllMessages(Context context) { List messages = messageService.getAllMessages(); - context.json(messages).status(200); + context.json(messages); + } + + private void getMessageById(Context context) { + int message_id = Integer.parseInt(context.pathParam("message_id")); + Message message = messageService.getMessageById(message_id); + if (message == null) { + context.status(200); + } else { + context.json(message); + } } private void getAccountTable(Context context) { List accounts = accountService.getAllAccounts(); - context.json(accounts).status(200); + context.json(accounts); } } \ No newline at end of file