From f446584122c68a3cfce81487807bbd026bcc6184 Mon Sep 17 00:00:00 2001 From: u-genuine Date: Sun, 21 Jan 2024 18:47:00 +0900 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=EB=82=AD=EB=A7=8C=EC=9A=B0?= =?UTF-8?q?=ED=8E=B8=ED=95=A8=20=EA=B3=A0=EB=AF=BC=20=ED=8E=B8=EC=A7=80=20?= =?UTF-8?q?=EB=AA=A9=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=20API=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../converter/NangmanPostBoxConverter.java | 18 ++++++++-- .../umcproject/entity/NangmanLetter.java | 22 ++++++------ .../repository/NangmanLetterRepository.java | 5 +++ ...ervice.java => NangmanPostBoxService.java} | 8 +++-- ...pl.java => NangmanPostBoxServiceImpl.java} | 9 ++++- .../controller/NangmanPostBoxController.java | 34 ++++++++++++++++--- .../web/dto/NangmanPostBoxResponseDTO.java | 12 +++++++ 7 files changed, 86 insertions(+), 22 deletions(-) rename src/main/java/aromanticcat/umcproject/service/{NangmanLetterService.java => NangmanPostBoxService.java} (77%) rename src/main/java/aromanticcat/umcproject/service/{NangmanLetterServiceImpl.java => NangmanPostBoxServiceImpl.java} (90%) diff --git a/src/main/java/aromanticcat/umcproject/converter/NangmanPostBoxConverter.java b/src/main/java/aromanticcat/umcproject/converter/NangmanPostBoxConverter.java index a70dfce..0300d80 100644 --- a/src/main/java/aromanticcat/umcproject/converter/NangmanPostBoxConverter.java +++ b/src/main/java/aromanticcat/umcproject/converter/NangmanPostBoxConverter.java @@ -10,17 +10,29 @@ public static NangmanPostBoxResponseDTO.SendLetterResultDTO toSendLetterResultDT return NangmanPostBoxResponseDTO.SendLetterResultDTO.builder() .nangmanLetterId(nangmanLetter.getId()) - .senderNickname(nangmanLetter.getSender_nickname()) + .senderNickname(nangmanLetter.getSenderNickname()) .createdAt(nangmanLetter.getCreatedAt()) .build(); } public static NangmanLetter toNangmanLetter(NangmanPostBoxRequestDTO.SendLetterDTO request){ return NangmanLetter.builder() - .is_public(request.getIsPublic()) + .isPublic(request.getIsPublic()) .content(request.getContent()) - .sender_nickname(request.getSenderRandomNickname()) + .senderNickname(request.getSenderRandomNickname()) // .member(request.getMember()) .build(); } + + public static NangmanPostBoxResponseDTO.LetterSummaryResultDTO toLetterSummaryResultDTO(NangmanLetter nangmanLetter){ + // 편지 내용을 40자 까지만 보이도록 + String content = nangmanLetter.getContent(); + String preview = content.length() <= 40 ? content: content.substring(0, 40) + "..."; + + return NangmanPostBoxResponseDTO.LetterSummaryResultDTO.builder() + .nangmanLetterId(nangmanLetter.getId()) + .preview(preview) + .createdAt(nangmanLetter.getCreatedAt()) + .build(); + } } diff --git a/src/main/java/aromanticcat/umcproject/entity/NangmanLetter.java b/src/main/java/aromanticcat/umcproject/entity/NangmanLetter.java index f786d29..81a6bff 100644 --- a/src/main/java/aromanticcat/umcproject/entity/NangmanLetter.java +++ b/src/main/java/aromanticcat/umcproject/entity/NangmanLetter.java @@ -18,35 +18,35 @@ public class NangmanLetter extends BaseEntity { private Long id; @NotNull - private String sender_nickname; + private String senderNickname; @NotNull - private Boolean is_public; + private Boolean isPublic; @NotNull private String content; @NotNull @Builder.Default - private Boolean has_response = false; + private Boolean hasResponse = false; - private Integer thumbs_up_cnt; + private Integer thumbsUpCnt; - private Integer heart_cnt; + private Integer heartCnt; - private Integer crying_cnt; + private Integer cryingCnt; - private Integer clover_cnt; + private Integer cloverCnt; - private Integer clap_cnt; + private Integer clapCnt; - private Integer star_cnt; + private Integer starCnt; @JoinColumn(name = "member_id") @ManyToOne(fetch = FetchType.LAZY) private Member member; - public void change(Boolean has_response){ - this.has_response = has_response; + public void change(Boolean hasResponse){ + this.hasResponse = hasResponse; } } diff --git a/src/main/java/aromanticcat/umcproject/repository/NangmanLetterRepository.java b/src/main/java/aromanticcat/umcproject/repository/NangmanLetterRepository.java index 79a52e5..c882ffb 100644 --- a/src/main/java/aromanticcat/umcproject/repository/NangmanLetterRepository.java +++ b/src/main/java/aromanticcat/umcproject/repository/NangmanLetterRepository.java @@ -3,5 +3,10 @@ import aromanticcat.umcproject.entity.NangmanLetter; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; + public interface NangmanLetterRepository extends JpaRepository { + + List findByHasResponseFalse(); + } diff --git a/src/main/java/aromanticcat/umcproject/service/NangmanLetterService.java b/src/main/java/aromanticcat/umcproject/service/NangmanPostBoxService.java similarity index 77% rename from src/main/java/aromanticcat/umcproject/service/NangmanLetterService.java rename to src/main/java/aromanticcat/umcproject/service/NangmanPostBoxService.java index 9077822..64fa2e3 100644 --- a/src/main/java/aromanticcat/umcproject/service/NangmanLetterService.java +++ b/src/main/java/aromanticcat/umcproject/service/NangmanPostBoxService.java @@ -3,10 +3,14 @@ import aromanticcat.umcproject.entity.NangmanLetter; import aromanticcat.umcproject.web.dto.NangmanPostBoxRequestDTO; -public interface NangmanLetterService { +import java.util.List; + +public interface NangmanPostBoxService { NangmanLetter writeAndSendLetter(NangmanPostBoxRequestDTO.SendLetterDTO requestDTO, String randomNickname); -// + + List getLetterList(); + // NangmanLetterDTO readOne(Long id); // // void receivedReply(NangmanLetterDTO nangmanLetterDTO); diff --git a/src/main/java/aromanticcat/umcproject/service/NangmanLetterServiceImpl.java b/src/main/java/aromanticcat/umcproject/service/NangmanPostBoxServiceImpl.java similarity index 90% rename from src/main/java/aromanticcat/umcproject/service/NangmanLetterServiceImpl.java rename to src/main/java/aromanticcat/umcproject/service/NangmanPostBoxServiceImpl.java index 9a7f54c..ed26602 100644 --- a/src/main/java/aromanticcat/umcproject/service/NangmanLetterServiceImpl.java +++ b/src/main/java/aromanticcat/umcproject/service/NangmanPostBoxServiceImpl.java @@ -9,11 +9,12 @@ import org.springframework.stereotype.Service; import javax.transaction.Transactional; +import java.util.List; @Service @RequiredArgsConstructor @Transactional -public class NangmanLetterServiceImpl implements NangmanLetterService{ +public class NangmanPostBoxServiceImpl implements NangmanPostBoxService { private final NangmanLetterRepository nangmanLetterRepository; private final MemberRepository memberRepository; @@ -26,6 +27,12 @@ public NangmanLetter writeAndSendLetter(NangmanPostBoxRequestDTO.SendLetterDTO r return nangmanLetterRepository.save(newNangmanLetter); } + + @Override + @Transactional + public List getLetterList(){ + return nangmanLetterRepository.findByHasResponseFalse(); + } // // @Override // public NangmanLetterDTO readOne(Long id){ diff --git a/src/main/java/aromanticcat/umcproject/web/controller/NangmanPostBoxController.java b/src/main/java/aromanticcat/umcproject/web/controller/NangmanPostBoxController.java index d98f7ea..8de26ac 100644 --- a/src/main/java/aromanticcat/umcproject/web/controller/NangmanPostBoxController.java +++ b/src/main/java/aromanticcat/umcproject/web/controller/NangmanPostBoxController.java @@ -3,7 +3,7 @@ import aromanticcat.umcproject.apiPayload.ApiResponse; import aromanticcat.umcproject.converter.NangmanPostBoxConverter; import aromanticcat.umcproject.entity.NangmanLetter; -import aromanticcat.umcproject.service.NangmanLetterService; +import aromanticcat.umcproject.service.NangmanPostBoxService; import aromanticcat.umcproject.service.RandomNicknameService; import aromanticcat.umcproject.web.dto.NangmanPostBoxRequestDTO; import aromanticcat.umcproject.web.dto.NangmanPostBoxResponseDTO; @@ -12,16 +12,19 @@ import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; +import java.util.List; +import java.util.stream.Collectors; + @RestController @RequestMapping("/nangman-post") @RequiredArgsConstructor public class NangmanPostBoxController { - private final NangmanLetterService nangmanLetterService; + private final NangmanPostBoxService nangmanPostBoxService; private final RandomNicknameService randomNicknameService; @GetMapping("/send/random-nickname") - @ApiOperation(value = "낭만 우편함 편지에 사용될 랜덤 닉네임 생성") + @ApiOperation(value = "낭만우편함 고민 편지에 사용될 랜덤 닉네임 생성") public ApiResponse getRandomNickname(){ try { //랜덤 닉네임 생성 @@ -36,12 +39,12 @@ public ApiResponse getRandomNickname(){ @PostMapping("/send") - @ApiOperation(value = "낭만 우편함 편지 작성") + @ApiOperation(value = "낭만우편함 고민 편지 발송") public ApiResponse sendLetter(@RequestBody NangmanPostBoxRequestDTO.SendLetterDTO request){ try{ //편지 작성 및 발송 String senderNickname = request.getSenderRandomNickname(); - NangmanLetter nangmanLetter = nangmanLetterService.writeAndSendLetter(request, senderNickname); + NangmanLetter nangmanLetter = nangmanPostBoxService.writeAndSendLetter(request, senderNickname); //성공 응답 생성 return ApiResponse.onSuccess(NangmanPostBoxConverter.toSendLetterResultDTO(nangmanLetter)); @@ -51,4 +54,25 @@ public ApiResponse sendLetter(@Re return ApiResponse.onFailure(HttpStatus.INTERNAL_SERVER_ERROR.toString(), e.getMessage(), null); } } + + @GetMapping("/letter-list") + @ApiOperation(value = "낭만우편함 답장하기 - 편지 목록") + public ApiResponse> getLetterList(){ + try{ + //편지 목록 조회 + List letterList = nangmanPostBoxService.getLetterList(); + + //편지 내용의 두 줄만 포함하도록 변환 + List letterSummaryDTOList = letterList.stream() + .map(NangmanPostBoxConverter::toLetterSummaryResultDTO) + .collect(Collectors.toList()); + + //성공 응답 생성 + return ApiResponse.onSuccess(letterSummaryDTOList); + }catch (Exception e){ + //에러 발생 시 실패 응답 반환 + return ApiResponse.onFailure(HttpStatus.INTERNAL_SERVER_ERROR.toString(), e.getMessage(), null); + + } + } } diff --git a/src/main/java/aromanticcat/umcproject/web/dto/NangmanPostBoxResponseDTO.java b/src/main/java/aromanticcat/umcproject/web/dto/NangmanPostBoxResponseDTO.java index 1408bcb..807a6d1 100644 --- a/src/main/java/aromanticcat/umcproject/web/dto/NangmanPostBoxResponseDTO.java +++ b/src/main/java/aromanticcat/umcproject/web/dto/NangmanPostBoxResponseDTO.java @@ -21,4 +21,16 @@ public static class SendLetterResultDTO { LocalDateTime createdAt; } + + @Getter + @Builder + @AllArgsConstructor + @NoArgsConstructor + public static class LetterSummaryResultDTO{ + private Long nangmanLetterId; + + private String preview; + + private LocalDateTime createdAt; + } }