Skip to content

Commit

Permalink
✨ 낭만 우편함 편지 등록 API 개발 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
u-genuine committed Jan 19, 2024
1 parent c8a7e8d commit fd1b20b
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -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<NangmanLetterDTO> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package aromanticcat.umcproject.dto;

import aromanticcat.umcproject.entity.Member;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -14,6 +13,7 @@
@NoArgsConstructor
public class NangmanLetterDTO {

@NotNull
private Long id;

@NotNull(message = "sender_nickname은 null일 수 없습니다.")
Expand Down Expand Up @@ -41,6 +41,5 @@ public class NangmanLetterDTO {
private Integer star_cnt;

@NotNull(message = "member는 null일 수 없습니다.")
private Member member;

private Long member_id;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<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
@@ -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;

Expand All @@ -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);
// }
}

0 comments on commit fd1b20b

Please sign in to comment.