Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ 낭만우편함 편지 작성 API 수정 #23

Merged
merged 5 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions src/main/java/aromanticcat/umcproject/config/AppConfig.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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();
}
}
45 changes: 0 additions & 45 deletions src/main/java/aromanticcat/umcproject/dto/NangmanLetterDTO.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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<NangmanLetter> 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<NangmanLetter> 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<NangmanLetter> 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<NangmanLetter> 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){
// // 낭만 모음집에 해당 편지를 추가하는 로직을 구현
// // 추가되는 방법은 해당 모음집의 컬렉션에 편지를 추가하거나 다른 적절한 방법으로 구현
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class RandomNicknameService {
private static final String[] ADJECTIVES = {
"이야기를 나누는", "그림을 그리는", "창문 너머를 보는", "뒤척이는", "별빛을 따라 떠나는", "달콤한 꿈을 꾸는", "손에 쥐는",
"어깨에 기대는", "길을 걷는", "노래하는", "햇살을 받는", "책을 읽는", "쇼핑을 하는", "생각에 잠긴", "향기에 취하는", "바람에 실려 오는",
"바다를 바라보는", "눈을 감고 듣는", "손을 흔드는", "입술에 미소 지어지는", "흰 구름을 따라", "옛 추억에 빠진", "노을을 바라보는",
"바다를 바라보는", "눈을 감고 듣는", "손을 흔드는", "입술에 미소 지어지는", "흰 구름을 따라가는", "옛 추억에 빠진", "노을을 바라보는",
"별들을 세는", "사진을 찍는", "운전하는", "손에 든 커피맛이 나는", "열쇠를 꽂는", "무엇인가를 찾는", "일기를 쓰는", "구름 사이를 걷는",
"친구에게 전화하는", "빗소리를 듣는", "높은 곳을 내려다보는", "모자를 쓴", "새로운 일을 기대하는", "유쾌한 이야기를 나누는", "햇볕에 담근",
"색다른 맛을 느끼는", "머릿결에 바람이 스치는", "두 손에 꽃을 든", "몽환적인 햇살에 비친", "달콤한 미소를 띤", "꿈결처럼 가벼운", "가슴에 품은",
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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<NangmanPostBoxResponseDTO.SendLetterResultDTO> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading