diff --git a/src/main/java/aromanticcat/umcproject/config/AppConfig.java b/src/main/java/aromanticcat/umcproject/config/AppConfig.java deleted file mode 100644 index a764755..0000000 --- a/src/main/java/aromanticcat/umcproject/config/AppConfig.java +++ /dev/null @@ -1,21 +0,0 @@ -package aromanticcat.umcproject.config; - -import org.modelmapper.ModelMapper; -import org.modelmapper.convention.MatchingStrategies; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class AppConfig { - - @Bean - public ModelMapper getMapper(){ - ModelMapper modelMapper = new ModelMapper(); - modelMapper.getConfiguration() - .setFieldMatchingEnabled(true) - .setFieldAccessLevel(org.modelmapper.config.Configuration.AccessLevel.PRIVATE) - .setMatchingStrategy(MatchingStrategies.STRICT); - - return modelMapper; - } -} diff --git a/src/main/java/aromanticcat/umcproject/controller/NangmanLetterController.java b/src/main/java/aromanticcat/umcproject/controller/NangmanLetterController.java deleted file mode 100644 index 9224798..0000000 --- a/src/main/java/aromanticcat/umcproject/controller/NangmanLetterController.java +++ /dev/null @@ -1,36 +0,0 @@ -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/converter/NangmanPostBoxConverter.java b/src/main/java/aromanticcat/umcproject/converter/NangmanPostBoxConverter.java new file mode 100644 index 0000000..a70dfce --- /dev/null +++ b/src/main/java/aromanticcat/umcproject/converter/NangmanPostBoxConverter.java @@ -0,0 +1,26 @@ +package aromanticcat.umcproject.converter; + +import aromanticcat.umcproject.entity.NangmanLetter; +import aromanticcat.umcproject.web.dto.NangmanPostBoxRequestDTO; +import aromanticcat.umcproject.web.dto.NangmanPostBoxResponseDTO; + +public class NangmanPostBoxConverter { + + public static NangmanPostBoxResponseDTO.SendLetterResultDTO toSendLetterResultDTO(NangmanLetter nangmanLetter){ + + return NangmanPostBoxResponseDTO.SendLetterResultDTO.builder() + .nangmanLetterId(nangmanLetter.getId()) + .senderNickname(nangmanLetter.getSender_nickname()) + .createdAt(nangmanLetter.getCreatedAt()) + .build(); + } + + public static NangmanLetter toNangmanLetter(NangmanPostBoxRequestDTO.SendLetterDTO request){ + return NangmanLetter.builder() + .is_public(request.getIsPublic()) + .content(request.getContent()) + .sender_nickname(request.getSenderRandomNickname()) +// .member(request.getMember()) + .build(); + } +} diff --git a/src/main/java/aromanticcat/umcproject/dto/NangmanLetterDTO.java b/src/main/java/aromanticcat/umcproject/dto/NangmanLetterDTO.java deleted file mode 100644 index a161783..0000000 --- a/src/main/java/aromanticcat/umcproject/dto/NangmanLetterDTO.java +++ /dev/null @@ -1,45 +0,0 @@ -package aromanticcat.umcproject.dto; - -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; - -import javax.validation.constraints.NotNull; - -@Data -@Builder -@AllArgsConstructor -@NoArgsConstructor -public class NangmanLetterDTO { - - @NotNull - private Long id; - - @NotNull(message = "sender_nickname은 null일 수 없습니다.") - private String sender_nickname; - - @NotNull(message = "is_public는 null일 수 없습니다.") - private Boolean is_public; - - @NotNull(message = "content는 null일 수 없습니다.") - private String content; - - @Builder.Default - private Boolean has_response = false; - - private Integer thumbs_up_cnt; - - private Integer heart_cnt; - - private Integer crying_cnt; - - private Integer clover_cnt; - - private Integer clap_cnt; - - private Integer star_cnt; - - @NotNull(message = "member는 null일 수 없습니다.") - 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 84e2df0..9077822 100644 --- a/src/main/java/aromanticcat/umcproject/service/NangmanLetterService.java +++ b/src/main/java/aromanticcat/umcproject/service/NangmanLetterService.java @@ -1,12 +1,13 @@ package aromanticcat.umcproject.service; -import aromanticcat.umcproject.dto.NangmanLetterDTO; +import aromanticcat.umcproject.entity.NangmanLetter; +import aromanticcat.umcproject.web.dto.NangmanPostBoxRequestDTO; public interface NangmanLetterService { - NangmanLetterDTO register(NangmanLetterDTO nangmanLetterDTO); - - NangmanLetterDTO readOne(Long id); - - void receivedReply(NangmanLetterDTO nangmanLetterDTO); + NangmanLetter writeAndSendLetter(NangmanPostBoxRequestDTO.SendLetterDTO requestDTO, String randomNickname); +// +// 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/NangmanLetterServiceImpl.java index 6dd28c0..9a7f54c 100644 --- a/src/main/java/aromanticcat/umcproject/service/NangmanLetterServiceImpl.java +++ b/src/main/java/aromanticcat/umcproject/service/NangmanLetterServiceImpl.java @@ -1,79 +1,65 @@ package aromanticcat.umcproject.service; -import aromanticcat.umcproject.dto.NangmanLetterDTO; +import aromanticcat.umcproject.converter.NangmanPostBoxConverter; import aromanticcat.umcproject.entity.NangmanLetter; import aromanticcat.umcproject.repository.MemberRepository; import aromanticcat.umcproject.repository.NangmanLetterRepository; +import aromanticcat.umcproject.web.dto.NangmanPostBoxRequestDTO; import lombok.RequiredArgsConstructor; -import org.modelmapper.ModelMapper; import org.springframework.stereotype.Service; import javax.transaction.Transactional; -import java.util.Optional; @Service @RequiredArgsConstructor @Transactional public class NangmanLetterServiceImpl implements NangmanLetterService{ - private final ModelMapper modelMapper; private final NangmanLetterRepository nangmanLetterRepository; private final MemberRepository memberRepository; private final RandomNicknameService randomNicknameService; @Override - public NangmanLetterDTO register(NangmanLetterDTO nangmanLetterDTO){ + @Transactional + public NangmanLetter writeAndSendLetter(NangmanPostBoxRequestDTO.SendLetterDTO request, String randomNickname){ + NangmanLetter newNangmanLetter = NangmanPostBoxConverter.toNangmanLetter(request); - // 랜덤 닉네임 생성 - String randomNickname = randomNicknameService.generateRandomNickname(); - - // DTO에 랜덤 닉네임 설정 - nangmanLetterDTO.setSender_nickname(randomNickname); - - // DTO를 엔티티로 변환 - NangmanLetter nangmanLetter = modelMapper.map(nangmanLetterDTO, NangmanLetter.class); - - // 엔티티 저장 - Long id = nangmanLetterRepository.save(nangmanLetter).getId(); - - nangmanLetterDTO.setId(id); - - return nangmanLetterDTO; - } - - @Override - public NangmanLetterDTO readOne(Long id){ - - Optional result = nangmanLetterRepository.findById(id); - - NangmanLetter nangmanLetter = result.orElseThrow(); - - NangmanLetterDTO nangmanLetterDTO = modelMapper.map(nangmanLetter, NangmanLetterDTO.class); - - return nangmanLetterDTO; - } - - - - //답장 받을 때 has_response 업데이트 - @Override - 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){ - // 낭만 모음집에 해당 편지를 추가하는 로직을 구현 - // 추가되는 방법은 해당 모음집의 컬렉션에 편지를 추가하거나 다른 적절한 방법으로 구현 + return nangmanLetterRepository.save(newNangmanLetter); } +// +// @Override +// public NangmanLetterDTO readOne(Long id){ +// +// Optional result = nangmanLetterRepository.findById(id); +// +// NangmanLetter nangmanLetter = result.orElseThrow(); +// +// NangmanLetterDTO nangmanLetterDTO = modelMapper.map(nangmanLetter, NangmanLetterDTO.class); +// +// return nangmanLetterDTO; +// } +// +// +// +// //답장 받을 때 has_response 업데이트 +// @Override +// 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/main/java/aromanticcat/umcproject/service/RandomNicknameService.java b/src/main/java/aromanticcat/umcproject/service/RandomNicknameService.java index be0b163..3486a12 100644 --- a/src/main/java/aromanticcat/umcproject/service/RandomNicknameService.java +++ b/src/main/java/aromanticcat/umcproject/service/RandomNicknameService.java @@ -10,7 +10,7 @@ public class RandomNicknameService { private static final String[] ADJECTIVES = { "이야기를 나누는", "그림을 그리는", "창문 너머를 보는", "뒤척이는", "별빛을 따라 떠나는", "달콤한 꿈을 꾸는", "손에 쥐는", "어깨에 기대는", "길을 걷는", "노래하는", "햇살을 받는", "책을 읽는", "쇼핑을 하는", "생각에 잠긴", "향기에 취하는", "바람에 실려 오는", - "바다를 바라보는", "눈을 감고 듣는", "손을 흔드는", "입술에 미소 지어지는", "흰 구름을 따라", "옛 추억에 빠진", "노을을 바라보는", + "바다를 바라보는", "눈을 감고 듣는", "손을 흔드는", "입술에 미소 지어지는", "흰 구름을 따라가는", "옛 추억에 빠진", "노을을 바라보는", "별들을 세는", "사진을 찍는", "운전하는", "손에 든 커피맛이 나는", "열쇠를 꽂는", "무엇인가를 찾는", "일기를 쓰는", "구름 사이를 걷는", "친구에게 전화하는", "빗소리를 듣는", "높은 곳을 내려다보는", "모자를 쓴", "새로운 일을 기대하는", "유쾌한 이야기를 나누는", "햇볕에 담근", "색다른 맛을 느끼는", "머릿결에 바람이 스치는", "두 손에 꽃을 든", "몽환적인 햇살에 비친", "달콤한 미소를 띤", "꿈결처럼 가벼운", "가슴에 품은", diff --git a/src/main/java/aromanticcat/umcproject/web/controller/NangmanPostBoxController.java b/src/main/java/aromanticcat/umcproject/web/controller/NangmanPostBoxController.java new file mode 100644 index 0000000..d98f7ea --- /dev/null +++ b/src/main/java/aromanticcat/umcproject/web/controller/NangmanPostBoxController.java @@ -0,0 +1,54 @@ +package aromanticcat.umcproject.web.controller; + +import aromanticcat.umcproject.apiPayload.ApiResponse; +import aromanticcat.umcproject.converter.NangmanPostBoxConverter; +import aromanticcat.umcproject.entity.NangmanLetter; +import aromanticcat.umcproject.service.NangmanLetterService; +import aromanticcat.umcproject.service.RandomNicknameService; +import aromanticcat.umcproject.web.dto.NangmanPostBoxRequestDTO; +import aromanticcat.umcproject.web.dto.NangmanPostBoxResponseDTO; +import io.swagger.annotations.ApiOperation; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/nangman-post") +@RequiredArgsConstructor +public class NangmanPostBoxController { + + private final NangmanLetterService nangmanLetterService; + private final RandomNicknameService randomNicknameService; + + @GetMapping("/send/random-nickname") + @ApiOperation(value = "낭만 우편함 편지에 사용될 랜덤 닉네임 생성") + public ApiResponse getRandomNickname(){ + try { + //랜덤 닉네임 생성 + String randomNickname = randomNicknameService.generateRandomNickname(); + + // 성공 응답 생성 + return ApiResponse.onSuccess(randomNickname); + } catch (Exception e){ + return ApiResponse.onFailure(HttpStatus.INTERNAL_SERVER_ERROR.toString(), e.getMessage(), null); + } + } + + + @PostMapping("/send") + @ApiOperation(value = "낭만 우편함 편지 작성") + public ApiResponse sendLetter(@RequestBody NangmanPostBoxRequestDTO.SendLetterDTO request){ + try{ + //편지 작성 및 발송 + String senderNickname = request.getSenderRandomNickname(); + NangmanLetter nangmanLetter = nangmanLetterService.writeAndSendLetter(request, senderNickname); + + //성공 응답 생성 + return ApiResponse.onSuccess(NangmanPostBoxConverter.toSendLetterResultDTO(nangmanLetter)); + + }catch (Exception e){ + //에러 발생 시 실패 응답 반환 + return ApiResponse.onFailure(HttpStatus.INTERNAL_SERVER_ERROR.toString(), e.getMessage(), null); + } + } +} diff --git a/src/main/java/aromanticcat/umcproject/web/dto/NangmanPostBoxRequestDTO.java b/src/main/java/aromanticcat/umcproject/web/dto/NangmanPostBoxRequestDTO.java new file mode 100644 index 0000000..ce29302 --- /dev/null +++ b/src/main/java/aromanticcat/umcproject/web/dto/NangmanPostBoxRequestDTO.java @@ -0,0 +1,15 @@ +package aromanticcat.umcproject.web.dto; + +import lombok.Getter; + +public class NangmanPostBoxRequestDTO { + + @Getter + public static class SendLetterDTO { + + private Boolean isPublic; + private String content; + private String senderRandomNickname; + private Long memberId; + } +} diff --git a/src/main/java/aromanticcat/umcproject/web/dto/NangmanPostBoxResponseDTO.java b/src/main/java/aromanticcat/umcproject/web/dto/NangmanPostBoxResponseDTO.java new file mode 100644 index 0000000..1408bcb --- /dev/null +++ b/src/main/java/aromanticcat/umcproject/web/dto/NangmanPostBoxResponseDTO.java @@ -0,0 +1,24 @@ +package aromanticcat.umcproject.web.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.time.LocalDateTime; + +public class NangmanPostBoxResponseDTO { + + @Getter + @Builder + @AllArgsConstructor + @NoArgsConstructor + public static class SendLetterResultDTO { + + private Long nangmanLetterId; + + private String senderNickname; + + LocalDateTime createdAt; + } +}