Skip to content

Commit

Permalink
get message by id endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsundin committed Jan 30, 2024
1 parent 0c76bbb commit 0699039
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/main/java/Controller/SocialMediaController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -86,7 +87,7 @@ private void login(Context context) {
if (accountLoggedIn == null) {
context.status(401);
} else {
context.json(accountLoggedIn).status(200);
context.json(accountLoggedIn);
}
}
}
Expand All @@ -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 {

}
Expand All @@ -111,11 +112,21 @@ private void messages(Context context) {

private void getAllMessages(Context context) {
List<Message> 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<Account> accounts = accountService.getAllAccounts();
context.json(accounts).status(200);
context.json(accounts);
}
}

0 comments on commit 0699039

Please sign in to comment.