-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from a-romantic-cat/feat/18
✨ 낭만 우편함 편지 등록 API 구현
- Loading branch information
Showing
6 changed files
with
258 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/main/java/aromanticcat/umcproject/controller/NangmanLetterController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/aromanticcat/umcproject/dto/NangmanLetterDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
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; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/aromanticcat/umcproject/service/NangmanLetterService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package aromanticcat.umcproject.service; | ||
|
||
import aromanticcat.umcproject.dto.NangmanLetterDTO; | ||
|
||
public interface NangmanLetterService { | ||
|
||
NangmanLetterDTO register(NangmanLetterDTO nangmanLetterDTO); | ||
|
||
NangmanLetterDTO readOne(Long id); | ||
|
||
void receivedReply(NangmanLetterDTO nangmanLetterDTO); | ||
} |
79 changes: 79 additions & 0 deletions
79
src/main/java/aromanticcat/umcproject/service/NangmanLetterServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package aromanticcat.umcproject.service; | ||
|
||
import aromanticcat.umcproject.dto.NangmanLetterDTO; | ||
import aromanticcat.umcproject.entity.NangmanLetter; | ||
import aromanticcat.umcproject.repository.MemberRepository; | ||
import aromanticcat.umcproject.repository.NangmanLetterRepository; | ||
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){ | ||
|
||
// 랜덤 닉네임 생성 | ||
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){ | ||
// 낭만 모음집에 해당 편지를 추가하는 로직을 구현 | ||
// 추가되는 방법은 해당 모음집의 컬렉션에 편지를 추가하거나 다른 적절한 방법으로 구현 | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/aromanticcat/umcproject/service/RandomNicknameService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package aromanticcat.umcproject.service; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Random; | ||
|
||
@Service | ||
public class RandomNicknameService { | ||
|
||
private static final String[] ADJECTIVES = { | ||
"이야기를 나누는", "그림을 그리는", "창문 너머를 보는", "뒤척이는", "별빛을 따라 떠나는", "달콤한 꿈을 꾸는", "손에 쥐는", | ||
"어깨에 기대는", "길을 걷는", "노래하는", "햇살을 받는", "책을 읽는", "쇼핑을 하는", "생각에 잠긴", "향기에 취하는", "바람에 실려 오는", | ||
"바다를 바라보는", "눈을 감고 듣는", "손을 흔드는", "입술에 미소 지어지는", "흰 구름을 따라", "옛 추억에 빠진", "노을을 바라보는", | ||
"별들을 세는", "사진을 찍는", "운전하는", "손에 든 커피맛이 나는", "열쇠를 꽂는", "무엇인가를 찾는", "일기를 쓰는", "구름 사이를 걷는", | ||
"친구에게 전화하는", "빗소리를 듣는", "높은 곳을 내려다보는", "모자를 쓴", "새로운 일을 기대하는", "유쾌한 이야기를 나누는", "햇볕에 담근", | ||
"색다른 맛을 느끼는", "머릿결에 바람이 스치는", "두 손에 꽃을 든", "몽환적인 햇살에 비친", "달콤한 미소를 띤", "꿈결처럼 가벼운", "가슴에 품은", | ||
"순수한 눈으로 보는", "바닷가를 걷는", "강을 건너는", "별빛이 내리는", "작은 행복을 쌓는" | ||
}; | ||
|
||
private static final String[] ANIMALS = { | ||
"알파카", "토끼", "햄스터", "고슴도치", "판다", "코알라", "산토끼", "레서 판다", "푸들", "기니피그", "레서캣", | ||
"물개", "비둘기", "고양이", "강아지", "병아리", "쿼카", "펭귄", "오소리", "개구리", "반달곰", "부엉이", "아기 곰", "거북이", | ||
"오리", "나비", "올빼미", "여우", "수달", "파랑새", "황제 펭귄", "호랑이", "해달", "자라", "아기 돼지", "백조" | ||
}; | ||
|
||
public String generateRandomNickname() { | ||
Random random = new Random(); | ||
String adjective = ADJECTIVES[random.nextInt(ADJECTIVES.length)]; | ||
String animal = ANIMALS[random.nextInt(ANIMALS.length)] | ||
; | ||
return adjective + " " + animal; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/test/java/aromanticcat/umcproject/service/NangmanLetterServiceTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package aromanticcat.umcproject.service; | ||
|
||
import aromanticcat.umcproject.repository.MemberRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@SpringBootTest | ||
public class NangmanLetterServiceTests { | ||
|
||
@Autowired | ||
private NangmanLetterService nangmanLetterService; | ||
|
||
@Autowired | ||
private MemberRepository memberRepository; | ||
|
||
@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); | ||
// } | ||
} |