Skip to content

Commit

Permalink
Merge pull request #69 from TelePigeon/feat/66
Browse files Browse the repository at this point in the history
[feat] Worry API 생성
  • Loading branch information
tkdwns414 committed Jun 6, 2024
2 parents e2d1a71 + 6f2fe6b commit abe3477
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.telepigeon.server.controller;

import com.telepigeon.server.annotation.UserId;
import com.telepigeon.server.dto.worry.request.WorryCreateDto;
import com.telepigeon.server.dto.worry.response.WorriesDto;
import com.telepigeon.server.service.worry.WorryService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1")
public class WorryController {

private final WorryService worryService;

@GetMapping("/rooms/{roomId}/worries")
public ResponseEntity<WorriesDto> getWorries(
@UserId final Long userId,
@PathVariable final Long roomId
) {
return ResponseEntity.ok(worryService.getWorries(userId, roomId));
}

@PostMapping("/rooms/{roomId}/worries")
public ResponseEntity<Void> createWorry(
@UserId final Long userId,
@PathVariable final Long roomId,
@RequestBody @Valid final WorryCreateDto request
) {
worryService.createWorry(userId, roomId, request);
return ResponseEntity.created(URI.create("/rooms/"+ roomId + "/worries")).build();
}

@DeleteMapping("/worries/{worryId}")
public ResponseEntity<Void> deleteWorry(
@UserId final Long userId,
@PathVariable final Long worryId
) {
worryService.deleteWorry(userId, worryId);
return ResponseEntity.noContent().build();
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/telepigeon/server/domain/Worry.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,27 @@ public class Worry {
@ManyToOne(targetEntity=Profile.class, fetch=FetchType.LAZY)
@JoinColumn(name="profile_id")
private Profile profile;

private Worry(
final String name,
final String content,
final String times,
final Profile profile
) {
this.name = name;
this.content = content;
this.times = times;
this.profile = profile;
this.createdAt = LocalDateTime.now();
this.updatedAt = null;
}

public static Worry create(
final String name,
final String content,
final String times,
final Profile profile
) {
return new Worry(name, content, times, profile);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.telepigeon.server.dto.worry.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

import java.util.List;

public record WorryCreateDto (
@NotBlank
String name,
@NotBlank
String content,
@Size(min = 1)
List<String> times
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.telepigeon.server.dto.worry.response;

import com.telepigeon.server.domain.Worry;

import java.util.Arrays;
import java.util.List;

public record WorriesDto (
List<WorryDto> worries
) {

public static WorriesDto of(List<Worry> worries) {
return new WorriesDto(
worries.stream()
.map(WorryDto::of)
.toList()
);
}

public record WorryDto(
Long id,
String name,
String content,
List<String> times
) {
public static WorryDto of(Worry worry) {
return new WorryDto(
worry.getId(),
worry.getName(),
worry.getContent(),
Arrays.stream(worry.getTimes().split("\\|")).toList()
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.telepigeon.server.domain.Profile;
import com.telepigeon.server.domain.Worry;
import com.telepigeon.server.exception.NotFoundException;
import com.telepigeon.server.exception.code.NotFoundErrorCode;
import com.telepigeon.server.repository.WorryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
Expand All @@ -17,4 +19,10 @@ public class WorryRetriever {
public List<Worry> findAllByProfile(final Profile profile) {
return worryRepository.findAllByProfile(profile);
}

public Worry findById(Long worryId) {
return worryRepository.findById(worryId).orElseThrow(
()-> new NotFoundException(NotFoundErrorCode.WORRY_NOT_FOUND)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class WorrySaver {

private final WorryRepository worryRepository;

public Worry create(final Worry worry){
public Worry save(final Worry worry){
return worryRepository.save(worry);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.telepigeon.server.service.worry;

import com.telepigeon.server.domain.Profile;
import com.telepigeon.server.domain.Room;
import com.telepigeon.server.domain.User;
import com.telepigeon.server.domain.Worry;
import com.telepigeon.server.dto.worry.request.WorryCreateDto;
import com.telepigeon.server.dto.worry.response.WorriesDto;
import com.telepigeon.server.exception.ForbiddenException;
import com.telepigeon.server.exception.code.ForbiddenErrorCode;
import com.telepigeon.server.service.profile.ProfileRetriever;
import com.telepigeon.server.service.room.RoomRetriever;
import com.telepigeon.server.service.user.UserRetriever;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class WorryService {

private final WorryRetriever worryRetriever;
private final WorrySaver worrySaver;
private final WorryRemover worryRemover;
private final ProfileRetriever profileRetriever;
private final RoomRetriever roomRetriever;
private final UserRetriever userRetriever;

@Transactional(readOnly = true)
public WorriesDto getWorries(final Long userId, final Long roomId) {
User user = userRetriever.findById(userId);
Room room = roomRetriever.findById(roomId);
Profile profile = profileRetriever.findByUserAndRoom(user, room);
List<Worry> worries = worryRetriever.findAllByProfile(profile);
return WorriesDto.of(worries);
}

@Transactional
public void createWorry(
final Long userId,
final Long roomId,
final WorryCreateDto request
) {
User user = userRetriever.findById(userId);
Room room = roomRetriever.findById(roomId);
Profile profile = profileRetriever.findByUserAndRoom(user, room);
Worry worry = Worry.create(
request.name(),
request.content(),
String.join("|", request.times()),
profile
);
worrySaver.save(worry);
}

@Transactional
public void deleteWorry(Long userId, Long worryId) {
User user = userRetriever.findById(userId);
Worry worry = worryRetriever.findById(worryId);
if (!worry.getProfile().getUser().getId().equals(user.getId()))
throw new ForbiddenException(ForbiddenErrorCode.FORBIDDEN);
worryRemover.remove(worry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@
// Room room = Mockito.mock(Room.class);
// User user = Mockito.mock(User.class);
// Relation relation = Relation.FRIEND;
// Profile profile = Profile.create(user, room, relation, "건강");
// Profile receiver = Profile.create(user, room, relation, "건강");
// Question question = Question.create("건강", "hi", profile);
// Profile profile = Profile.save(user, room, relation, "건강");
// Profile receiver = Profile.save(user, room, relation, "건강");
// Question question = Question.save("건강", "hi", profile);
// Mockito.when(questionRetriever.findFirstByProfile(profile)).thenReturn(null);
// Mockito.when(openAiService.createQuestion(
// relation.getContent(),
// "건강")
// ).thenReturn("밥은 먹었나요?");
// Mockito.when(questionSaver.create(question)).thenReturn(question);
// Mockito.when(questionSaver.save(question)).thenReturn(question);
// Mockito.when(profileRetriever.findByUserNotAndRoom(user, room)).thenReturn(receiver);
// Mockito.when(hurryRetriever.existsByRoomIdAndSenderId(
// room.getId(),
Expand All @@ -93,7 +93,7 @@
// 1L
// )
// );
// Question question1 = questionService.create(profile);
// Question question1 = questionService.save(profile);
// Assertions.assertEquals(question.getProfile(), question1.getProfile());
// Assertions.assertEquals("밥은 먹었나요?", question1.getContent());
// }
Expand All @@ -103,27 +103,27 @@
// void checkCreateQuestion2(){
// Room room = Mockito.mock(Room.class);
// User user = Mockito.mock(User.class);
// Profile profile = Profile.create(user, room);
// Question question = Question.create("건강", "hi", profile);
// Profile profile = Profile.save(user, room);
// Question question = Question.save("건강", "hi", profile);
// Mockito.when(questionRetriever.findFirstByProfile(profile)).thenReturn(question);
// Mockito.when(answerRetriever.existsByQuestion(question)).thenReturn(false);
// Mockito.when(questionSaver.create(question)).thenReturn(question);
// Mockito.when(questionSaver.save(question)).thenReturn(question);
// Assertions.assertThrows(BusinessException.class,
// () -> questionService.create(profile));
// () -> questionService.save(profile));
// }
// @Test
// @DisplayName("Question 생성 로직 확인 - 모든 질문에 답장이 왔을 경우")
// void checkCreateQuestion3(){
// Room room = Mockito.mock(Room.class);
// User user = Mockito.mock(User.class);
// Relation relation = Relation.FRIEND;
// Profile profile = Profile.create(user, room, relation, "건강");
// Profile receiver = Profile.create(user, room, relation, "건강");
// Question question = Question.create("건강", "hi", profile);
// Profile profile = Profile.save(user, room, relation, "건강");
// Profile receiver = Profile.save(user, room, relation, "건강");
// Question question = Question.save("건강", "hi", profile);
// question.updateCreatedAt();
// Mockito.when(questionRetriever.findFirstByProfile(profile)).thenReturn(question);
// Mockito.when(answerRetriever.existsByQuestion(question)).thenReturn(true);
// Mockito.when(questionSaver.create(question)).thenReturn(question);
// Mockito.when(questionSaver.save(question)).thenReturn(question);
// Mockito.when(profileRetriever.findByUserNotAndRoom(user, room)).thenReturn(receiver);
// Mockito.when(openAiService.createQuestion(
// relation.getContent(),
Expand All @@ -140,7 +140,7 @@
// 1L
// )
// );
// Question question1 = questionService.create(profile);
// Question question1 = questionService.save(profile);
// Assertions.assertEquals(question.getProfile(), question1.getProfile());
// Assertions.assertEquals("밥은 먹었나요?", question1.getContent());
// }
Expand All @@ -152,7 +152,7 @@
// long roomId = 1L;
// Room room = Mockito.mock(Room.class);
// User user = Mockito.mock(User.class);
// Profile profile = Profile.create(user, room);
// Profile profile = Profile.save(user, room);
// Mockito.when(userRetriever.findById(userId)).thenReturn(user);
// Mockito.when(roomRetriever.findById(roomId)).thenReturn(room);
// Mockito.when(profileRetriever.findByUserNotAndRoom(user, room)).thenReturn(profile);
Expand All @@ -168,8 +168,8 @@
// long roomId = 1L;
// Room room = Mockito.mock(Room.class);
// User user = Mockito.mock(User.class);
// Profile profile = Profile.create(user, room);
// Question question = Question.create("건강", "hi", profile);
// Profile profile = Profile.save(user, room);
// Question question = Question.save("건강", "hi", profile);
// Mockito.when(userRetriever.findById(userId)).thenReturn(user);
// Mockito.when(roomRetriever.findById(roomId)).thenReturn(room);
// Mockito.when(profileRetriever.findByUserNotAndRoom(user, room)).thenReturn(profile);
Expand All @@ -186,8 +186,8 @@
// long roomId = 1L;
// Room room = Mockito.mock(Room.class);
// User user = Mockito.mock(User.class);
// Profile profile = Profile.create(user, room);
// Question question = Question.create("건강", "hi", profile);
// Profile profile = Profile.save(user, room);
// Question question = Question.save("건강", "hi", profile);
// Mockito.when(userRetriever.findById(userId)).thenReturn(user);
// Mockito.when(roomRetriever.findById(roomId)).thenReturn(room);
// Mockito.when(profileRetriever.findByUserNotAndRoom(user, room)).thenReturn(profile);
Expand Down
26 changes: 13 additions & 13 deletions src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@
// RoomCreateDto roomCreateDto = new RoomCreateDto("test");
// Long userId = 1L;
//
// Room room = Room.create(roomCreateDto, "code");
// Room room = Room.save(roomCreateDto, "code");
// User user = Mockito.mock(User.class);
// Profile profile = Profile.create(user, room);
// Profile profile = Profile.save(user, room);
//
// when(userRetriever.findById(userId)).thenReturn(user);
// when(roomRepository.existsByCode(any(String.class))).thenReturn(false);
Expand All @@ -128,7 +128,7 @@
// @DisplayName("DB에 저장된 Room을 꺼내와서 확인하기")
// public void checkRoomToDB() {
// RoomRetriever roomRetriever = new RoomRetriever(roomRepository);
// Room room = Room.create(new RoomCreateDto("name"), "code");
// Room room = Room.save(new RoomCreateDto("name"), "code");
// Mockito.doAnswer(invocation -> true).when(roomRepository).existsByName(room.getName());
// RoomCreateDto roomCreateDto = RoomCreateDto.of(room);
// boolean isCheck = roomRetriever.existsByName(roomCreateDto.name());
Expand All @@ -142,30 +142,30 @@
// Long userId1 = 1L;
//
// // User 생성
// User user1 = User.create("user1", "[email protected]", "123456", "kakao");
// User user2 = User.create("user2", "[email protected]", "125634", "kakao");
// User user1 = User.save("user1", "[email protected]", "123456", "kakao");
// User user2 = User.save("user2", "[email protected]", "125634", "kakao");
//
// // Room 생성
// RoomCreateDto roomCreateDto = new RoomCreateDto("test");
// Room createdRoom = Room.create(roomCreateDto, "code");
// Room createdRoom = Room.save(roomCreateDto, "code");
// when(roomSaver.save(any(Room.class))).thenReturn(createdRoom);
//
// // User1 Profile 생성
// Profile profile1 = Profile.create(user1, createdRoom, Relation.valueOf("CHILD"));
// Profile profile1 = Profile.save(user1, createdRoom, Relation.valueOf("CHILD"));
// when(profileSaver.save(profile1)).thenReturn(profile1);
//
// // User2 Profile 생성
// Profile profile2 = Profile.create(user2, createdRoom, Relation.valueOf("MOTHER"));
// Profile profile2 = Profile.save(user2, createdRoom, Relation.valueOf("MOTHER"));
// when(profileSaver.save(profile2)).thenReturn(profile2);
//
// Question question1 = Question.create("건강", "question1", profile1);
// Question question2 = Question.create("건강", "question2", profile2);
// Question question1 = Question.save("건강", "question1", profile1);
// Question question2 = Question.save("건강", "question2", profile2);
//
// AnswerCreateDto answerCreateDto = new AnswerCreateDto("answer1",null);
// Answer ans1 = Answer.create(answerCreateDto, 0.0, question1, profile1);
// Answer ans1 = Answer.save(answerCreateDto, 0.0, question1, profile1);
//
// AnswerCreateDto answerCreateDto2 = new AnswerCreateDto("answer2",null);
// Answer ans2 = Answer.create(answerCreateDto2, 0.0, question2, profile2);
// Answer ans2 = Answer.save(answerCreateDto2, 0.0, question2, profile2);
//
// when(profileRetriever.findByUserId(userId1)).thenReturn(Collections.singletonList(profile1));
// when(userRetriever.findById(userId1)).thenReturn(user1);
Expand Down Expand Up @@ -230,7 +230,7 @@
// RoomEnterDto roomEnterDto = new RoomEnterDto(code);
// when(roomRetriever.findByCode(code)).thenReturn(room);
//
// Profile savedProfile = Profile.create(user, room);
// Profile savedProfile = Profile.save(user, room);
// Mockito.when(profileSaver.save(savedProfile)).thenReturn(savedProfile);
//
// // When
Expand Down

0 comments on commit abe3477

Please sign in to comment.