Skip to content

Commit

Permalink
[fix] 1차 리팩토링 모든 경고 제거 x
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryeolee committed Nov 17, 2023
1 parent b1b6abf commit d7b9562
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 117 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: ModernFarmer CI/CD

on:
push:
branches: ["feature_11/커뮤니티-API-수정"]
branches: ["feature_13/코드-리팩토링"]

pull_request:
branches: ["dev"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package modernfarmer.server.farmuscommunity.community.controller;


import lombok.Getter;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import modernfarmer.server.farmuscommunity.community.dto.request.DeleteCommentRequest;
import modernfarmer.server.farmuscommunity.community.dto.request.UpdateCommentRequest;
import modernfarmer.server.farmuscommunity.community.dto.request.WriteCommentRequest;
import modernfarmer.server.farmuscommunity.community.dto.response.BaseResponseDto;
import modernfarmer.server.farmuscommunity.community.dto.response.PostingCommentResponseDto;
import modernfarmer.server.farmuscommunity.community.service.CommentService;
import modernfarmer.server.farmuscommunity.community.service.PostingService;
import modernfarmer.server.farmuscommunity.community.util.JwtTokenProvider;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
Expand All @@ -22,51 +22,42 @@
@RestController
@RequestMapping("/api/community/comment")
public class CommentController {

private final JwtTokenProvider jwtTokenProvider;
private final CommentService commentService;

private final CommentService commentService;

@PostMapping("/write")
public BaseResponseDto writeComment(HttpServletRequest request, @Validated @RequestBody WriteCommentRequest writeCommentRequestst){
public BaseResponseDto<Void> writeComment(HttpServletRequest request, @Validated @RequestBody WriteCommentRequest writeCommentRequestst){

String userId = jwtTokenProvider.getUserId(request);

BaseResponseDto baseResponseDto = commentService.writeComment(Long.valueOf(userId), writeCommentRequestst);

return baseResponseDto;
return commentService.writeComment(Long.valueOf(userId), writeCommentRequestst);
}

@PatchMapping ("/update")
public BaseResponseDto updateComment(HttpServletRequest request, @Validated @RequestBody UpdateCommentRequest updateCommentRequest){
public BaseResponseDto<Void> updateComment(HttpServletRequest request, @Validated @RequestBody UpdateCommentRequest updateCommentRequest){

String userId = jwtTokenProvider.getUserId(request);

BaseResponseDto baseResponseDto = commentService.updateComment(Long.valueOf(userId), updateCommentRequest);

return baseResponseDto;
return commentService.updateComment(Long.valueOf(userId), updateCommentRequest);
}

@DeleteMapping ("/delete")
public BaseResponseDto deleteComment(HttpServletRequest request, @Validated @RequestBody DeleteCommentRequest deleteCommentRequest){
public BaseResponseDto<Void> deleteComment(HttpServletRequest request, @Validated @RequestBody DeleteCommentRequest deleteCommentRequest){

String userId = jwtTokenProvider.getUserId(request);

BaseResponseDto baseResponseDto = commentService.deleteComment(Long.valueOf(userId), deleteCommentRequest);

return baseResponseDto;
return commentService.deleteComment(Long.valueOf(userId), deleteCommentRequest);
}

@GetMapping("/posting-comments")
public BaseResponseDto postingComment(@RequestParam("posterId") Long posterId,
@RequestParam("userId") Long userId
public BaseResponseDto<PostingCommentResponseDto> postingComment(@RequestParam("posterId") Long posterId,
@RequestParam("userId") Long userId
){


return commentService.postingComment(posterId, userId);
}





}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import lombok.extern.slf4j.Slf4j;
import modernfarmer.server.farmuscommunity.community.dto.request.ReportPostingRequest;
import modernfarmer.server.farmuscommunity.community.dto.response.BaseResponseDto;
import modernfarmer.server.farmuscommunity.community.dto.response.WholePostingResponseDto;
import modernfarmer.server.farmuscommunity.community.service.PostingService;
import modernfarmer.server.farmuscommunity.community.util.JwtTokenProvider;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@Slf4j
Expand All @@ -26,7 +26,7 @@ public class PostingController {


@PostMapping("/write")
public BaseResponseDto writePosting(HttpServletRequest request,
public BaseResponseDto<Void> writePosting(HttpServletRequest request,
@RequestPart("file") List<MultipartFile> multipartFiles,
@RequestParam("title") String title,
@RequestParam("contents") String contents,
Expand All @@ -38,15 +38,15 @@ public BaseResponseDto writePosting(HttpServletRequest request,
}

@PostMapping("/report")
public BaseResponseDto reportPosting(HttpServletRequest request, @Validated @RequestBody ReportPostingRequest reportPostingRequest) throws Exception {
public BaseResponseDto<Void> reportPosting(HttpServletRequest request, @Validated @RequestBody ReportPostingRequest reportPostingRequest) throws Exception {

String userId = jwtTokenProvider.getUserId(request);

return postingService.reportPosting(Long.valueOf(userId), reportPostingRequest);
}

@PatchMapping("/write-update")
public BaseResponseDto updatePosting(HttpServletRequest request,
public BaseResponseDto<Void> updatePosting(HttpServletRequest request,
@RequestParam("removeFile") List<String> removeFiles,
@RequestPart(value = "updateFile", required = false) List<MultipartFile> updateFiles,
@RequestParam("title") String title,
Expand All @@ -61,23 +61,17 @@ public BaseResponseDto updatePosting(HttpServletRequest request,


@GetMapping("/whole-posting")
public BaseResponseDto getWholePosting() {
public BaseResponseDto<WholePostingResponseDto> getWholePosting() {

return postingService.getWholePosting();
}

@GetMapping("/my-posting")
public BaseResponseDto getMyPosting(HttpServletRequest request) {
public BaseResponseDto<WholePostingResponseDto> getMyPosting(HttpServletRequest request) {

String userId = jwtTokenProvider.getUserId(request);

return postingService.getMyPosting(Long.valueOf(userId));
}







}
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ public class BaseResponseDto<T> {
@JsonInclude(JsonInclude.Include.NON_NULL)
private T data;

public static <T> BaseResponseDto of(SuccessMessage successMessage, T data){
public static <T> BaseResponseDto<T> of(SuccessMessage successMessage, T data){

return BaseResponseDto.builder()
return BaseResponseDto.<T>builder()
.code(successMessage.getCode())
.message(successMessage.getMessage())
.data(data)
.build();
}

public static <T> BaseResponseDto of(ErrorMessage errorMessage){
public static <T> BaseResponseDto<T> of(ErrorMessage errorMessage){

return BaseResponseDto.builder()
return BaseResponseDto.<T>builder()
.code(errorMessage.getCode())
.message(errorMessage.getMessage())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Table(name = "comments")
public class Comment extends BaseEntity{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Table(name = "posting")
public class Posting extends BaseEntity{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Table(name = "posting_images")
public class PostingImage extends BaseEntity{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


@AllArgsConstructor
@NoArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Builder
@Entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import modernfarmer.server.farmuscommunity.community.entity.PostingImage;
import modernfarmer.server.farmuscommunity.community.repository.CommentRepository;
import modernfarmer.server.farmuscommunity.community.repository.PostingRepository;
import modernfarmer.server.farmuscommunity.community.util.TimeCalculator;
import modernfarmer.server.farmuscommunity.global.exception.fail.ErrorMessage;
import modernfarmer.server.farmuscommunity.global.exception.success.SuccessMessage;
import modernfarmer.server.farmuscommunity.user.UserServiceFeignClient;
Expand All @@ -30,11 +31,16 @@ public class CommentService {


private final CommentRepository commentRepository;

private final UserServiceFeignClient userServiceFeignClient;
private final PostingService postingService;

private final TimeCalculator timeCalculator;



private final PostingRepository postingRepository;

public BaseResponseDto writeComment(Long userId, WriteCommentRequest writeCommentRequest){
public BaseResponseDto<Void> writeComment(Long userId, WriteCommentRequest writeCommentRequest){

Posting posting = Posting.builder().id(writeCommentRequest.getPostingId()).build();

Expand All @@ -47,28 +53,34 @@ public BaseResponseDto writeComment(Long userId, WriteCommentRequest writeCommen

commentRepository.save(comment);

log.info("댓글 쓰기 완료");

return BaseResponseDto.of(SuccessMessage.SUCCESS, null);
}

public BaseResponseDto updateComment(Long userId, UpdateCommentRequest updateCommentRequest){
public BaseResponseDto<Void> updateComment(Long userId, UpdateCommentRequest updateCommentRequest){

Posting posting = Posting.builder().id(updateCommentRequest.getPostingId()).build();

commentRepository.updateComment(userId, posting,updateCommentRequest.getCommentId(),updateCommentRequest.getComment());

log.info("댓글 수정 완료");

return BaseResponseDto.of(SuccessMessage.SUCCESS, null);
}

public BaseResponseDto deleteComment(Long userId, DeleteCommentRequest deleteCommentRequest){
public BaseResponseDto<Void> deleteComment(Long userId, DeleteCommentRequest deleteCommentRequest){

Posting posting = Posting.builder().id(deleteCommentRequest.getPostingId()).build();

commentRepository.deleteComment(userId, posting,deleteCommentRequest.getCommentId());

log.info("댓글 삭제 완료");

return BaseResponseDto.of(SuccessMessage.SUCCESS, null);
}

public BaseResponseDto postingComment(Long postingId, Long userId){
public BaseResponseDto<PostingCommentResponseDto> postingComment(Long postingId, Long userId){


modernfarmer.server.farmuscommunity.user.dto.BaseResponseDto userData = userServiceFeignClient.allUser();
Expand Down Expand Up @@ -106,15 +118,15 @@ public BaseResponseDto postingComment(Long postingId, Long userId){
.title(posting.get().getTitle())
.contents(posting.get().getContents())
.postingImage(list)
.created_at(postingService.formatCreatedAt(posting.get().getCreatedAt()))
.created_at(timeCalculator.formatCreatedAt(posting.get().getCreatedAt()))
.build();


List<PostingCommentDto> postingCommentList = commentList.stream()
.map(comment -> {

// 시간 형식 업데이트 로직
String formattedDate = postingService.formatCreatedAt(comment.getCreatedAt());
String formattedDate = timeCalculator.formatCreatedAt(comment.getCreatedAt());

Integer getUserId = Math.toIntExact(comment.getUserId());
Map<String, Object> userDto = userDtoMap.get(getUserId);
Expand All @@ -135,6 +147,8 @@ public BaseResponseDto postingComment(Long postingId, Long userId){
})
.collect(Collectors.toList());

log.info("게시글에 대한 정보와 게시물에 대한 댓글 조회 완료");

return BaseResponseDto.of(SuccessMessage.SUCCESS, PostingCommentResponseDto.of(wholePostingDto, postingCommentList));
}

Expand Down
Loading

0 comments on commit d7b9562

Please sign in to comment.