-
Notifications
You must be signed in to change notification settings - Fork 0
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 #69 from TelePigeon/feat/66
[feat] Worry API 생성
- Loading branch information
Showing
9 changed files
with
228 additions
and
33 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/main/java/com/telepigeon/server/controller/WorryController.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,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(); | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
src/main/java/com/telepigeon/server/dto/worry/request/WorryCreateDto.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,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 | ||
) { | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/telepigeon/server/dto/worry/response/WorriesDto.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,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() | ||
); | ||
} | ||
} | ||
} |
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
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
66 changes: 66 additions & 0 deletions
66
src/main/java/com/telepigeon/server/service/worry/WorryService.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,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); | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -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); | ||
|
@@ -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()); | ||
|
@@ -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); | ||
|
@@ -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 | ||
|