From 930e6ab6d604963ae6a48c58fc1bd3735071d9ee Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 20 Nov 2018 00:07:06 +0300 Subject: getMessage refactoring --- src/main/java/com/juick/server/api/Post.java | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'src/main/java/com/juick/server/api/Post.java') 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 = 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 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 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 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(); -- cgit v1.2.3