diff --git a/module-api/src/main/java/com/mile/common/interceptor/MoimAuthInterceptor.java b/module-api/src/main/java/com/mile/common/interceptor/MoimAuthInterceptor.java index 7fbe089b..fe2cafa6 100644 --- a/module-api/src/main/java/com/mile/common/interceptor/MoimAuthInterceptor.java +++ b/module-api/src/main/java/com/mile/common/interceptor/MoimAuthInterceptor.java @@ -62,7 +62,7 @@ private String getUserTokenFromHeader(final HttpServletRequest request) { } private boolean authenticateUserFromMap(final UserAuthAnnotation annotation, - final Map userRoles, + final HashMap userRoles, final Map pathVariables) { switch (annotation.value()) { case OWNER -> { @@ -70,7 +70,7 @@ private boolean authenticateUserFromMap(final UserAuthAnnotation annotation, if (!userRoles.containsKey(requestMoimId) || !userRoles.get(requestMoimId).moimRole().equals(MoimRole.OWNER)) { throw new ForbiddenException(ErrorMessage.MOIM_OWNER_AUTHENTICATION_ERROR); } - WriterNameContextUtil.setWriterNameContext(userRoles.get(requestMoimId).writerNameId()); + WriterNameContextUtil.setWriterNameIdContext(userRoles.get(requestMoimId).writerNameId()); return true; } case WRITER_NAME -> { @@ -78,10 +78,11 @@ private boolean authenticateUserFromMap(final UserAuthAnnotation annotation, if (!userRoles.containsKey(requestMoimId)) { throw new ForbiddenException(ErrorMessage.USER_MOIM_AUTHENTICATE_ERROR); } - WriterNameContextUtil.setWriterNameContext(userRoles.get(requestMoimId).writerNameId()); + WriterNameContextUtil.setWriterNameIdContext(userRoles.get(requestMoimId).writerNameId()); return true; } case USER -> { + WriterNameContextUtil.setMoimWriterNameMapContext(userRoles); return true; } } diff --git a/module-api/src/main/java/com/mile/common/utils/thread/WriterNameContextUtil.java b/module-api/src/main/java/com/mile/common/utils/thread/WriterNameContextUtil.java index 2827a723..2bc28250 100644 --- a/module-api/src/main/java/com/mile/common/utils/thread/WriterNameContextUtil.java +++ b/module-api/src/main/java/com/mile/common/utils/thread/WriterNameContextUtil.java @@ -1,15 +1,25 @@ package com.mile.common.utils.thread; +import com.mile.writername.service.vo.WriterNameInfo; + +import java.util.HashMap; + public class WriterNameContextUtil { private static final ThreadLocal writerNameContext = new ThreadLocal<>(); + private static final ThreadLocal> moimWriterNameMapContext = new ThreadLocal<>(); + + public static void setMoimWriterNameMapContext(HashMap moimInfoMap) { + moimWriterNameMapContext.set(moimInfoMap); + } - public static void setWriterNameContext(Long writerNameId) { + public static void setWriterNameIdContext(Long writerNameId) { writerNameContext.set(writerNameId); } public static Long getWriterNameContext() { return writerNameContext.get(); } + public static HashMap getMoimWriterNameMapContext() {return moimWriterNameMapContext.get();} public static void clear() { writerNameContext.remove(); diff --git a/module-api/src/main/java/com/mile/controller/moim/MoimController.java b/module-api/src/main/java/com/mile/controller/moim/MoimController.java index 5e5a439a..1d4a0ec7 100644 --- a/module-api/src/main/java/com/mile/controller/moim/MoimController.java +++ b/module-api/src/main/java/com/mile/controller/moim/MoimController.java @@ -6,6 +6,7 @@ import com.mile.common.auth.dto.AccessTokenDto; import com.mile.common.resolver.moim.MoimIdPathVariable; import com.mile.common.resolver.user.UserId; +import com.mile.common.utils.thread.WriterNameContextUtil; import com.mile.dto.SuccessResponse; import com.mile.exception.message.SuccessMessage; import com.mile.moim.service.MoimService; @@ -32,7 +33,7 @@ import com.mile.moim.service.dto.response.TopicListResponse; import com.mile.moim.service.dto.response.WriterNameConflictCheckResponse; import com.mile.writername.domain.MoimRole; -import com.mile.writername.service.dto.response.WriterNameShortResponse; +import com.mile.writername.service.dto.response.WriterNameInformationResponse; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.enums.ParameterIn; import io.swagger.v3.oas.annotations.media.Schema; @@ -284,13 +285,13 @@ public ResponseEntity> getWriterN } @Override + @UserAuthAnnotation(UserAuthenticationType.WRITER_NAME) @GetMapping("/{moimId}/writername") - public ResponseEntity> getWriterNameOfUser( + public ResponseEntity> getWriterNameOfUser( @MoimIdPathVariable final Long moimId, - @UserId final Long userId, @PathVariable("moimId") final String moimUrl ) { - return ResponseEntity.ok(SuccessResponse.of(SuccessMessage.WRITER_NAME_GET_SUCCESS, moimService.getWriterNameOfUser(moimId, userId))); + return ResponseEntity.ok(SuccessResponse.of(SuccessMessage.WRITER_NAME_GET_SUCCESS, moimService.getWriterNameOfUser(WriterNameContextUtil.getWriterNameContext()))); } @Override diff --git a/module-api/src/main/java/com/mile/controller/moim/MoimControllerSwagger.java b/module-api/src/main/java/com/mile/controller/moim/MoimControllerSwagger.java index ef12ccb9..e0aa0141 100644 --- a/module-api/src/main/java/com/mile/controller/moim/MoimControllerSwagger.java +++ b/module-api/src/main/java/com/mile/controller/moim/MoimControllerSwagger.java @@ -9,7 +9,6 @@ import com.mile.moim.service.dto.response.ContentListResponse; import com.mile.moim.service.dto.response.InvitationCodeGetResponse; import com.mile.moim.service.dto.request.MoimCreateRequest; -import com.mile.moim.service.dto.response.MoimCreateResponse; import com.mile.moim.service.dto.response.MoimCuriousPostListResponse; import com.mile.moim.service.dto.request.MoimInfoModifyRequest; import com.mile.moim.service.dto.response.MoimInfoOwnerResponse; @@ -25,7 +24,7 @@ import com.mile.moim.service.dto.response.TopicListResponse; import com.mile.moim.service.dto.request.WriterMemberJoinRequest; import com.mile.moim.service.dto.response.WriterNameConflictCheckResponse; -import com.mile.writername.service.dto.response.WriterNameShortResponse; +import com.mile.writername.service.dto.response.WriterNameInformationResponse; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.enums.ParameterIn; @@ -395,9 +394,8 @@ ResponseEntity> getWriterNameList content = @Content(schema = @Schema(implementation = ErrorResponse.class))) } ) - ResponseEntity> getWriterNameOfUser( + ResponseEntity> getWriterNameOfUser( @Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) final Long moimId, - @Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) final @UserId Long userId, @PathVariable("moimId") final String moimUrl ); diff --git a/module-api/src/main/java/com/mile/controller/post/PostController.java b/module-api/src/main/java/com/mile/controller/post/PostController.java index f397708d..f56bb1d1 100644 --- a/module-api/src/main/java/com/mile/controller/post/PostController.java +++ b/module-api/src/main/java/com/mile/controller/post/PostController.java @@ -1,6 +1,9 @@ package com.mile.controller.post; +import com.mile.common.auth.annotation.UserAuthAnnotation; +import com.mile.common.auth.annotation.UserAuthenticationType; import com.mile.common.resolver.user.UserId; +import com.mile.common.utils.thread.WriterNameContextUtil; import com.mile.curious.service.dto.CuriousInfoResponse; import com.mile.dto.SuccessResponse; import com.mile.exception.message.SuccessMessage; @@ -39,15 +42,15 @@ public class PostController implements PostControllerSwagger { @PostMapping("/{postId}/comment") @Override + @UserAuthAnnotation(UserAuthenticationType.USER) public SuccessResponse postComment( @PostIdPathVariable final Long postId, @Valid @RequestBody final CommentCreateRequest commentCreateRequest, - @UserId final Long userId, @PathVariable("postId") final String postUrl ) { postService.createCommentOnPost( postId, - userId, + WriterNameContextUtil.getMoimWriterNameMapContext(), commentCreateRequest ); return SuccessResponse.of(SuccessMessage.COMMENT_CREATE_SUCCESS); @@ -56,43 +59,51 @@ public SuccessResponse postComment( @PostMapping("/{postId}/curious") @Override + @UserAuthAnnotation(UserAuthenticationType.USER) public SuccessResponse postCurious( @PostIdPathVariable final Long postId, - @UserId final Long userId, @PathVariable("postId") final String postUrl ) { - return SuccessResponse.of(SuccessMessage.CURIOUS_CREATE_SUCCESS, postService.createCuriousOnPost(postId, userId)); + return SuccessResponse.of(SuccessMessage.CURIOUS_CREATE_SUCCESS, postService.createCuriousOnPost(postId, WriterNameContextUtil.getMoimWriterNameMapContext())); } @GetMapping("/{postId}/comment") @Override + @UserAuthAnnotation(UserAuthenticationType.USER) public ResponseEntity> getComments( @PostIdPathVariable final Long postId, - @UserId Long userId, @PathVariable("postId") final String postUrl ) { - return ResponseEntity.ok(SuccessResponse.of(SuccessMessage.COMMENT_SEARCH_SUCCESS, postService.getComments(postId, userId))); + return ResponseEntity.ok(SuccessResponse.of(SuccessMessage.COMMENT_SEARCH_SUCCESS, postService.getComments(postId, WriterNameContextUtil.getMoimWriterNameMapContext()))); } @GetMapping("/{postId}/info/curious") @Override + @UserAuthAnnotation(UserAuthenticationType.USER) public ResponseEntity> getCuriousInfo( @PostIdPathVariable final Long postId, - @UserId final Long userId, @PathVariable("postId") final String postUrl ) { - return ResponseEntity.status(HttpStatus.OK).body(SuccessResponse.of(SuccessMessage.CURIOUS_INFO_SEARCH_SUCCESS, postService.getCuriousInfoOfPost(postId, userId))); + return ResponseEntity.status(HttpStatus.OK).body(SuccessResponse.of(SuccessMessage.CURIOUS_INFO_SEARCH_SUCCESS, + postService.getCuriousInfoOfPost( + postId, + WriterNameContextUtil.getMoimWriterNameMapContext() + ))); } @DeleteMapping("/{postId}/curious") @Override + @UserAuthAnnotation(UserAuthenticationType.USER) public SuccessResponse deleteCurious( @PostIdPathVariable final Long postId, - @UserId final Long userId, @PathVariable("postId") final String postUrl ) { - return SuccessResponse.of(SuccessMessage.CURIOUS_DELETE_SUCCESS, postService.deleteCuriousOnPost(postId, userId)); + return SuccessResponse.of(SuccessMessage.CURIOUS_DELETE_SUCCESS, + postService.deleteCuriousOnPost( + postId, + WriterNameContextUtil.getMoimWriterNameMapContext() + )); } @GetMapping("/{postId}/authenticate") @@ -107,13 +118,13 @@ public ResponseEntity> getAuthenticate @PutMapping("/{postId}") @Override + @UserAuthAnnotation(UserAuthenticationType.USER) public ResponseEntity putPost( @PostIdPathVariable final Long postId, @Valid @RequestBody final PostPutRequest putRequest, - @UserId final Long userId, @PathVariable("postId") final String postUrl ) { - postService.updatePost(postId, userId, putRequest); + postService.updatePost(postId, WriterNameContextUtil.getMoimWriterNameMapContext(), putRequest); return ResponseEntity.status(HttpStatus.OK).body(SuccessResponse.of(SuccessMessage.POST_PUT_SUCCESS)); } @@ -130,13 +141,14 @@ public ResponseEntity deletePost( @Override @GetMapping("/temporary/{postId}") + @UserAuthAnnotation(UserAuthenticationType.USER) public SuccessResponse getTemporaryPost( @PostIdPathVariable final Long postId, - @UserId final Long userId, @PathVariable("postId") final String postUrl ) { return SuccessResponse.of(SuccessMessage.TEMPORARY_POST_GET_SUCCESS, - postService.getTemporaryPost(postId, userId)); + postService.getTemporaryPost(postId, + WriterNameContextUtil.getMoimWriterNameMapContext())); } @Override @@ -176,14 +188,14 @@ public SuccessResponse createTemporaryPost( @Override @DeleteMapping("/temporary/{postId}") + @UserAuthAnnotation(UserAuthenticationType.USER) public ResponseEntity deleteTemporaryPost( @PostIdPathVariable final Long postId, - @UserId final Long userId, @PathVariable("postId") final String postUrl ) { postService.deleteTemporaryPost( - userId, - postId + postId, + WriterNameContextUtil.getMoimWriterNameMapContext() ); return ResponseEntity.ok(SuccessResponse.of(SuccessMessage.TEMPORARY_POST_DELETE_SUCCESS)); } @@ -205,13 +217,13 @@ public ResponseEntity> putTemporaryToFixedPo @Override @GetMapping("/modify/{postId}") + @UserAuthAnnotation(UserAuthenticationType.USER) public SuccessResponse getPostForModifying( @PostIdPathVariable final Long postId, - @UserId final Long userId, @PathVariable("postId") final String postUrl ) { return SuccessResponse.of(SuccessMessage.MODIFY_POST_GET_SUCCESS, - postService.getPostForModifying(postId, userId)); + postService.getPostForModifying(postId, WriterNameContextUtil.getMoimWriterNameMapContext())); } } diff --git a/module-api/src/main/java/com/mile/controller/post/PostControllerSwagger.java b/module-api/src/main/java/com/mile/controller/post/PostControllerSwagger.java index 021cac42..bd2c9f80 100644 --- a/module-api/src/main/java/com/mile/controller/post/PostControllerSwagger.java +++ b/module-api/src/main/java/com/mile/controller/post/PostControllerSwagger.java @@ -48,7 +48,6 @@ public interface PostControllerSwagger { SuccessResponse postComment( @Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) final Long postId, @Valid @RequestBody final CommentCreateRequest commentCreateRequest, - @Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) @UserId final Long userId, @PathVariable("postId") final String postUrl ); @@ -65,7 +64,6 @@ SuccessResponse postComment( ) SuccessResponse postCurious( @Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) final Long postId, - @Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) @UserId final Long userId, @PathVariable("postId") final String postUrl ); @@ -83,7 +81,6 @@ SuccessResponse postCurious( ) ResponseEntity> getComments( @Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) final Long postId, - @Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) @UserId final Long userId, @PathVariable("postId") final String postUrl ); @@ -100,7 +97,6 @@ ResponseEntity> getComments( ) ResponseEntity> getCuriousInfo( @Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) final Long postId, - @Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) @UserId final Long userId, @PathVariable("postId") final String postUrl ); @@ -117,7 +113,6 @@ ResponseEntity> getCuriousInfo( ) SuccessResponse deleteCurious( @Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) final Long postId, - @Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) @UserId final Long userId, @PathVariable("postId") final String postUrl ); @@ -167,7 +162,6 @@ ResponseEntity> getAuthenticateWrite( ResponseEntity putPost( @Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) final Long postId, @RequestBody final PostPutRequest putRequest, - @Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) @UserId final Long userId, @PathVariable("postId") final String postUrl ); @@ -185,7 +179,6 @@ ResponseEntity putPost( ) ResponseEntity deleteTemporaryPost( @Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) final Long postId, - @Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) @UserId final Long userId, @PathVariable("postId") final String postUrl ); @@ -219,7 +212,6 @@ ResponseEntity deletePost( ) SuccessResponse getTemporaryPost( @Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) final Long postId, - @Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) @UserId final Long userId, @PathVariable("postId") final String postUrl ); @@ -312,7 +304,6 @@ ResponseEntity> putTemporaryToFixedPost( ) SuccessResponse getPostForModifying( @Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) final Long postId, - @Parameter(schema = @Schema(implementation = String.class), in = ParameterIn.PATH) @UserId final Long userId, @PathVariable("postId") final String postUrl ); } diff --git a/module-domain/src/main/java/com/mile/comment/service/CommentService.java b/module-domain/src/main/java/com/mile/comment/service/CommentService.java index ea957cb1..f6fbc06d 100644 --- a/module-domain/src/main/java/com/mile/comment/service/CommentService.java +++ b/module-domain/src/main/java/com/mile/comment/service/CommentService.java @@ -54,13 +54,10 @@ public void deleteReply( } public List getCommentResponse( - final Long moimId, final Post post, - final Long userId + final Long writerNameId ) { - postRetriever.authenticateUserWithPost(post, userId); List commentList = commentRetriever.findByPostId(post.getId()); - Long writerNameId = writerNameRetriever.getWriterNameIdByMoimIdAndUserId(moimId, userId); return commentList.stream() .map(comment -> CommentResponse.of( diff --git a/module-domain/src/main/java/com/mile/moim/service/MoimService.java b/module-domain/src/main/java/com/mile/moim/service/MoimService.java index d5f0e7cb..e82c9b68 100644 --- a/module-domain/src/main/java/com/mile/moim/service/MoimService.java +++ b/module-domain/src/main/java/com/mile/moim/service/MoimService.java @@ -47,7 +47,7 @@ import com.mile.writername.service.WriterNameRemover; import com.mile.writername.service.WriterNameRetriever; import com.mile.writername.service.WriterNameService; -import com.mile.writername.service.dto.response.WriterNameShortResponse; +import com.mile.writername.service.dto.response.WriterNameInformationResponse; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -84,11 +84,10 @@ public ContentListResponse getContentsFromMoim( return ContentListResponse.of(topicRetriever.getContentsFromMoim(moimId)); } - public WriterNameShortResponse getWriterNameOfUser( - final Long moimId, - final Long userId + public WriterNameInformationResponse getWriterNameOfUser( + final Long writerNameId ) { - return writerNameRetriever.findWriterNameInfo(moimId, userId); + return writerNameRetriever.findWriterNameInfo(writerNameId); } public WriterNameConflictCheckResponse checkConflictOfWriterName(Long moimId, String writerName) { diff --git a/module-domain/src/main/java/com/mile/post/service/PostRetriever.java b/module-domain/src/main/java/com/mile/post/service/PostRetriever.java index a480a0f1..41e08df0 100644 --- a/module-domain/src/main/java/com/mile/post/service/PostRetriever.java +++ b/module-domain/src/main/java/com/mile/post/service/PostRetriever.java @@ -79,23 +79,6 @@ private List getPostHaveCuriousCount( return postList; } - public void authenticateUserWithPost( - final Post post, - final Long userId - ) { - Long moimId = post.getTopic().getMoim().getId(); - writerNameRetriever.findByMoimAndUserWithNotExceptionCase(moimId, userId); - } - - public void authenticateUserOfMoim( - final boolean isUserInMoim - ) { - if (!isUserInMoim) { - throw new ForbiddenException(ErrorMessage.USER_MOIM_AUTHENTICATE_ERROR); - } - } - - public boolean existsPostByWriterWithPost( final Long postId, final Long writerNameId @@ -112,12 +95,6 @@ public void authenticateWriterWithPost( } } - public void authenticateWriter( - final Long postId, - final WriterName writerName - ) { - authenticateWriterWithPost(postId, writerName.getId()); - } public boolean isWriterOfPost(final Post post, final WriterName writerName) { return post.getWriterName().equals(writerName); diff --git a/module-domain/src/main/java/com/mile/post/service/PostService.java b/module-domain/src/main/java/com/mile/post/service/PostService.java index 1d383427..85a66f2c 100644 --- a/module-domain/src/main/java/com/mile/post/service/PostService.java +++ b/module-domain/src/main/java/com/mile/post/service/PostService.java @@ -27,14 +27,17 @@ import com.mile.topic.service.TopicService; import com.mile.topic.service.dto.response.ContentWithIsSelectedResponse; import com.mile.common.utils.SecureUrlUtil; +import com.mile.util.MoimWriterNameMapUtil; import com.mile.writername.domain.WriterName; import com.mile.writername.service.WriterNameRetriever; import com.mile.writername.service.dto.response.WriterNameResponse; +import com.mile.writername.service.vo.WriterNameInfo; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.Base64; +import java.util.HashMap; import java.util.List; @@ -67,64 +70,75 @@ public class PostService { @Transactional public void createCommentOnPost( final Long postId, - final Long userId, + final HashMap moimWriterInfoMap, final CommentCreateRequest commentCreateRequest ) { Post post = postRetriever.findById(postId); final Long moimId = post.getTopic().getMoim().getId(); - postRetriever.authenticateUserWithPost(post, userId); - commentCreator.createComment(post, writerNameRetriever.findByMoimAndUser(moimId, userId), commentCreateRequest); + final Long writerNameId = MoimWriterNameMapUtil.getWriterNameIdMoimWriterNameMap(moimId, moimWriterInfoMap); + commentCreator.createComment(post, writerNameRetriever.findByIdNonException(writerNameId), commentCreateRequest); } @Transactional public PostCuriousResponse createCuriousOnPost( final Long postId, - final Long userId + final HashMap moimWriterNameMap ) { Post post = postRetriever.findById(postId); Long moimId = post.getTopic().getMoim().getId(); - postRetriever.authenticateUserWithPost(post, userId); - curiousService.createCurious(post, writerNameRetriever.findByMoimAndUser(moimId, userId)); + final Long writerNameId = MoimWriterNameMapUtil.getWriterNameIdMoimWriterNameMap(moimId, moimWriterNameMap); + curiousService.createCurious(post, writerNameRetriever.findById(writerNameId)); return PostCuriousResponse.of(CURIOUS_TRUE); } public CommentListResponse getComments( final Long postId, - final Long userId + final HashMap moimWriterNameMap ) { Post post = postRetriever.findById(postId); - return CommentListResponse.of(commentService.getCommentResponse(post.getTopic().getMoim().getId(), post, userId)); + final Long writerNameId = MoimWriterNameMapUtil.getWriterNameIdMoimWriterNameMap( + post.getTopic().getMoim().getId(), + moimWriterNameMap + ); + return CommentListResponse.of(commentService.getCommentResponse(post, writerNameId)); } @Transactional(readOnly = true) public CuriousInfoResponse getCuriousInfoOfPost( final Long postId, - final Long userId + final HashMap moimWriterNameMap ) { Post post = postRetriever.findById(postId); - postRetriever.authenticateUserWithPost(post, userId); - return curiousService.getCuriousInfoOfPostAndWriterName(post, writerNameRetriever.findByMoimAndUser(post.getTopic().getMoim().getId(), userId)); + final Long writerNameId = MoimWriterNameMapUtil.getWriterNameIdMoimWriterNameMap( + post.getTopic().getMoim().getId(), + moimWriterNameMap + ); + return curiousService.getCuriousInfoOfPostAndWriterName(post, writerNameRetriever.findById(writerNameId)); } @Transactional public PostCuriousResponse deleteCuriousOnPost( final Long postId, - final Long userId + final HashMap moimWriterNameMap ) { Post post = postRetriever.findById(postId); - curiousService.deleteCurious(post, writerNameRetriever.findByMoimAndUserWithNotExceptionCase(post.getTopic().getMoim().getId(), userId)); + final Long writerNameId = MoimWriterNameMapUtil.getWriterNameIdMoimWriterNameMap( + post.getTopic().getMoim().getId(), + moimWriterNameMap); + curiousService.deleteCurious(post, writerNameRetriever.findByIdNonException(writerNameId)); return PostCuriousResponse.of(CURIOUS_FALSE); } public void updatePost( final Long postId, - final Long userId, + final HashMap moimWriteNameMap, final PostPutRequest putRequest ) { Post post = postRetriever.findById(postId); - postRetriever.authenticateWriter(postId, - writerNameRetriever.findWriterNameByMoimIdAndUserId(post.getTopic().getMoim().getId(), userId)); + final Long moimId = post.getTopic().getMoim().getId(); + final Long writerNameId = MoimWriterNameMapUtil.getWriterNameIdMoimWriterNameMap(moimId, moimWriteNameMap); + postRetriever.authenticateWriterWithPost(postId, writerNameId); Topic topic = topicRetriever.findById(decodeUrlToLong(putRequest.topicId())); postUpdator.update(post, topic, putRequest); } @@ -162,7 +176,7 @@ public void deletePost( Post post = postRetriever.findById(postId); Long moimId = post.getTopic().getMoim().getId(); WriterName writerName = writerNameRetriever.findByMoimAndUser(moimId, userId); - if(!postRetriever.isWriterOfPost(post, writerName) && !moimRetriever.isMoimOwnerEqualsUser(post.getTopic().getMoim(), userId)){ + if (!postRetriever.isWriterOfPost(post, writerName) && !moimRetriever.isMoimOwnerEqualsUser(post.getTopic().getMoim(), userId)) { throw new ForbiddenException(ErrorMessage.WRITER_AUTHENTICATE_ERROR); } postRemover.delete(post); @@ -171,12 +185,13 @@ public void deletePost( @Transactional(readOnly = true) public TemporaryPostGetResponse getTemporaryPost( final Long postId, - final Long userId + final HashMap moimWriterNameMap ) { Post post = postRetriever.findById(postId); Topic selectedTopic = post.getTopic(); Moim moim = selectedTopic.getMoim(); - postRetriever.authenticateWriter(postId, writerNameRetriever.findByMoimAndUser(moim.getId(), userId)); + final Long writerNameId = MoimWriterNameMapUtil.getWriterNameIdMoimWriterNameMap(moim.getId(), moimWriterNameMap); + postRetriever.authenticateWriterWithPost(postId, writerNameId); isPostTemporary(post); List contentResponse = topicService.getContentsWithIsSelectedFromMoim(moim.getId(), selectedTopic.getId()); return TemporaryPostGetResponse.of(post, contentResponse); @@ -211,9 +226,11 @@ private Long decodeUrlToLong( return Long.parseLong(new String(Base64.getUrlDecoder().decode(url))); } - public void deleteTemporaryPost(final Long userId, final Long postId) { - Long moimId = getMoimIdFromPostId(postId); - postRetriever.authenticateWriter(postId, writerNameRetriever.findByMoimAndUser(moimId, userId)); + public void deleteTemporaryPost(final Long postId, + final HashMap moimWriterNameMap) { + final Long moimId = getMoimIdFromPostId(postId); + final Long writerNameId = MoimWriterNameMapUtil.getWriterNameIdMoimWriterNameMap(moimId, moimWriterNameMap); + postRetriever.authenticateWriterWithPost(postId, writerNameId); Post post = postRetriever.findById(postId); postRemover.deleteTemporaryPost(post); } @@ -242,11 +259,14 @@ public void createTemporaryPost( postCreator.createTemporaryPost(writerName, topic, temporaryPostCreateRequest); } + /* + FIX ME 아래 메서드는 리턴 값에 writerName의 name이 포함되어야 해서 Interceptor 인가가 필요 없을 듯 함 + * */ @Transactional public WriterNameResponse putTemporaryToFixedPost(final Long userId, final PostPutRequest request, final Long postId) { Post post = postRetriever.findById(postId); WriterName writerName = writerNameRetriever.findByMoimAndUser(post.getTopic().getMoim().getId(), userId); - postRetriever.authenticateWriter(postId, writerName); + postRetriever.authenticateWriterWithPost(postId, writerName.getId()); isPostTemporary(post); postUpdator.updateTemporaryPost(post, post.getTopic(), request); return WriterNameResponse.of(post.getIdUrl(), writerName.getName()); @@ -254,10 +274,12 @@ public WriterNameResponse putTemporaryToFixedPost(final Long userId, final PostP public ModifyPostGetResponse getPostForModifying( final Long postId, - final Long userId + final HashMap moimWriterNameMap ) { Post post = postRetriever.findById(postId); - postRetriever.authenticateUserWithPost(post, userId); + final Long moimId = post.getTopic().getMoim().getId(); + final Long writerNameId = MoimWriterNameMapUtil.getWriterNameIdMoimWriterNameMap(moimId, moimWriterNameMap); + postRetriever.authenticateWriterWithPost(post.getId(), writerNameId); isPostNotTemporary(post); List contentResponse = topicService.getContentsWithIsSelectedFromMoim(post.getTopic().getMoim().getId(), post.getTopic().getId()); return ModifyPostGetResponse.of(post, contentResponse); diff --git a/module-domain/src/main/java/com/mile/util/MoimWriterNameMapUtil.java b/module-domain/src/main/java/com/mile/util/MoimWriterNameMapUtil.java new file mode 100644 index 00000000..b6afdac4 --- /dev/null +++ b/module-domain/src/main/java/com/mile/util/MoimWriterNameMapUtil.java @@ -0,0 +1,29 @@ +package com.mile.util; + +import com.mile.exception.message.ErrorMessage; +import com.mile.exception.model.ForbiddenException; +import com.mile.writername.service.vo.WriterNameInfo; + +import java.util.HashMap; + +public class MoimWriterNameMapUtil { + + public static Long getWriterNameIdMoimWriterNameMap( + final Long moimId, + final HashMap moimWriterInfoMap + ) { + if(!moimWriterInfoMap.containsKey(moimId)) { + throw new ForbiddenException(ErrorMessage.WRITER_NAME_NON_AUTHENTICATE); + } + return moimWriterInfoMap.get(moimId).writerNameId(); + } + + public static void authenticateWriterName( + final Long moimId, + final HashMap moimWriterInfoMap + ) { + if(!moimWriterInfoMap.containsKey(moimId)) { + throw new ForbiddenException(ErrorMessage.WRITER_NAME_NON_AUTHENTICATE); + } + } +} diff --git a/module-domain/src/main/java/com/mile/writername/service/WriterNameRetriever.java b/module-domain/src/main/java/com/mile/writername/service/WriterNameRetriever.java index e4e1ff51..6b326c1e 100644 --- a/module-domain/src/main/java/com/mile/writername/service/WriterNameRetriever.java +++ b/module-domain/src/main/java/com/mile/writername/service/WriterNameRetriever.java @@ -8,7 +8,7 @@ import com.mile.writername.domain.MoimRole; import com.mile.writername.domain.WriterName; import com.mile.writername.repository.WriterNameRepository; -import com.mile.writername.service.dto.response.WriterNameShortResponse; +import com.mile.writername.service.dto.response.WriterNameInformationResponse; import com.mile.writername.service.vo.WriterNameInfo; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; @@ -40,15 +40,14 @@ public List findByWriter(final User user) { public WriterName findById(final Long id) { return writerNameRepository.findById(id).orElseThrow( - () -> new NotFoundException(ErrorMessage.USER_NOT_FOUND) + () -> new ForbiddenException(ErrorMessage.USER_MOIM_AUTHENTICATE_ERROR) ); } - public WriterNameShortResponse findWriterNameInfo( - final Long moimId, - final Long userId + public WriterNameInformationResponse findWriterNameInfo( + final Long writerNameId ) { - return WriterNameShortResponse.of(findByMoimAndUserWithNotExceptionCase(moimId, userId)); + return WriterNameInformationResponse.of(findByIdNonException(writerNameId)); } public boolean isUserInMoim( @@ -72,11 +71,10 @@ public WriterName findByMoimAndUser( ); } - public WriterName findByMoimAndUserWithNotExceptionCase( - final Long moimId, - final Long writerId + public WriterName findByIdNonException( + final Long writerNameId ) { - return writerNameRepository.findByMoimIdAndWriterId(moimId, writerId) + return writerNameRepository.findById(writerNameId) .orElseThrow(() -> new ForbiddenException(ErrorMessage.WRITER_NAME_NON_AUTHENTICATE) ); } diff --git a/module-domain/src/main/java/com/mile/writername/service/dto/response/WriterNameInformationResponse.java b/module-domain/src/main/java/com/mile/writername/service/dto/response/WriterNameInformationResponse.java new file mode 100644 index 00000000..11f9dac8 --- /dev/null +++ b/module-domain/src/main/java/com/mile/writername/service/dto/response/WriterNameInformationResponse.java @@ -0,0 +1,13 @@ +package com.mile.writername.service.dto.response; + +import com.mile.writername.domain.WriterName; + +public record WriterNameInformationResponse( + String writerName, + Long writerNameId, + String description +) { + public static WriterNameInformationResponse of(final WriterName writerName) { + return new WriterNameInformationResponse(writerName.getName(), writerName.getId(), writerName.getInformation()); + } +} diff --git a/module-domain/src/main/java/com/mile/writername/service/dto/response/WriterNameShortResponse.java b/module-domain/src/main/java/com/mile/writername/service/dto/response/WriterNameShortResponse.java deleted file mode 100644 index f79cad12..00000000 --- a/module-domain/src/main/java/com/mile/writername/service/dto/response/WriterNameShortResponse.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.mile.writername.service.dto.response; - -import com.mile.writername.domain.WriterName; - -public record WriterNameShortResponse( - String writerName, - Long writerNameId -) { - public static WriterNameShortResponse of(final WriterName writerName) { - return new WriterNameShortResponse(writerName.getName(), writerName.getId()); - } -}