diff --git a/src/main/java/aromanticcat/umcproject/controller/NangmanLetterController.java b/src/main/java/aromanticcat/umcproject/controller/NangmanLetterController.java new file mode 100644 index 0000000..9224798 --- /dev/null +++ b/src/main/java/aromanticcat/umcproject/controller/NangmanLetterController.java @@ -0,0 +1,36 @@ +package aromanticcat.umcproject.controller; + +import aromanticcat.umcproject.apiPayload.ApiResponse; +import aromanticcat.umcproject.dto.NangmanLetterDTO; +import aromanticcat.umcproject.service.NangmanLetterService; +import io.swagger.annotations.ApiOperation; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/nangman-post") +@RequiredArgsConstructor +public class NangmanLetterController { + + private final NangmanLetterService nangmanLetterService; + + @PostMapping("/send") + @ApiOperation(value = "낭만 우편함 편지 등록") + public ApiResponse send(@RequestBody NangmanLetterDTO nangmanLetterDTO){ + try{ + //낭만 우편함 편지 작성 및 응답 DTO 변환 + NangmanLetterDTO responseDTO = nangmanLetterService.register(nangmanLetterDTO); + + //전송 성공 응답 반환 + return ApiResponse.onSuccess(responseDTO); + + }catch (Exception e){ + //에러 발생 시 실패 응답 반환 + return ApiResponse.onFailure(HttpStatus.INTERNAL_SERVER_ERROR.toString(), e.getMessage(), null); + } + } +} diff --git a/src/main/java/aromanticcat/umcproject/dto/NangmanLetterDTO.java b/src/main/java/aromanticcat/umcproject/dto/NangmanLetterDTO.java index 790adc5..a161783 100644 --- a/src/main/java/aromanticcat/umcproject/dto/NangmanLetterDTO.java +++ b/src/main/java/aromanticcat/umcproject/dto/NangmanLetterDTO.java @@ -1,6 +1,5 @@ package aromanticcat.umcproject.dto; -import aromanticcat.umcproject.entity.Member; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -14,6 +13,7 @@ @NoArgsConstructor public class NangmanLetterDTO { + @NotNull private Long id; @NotNull(message = "sender_nickname은 null일 수 없습니다.") @@ -41,6 +41,5 @@ public class NangmanLetterDTO { private Integer star_cnt; @NotNull(message = "member는 null일 수 없습니다.") - private Member member; - + private Long member_id; } diff --git a/src/main/java/aromanticcat/umcproject/service/NangmanLetterService.java b/src/main/java/aromanticcat/umcproject/service/NangmanLetterService.java index 8c4c168..84e2df0 100644 --- a/src/main/java/aromanticcat/umcproject/service/NangmanLetterService.java +++ b/src/main/java/aromanticcat/umcproject/service/NangmanLetterService.java @@ -4,9 +4,9 @@ public interface NangmanLetterService { - Long register(NangmanLetterDTO nangmanLetterDTO); + NangmanLetterDTO register(NangmanLetterDTO nangmanLetterDTO); NangmanLetterDTO readOne(Long id); - void modify(NangmanLetterDTO nangmanLetterDTO); + void receivedReply(NangmanLetterDTO nangmanLetterDTO); } diff --git a/src/main/java/aromanticcat/umcproject/service/NangmanLetterServiceImpl.java b/src/main/java/aromanticcat/umcproject/service/NangmanLetterServiceImpl.java index 4e6a4f9..6dd28c0 100644 --- a/src/main/java/aromanticcat/umcproject/service/NangmanLetterServiceImpl.java +++ b/src/main/java/aromanticcat/umcproject/service/NangmanLetterServiceImpl.java @@ -22,21 +22,23 @@ public class NangmanLetterServiceImpl implements NangmanLetterService{ private final RandomNicknameService randomNicknameService; @Override - public Long register(NangmanLetterDTO nangmanLetterDTO){ + public NangmanLetterDTO register(NangmanLetterDTO nangmanLetterDTO){ - //랜덤 닉네임 생성 + // 랜덤 닉네임 생성 String randomNickname = randomNicknameService.generateRandomNickname(); - //DTO에 랜덤 닉네임 설정 + // DTO에 랜덤 닉네임 설정 nangmanLetterDTO.setSender_nickname(randomNickname); - //DTO를 엔티티로 변환 + // DTO를 엔티티로 변환 NangmanLetter nangmanLetter = modelMapper.map(nangmanLetterDTO, NangmanLetter.class); - //엔티티 저장 + // 엔티티 저장 Long id = nangmanLetterRepository.save(nangmanLetter).getId(); - return id; + nangmanLetterDTO.setId(id); + + return nangmanLetterDTO; } @Override @@ -51,15 +53,27 @@ public NangmanLetterDTO readOne(Long id){ return nangmanLetterDTO; } + + //답장 받을 때 has_response 업데이트 @Override - public void modify(NangmanLetterDTO nangmanLetterDTO) { + public void receivedReply(NangmanLetterDTO nangmanLetterDTO) { Optional result = nangmanLetterRepository.findById(nangmanLetterDTO.getId()); NangmanLetter nangmanLetter = result.orElseThrow(); + //has_response 값 업데이트 nangmanLetter.change(nangmanLetterDTO.getHas_response()); + //is_public 값에 따라 낭만 모음집에 추가 + if(nangmanLetter.getIs_public()){ + addLetterToNangmanCollectoin(nangmanLetter); + } nangmanLetterRepository.save(nangmanLetter); } + + private void addLetterToNangmanCollectoin(NangmanLetter nangmanLetter){ + // 낭만 모음집에 해당 편지를 추가하는 로직을 구현 + // 추가되는 방법은 해당 모음집의 컬렉션에 편지를 추가하거나 다른 적절한 방법으로 구현 + } } diff --git a/src/test/java/aromanticcat/umcproject/service/NangmanLetterServiceTests.java b/src/test/java/aromanticcat/umcproject/service/NangmanLetterServiceTests.java index 5da3c47..fa86d31 100644 --- a/src/test/java/aromanticcat/umcproject/service/NangmanLetterServiceTests.java +++ b/src/test/java/aromanticcat/umcproject/service/NangmanLetterServiceTests.java @@ -1,9 +1,6 @@ package aromanticcat.umcproject.service; -import aromanticcat.umcproject.dto.NangmanLetterDTO; -import aromanticcat.umcproject.entity.Member; import aromanticcat.umcproject.repository.MemberRepository; -import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -19,38 +16,38 @@ public class NangmanLetterServiceTests { @Autowired private RandomNicknameService randomNicknameService; - @Test - public void testRegister(){ - System.out.println(nangmanLetterService.getClass().getName()); - - Member member1 = memberRepository.findById(1L).orElseThrow(() -> new RuntimeException("Member not found")); - - //랜덤 닉네임 생성 - String randomNickname = randomNicknameService.generateRandomNickname(); - - NangmanLetterDTO nangmanLetterDTO = NangmanLetterDTO.builder() - .content("낭만 편지 샘플3") - .is_public(false) - .member(member1) - .build(); - - //생성된 랜덤 닉네임 설정 - nangmanLetterDTO.setSender_nickname(randomNickname); - - Long id = nangmanLetterService.register(nangmanLetterDTO); - - System.out.println("id: " + id); - } - - @Test - public void testModify(){ - - //변경에 필요한 데이터만 - NangmanLetterDTO nangmanLetterDTO = NangmanLetterDTO.builder() - .id(4L) - .has_response(true) - .build(); - - nangmanLetterService.modify(nangmanLetterDTO); - } +// @Test +// public void testRegister(){ +// System.out.println(nangmanLetterService.getClass().getName()); +// +// Member member1 = memberRepository.findById(1L).orElseThrow(() -> new RuntimeException("Member not found")); +// +// //랜덤 닉네임 생성 +// String randomNickname = randomNicknameService.generateRandomNickname(); +// +// NangmanLetterDTO nangmanLetterDTO = NangmanLetterDTO.builder() +// .content("낭만 편지 샘플3") +// .is_public(false) +// .member(member1) +// .build(); +// +// //생성된 랜덤 닉네임 설정 +// nangmanLetterDTO.setSender_nickname(randomNickname); +// +// Long id = nangmanLetterService.register(nangmanLetterDTO); +// +// System.out.println("id: " + id); +// } + +// @Test +// public void testModify(){ +// +// //변경에 필요한 데이터만 +// NangmanLetterDTO nangmanLetterDTO = NangmanLetterDTO.builder() +// .id(4L) +// .has_response(true) +// .build(); +// +// nangmanLetterService.modify(nangmanLetterDTO); +// } }