diff options
Diffstat (limited to 'src/main/java/com/juick/server/api/Post.java')
-rw-r--r-- | src/main/java/com/juick/server/api/Post.java | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/src/main/java/com/juick/server/api/Post.java b/src/main/java/com/juick/server/api/Post.java index 2c7346b3..d49ec332 100644 --- a/src/main/java/com/juick/server/api/Post.java +++ b/src/main/java/com/juick/server/api/Post.java @@ -41,6 +41,7 @@ import javax.validation.constraints.NotNull; import java.net.URI; import java.net.URL; import java.util.List; +import java.util.Optional; /** * Created by vt on 24/11/2016. @@ -114,11 +115,13 @@ public class Post { if (mid == 0) { throw new HttpBadRequestException(); } - com.juick.Message msg = messagesService.getMessage(mid); - if (msg == null) { + Optional<Message> message = messagesService.getMessage(mid); + if (!message.isPresent()) { throw new HttpNotFoundException(); } + Message msg = message.get(); + com.juick.Message reply = null; if (rid > 0) { reply = messagesService.getReply(mid, rid); @@ -158,10 +161,11 @@ public class Post { if (visitor.isAnonymous()) { throw new HttpForbiddenException(); } - com.juick.Message msg = messagesService.getMessage(mid); - if (msg == null) { + Optional<com.juick.Message> message = messagesService.getMessage(mid); + if (!message.isPresent()) { throw new HttpNotFoundException(); } + Message msg = message.get(); if (msg.getUser().getUid() == visitor.getUid()) { throw new HttpForbiddenException(); } @@ -177,10 +181,11 @@ public class Post { if (visitor.isAnonymous()) { throw new HttpForbiddenException(); } - com.juick.Message msg = messagesService.getMessage(mid); - if (msg == null) { + Optional<com.juick.Message> message = messagesService.getMessage(mid); + if (!message.isPresent()) { throw new HttpNotFoundException(); } + Message msg = message.get(); if (msg.getUser().getUid() == visitor.getUid()) { throw new HttpForbiddenException(); } @@ -205,10 +210,11 @@ public class Post { if (visitor.isAnonymous()) { throw new HttpForbiddenException(); } - com.juick.Message msg = messagesService.getMessage(mid); - if (msg == null) { + Optional<com.juick.Message> message = messagesService.getMessage(mid); + if (!message.isPresent()) { throw new HttpNotFoundException(); } + Message msg = message.get(); if (msg.getUser().getUid() == visitor.getUid()) { throw new HttpForbiddenException(); } @@ -228,7 +234,7 @@ public class Post { User author = rid == 0 ? messagesService.getMessageAuthor(mid) : messagesService.getReply(mid, rid).getUser(); if (visitor.equals(author)) { if (messagesService.updateMessage(mid, rid, body)) { - Message result = rid == 0 ? messagesService.getMessage(mid) : messagesService.getReply(mid, rid); + Message result = rid == 0 ? messagesService.getMessage(mid).orElseThrow(IllegalStateException::new) : messagesService.getReply(mid, rid); return CommandResult.build(result, "Message updated", StringUtils.EMPTY); } throw new HttpBadRequestException(); |