Skip to content

Commit

Permalink
added updateMessageById patch endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsundin committed Jan 31, 2024
1 parent 673c27b commit 372dffa
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/main/java/Controller/SocialMediaController.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public Javalin startAPI() {
app.get("/messages", this::getAllMessages);
app.get("/messages/{message_id}", this::getMessageById);
app.post("/messages", this::messages);
app.patch("/messages/{message_id}", this::updateMessageById);
app.delete("/messages/{message_id}", this::deleteMessageById);

return app;
Expand Down Expand Up @@ -99,12 +100,12 @@ private void messages(Context context) {
String message_text = messageFromBody.message_text;
long time_posted_epoch = messageFromBody.time_posted_epoch;

if (messageService.messageIsValid(posted_by, message_text)) {
if (accountService.findAccountByAccountId(posted_by) != null && messageService.messageIsValid(message_text)) {
Message message = messageService.insertMessage(posted_by, message_text, time_posted_epoch);
if (message != null) {
context.json(message);
} else {
context.status(400);
}
} else {
context.status(400);
Expand Down Expand Up @@ -137,6 +138,21 @@ private void deleteMessageById(Context context) {
}
}

private void updateMessageById(Context context) {
int message_id = Integer.parseInt(context.pathParam("message_id"));
Message messageFromBody = context.bodyAsClass(Message.class);
messageFromBody.setMessage_id(message_id);

Message message = messageService.getMessageById(message_id);
if (message == null || !messageService.messageIsValid(messageFromBody.message_text)) {
context.status(400);
}
else {
message = messageService.updateMessageById(message_id, messageFromBody.message_text);
context.json(message);
}
}

private void getAccountTable(Context context) {
List<Account> accounts = accountService.getAllAccounts();
context.json(accounts);
Expand Down

0 comments on commit 372dffa

Please sign in to comment.