From fdf0783ce832ae8b9c0210683f4a050e5c8fe30e Mon Sep 17 00:00:00 2001 From: tkdwns414 Date: Wed, 5 Jun 2024 23:22:22 +0900 Subject: [PATCH 1/5] [feat] #66 create getWorries API --- .../server/controller/WorryController.java | 26 ++++++++++++++ .../server/dto/worry/WorriesDto.java | 35 +++++++++++++++++++ .../server/service/worry/WorryService.java | 34 ++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 src/main/java/com/telepigeon/server/controller/WorryController.java create mode 100644 src/main/java/com/telepigeon/server/dto/worry/WorriesDto.java create mode 100644 src/main/java/com/telepigeon/server/service/worry/WorryService.java diff --git a/src/main/java/com/telepigeon/server/controller/WorryController.java b/src/main/java/com/telepigeon/server/controller/WorryController.java new file mode 100644 index 0000000..f0ebfbc --- /dev/null +++ b/src/main/java/com/telepigeon/server/controller/WorryController.java @@ -0,0 +1,26 @@ +package com.telepigeon.server.controller; + +import com.telepigeon.server.annotation.UserId; +import com.telepigeon.server.service.worry.WorryService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/v1") +public class WorryController { + + private final WorryService worryService; + + @GetMapping("/rooms/{roomId}/worries") + public ResponseEntity getWorries( + @UserId final Long userId, + @PathVariable final Long roomId + ) { + return ResponseEntity.ok(worryService.getWorries(userId, roomId)); + } +} diff --git a/src/main/java/com/telepigeon/server/dto/worry/WorriesDto.java b/src/main/java/com/telepigeon/server/dto/worry/WorriesDto.java new file mode 100644 index 0000000..1ce2bed --- /dev/null +++ b/src/main/java/com/telepigeon/server/dto/worry/WorriesDto.java @@ -0,0 +1,35 @@ +package com.telepigeon.server.dto.worry; + +import com.telepigeon.server.domain.Worry; + +import java.util.Arrays; +import java.util.List; + +public record WorriesDto ( + List worries +) { + + public static WorriesDto of(List worries) { + return new WorriesDto( + worries.stream() + .map(WorryDto::of) + .toList() + ); + } + + public record WorryDto( + Long id, + String name, + String content, + List times + ) { + public static WorryDto of(Worry worry) { + return new WorryDto( + worry.getId(), + worry.getName(), + worry.getContent(), + Arrays.stream(worry.getTimes().split("\\|")).toList() + ); + } + } +} diff --git a/src/main/java/com/telepigeon/server/service/worry/WorryService.java b/src/main/java/com/telepigeon/server/service/worry/WorryService.java new file mode 100644 index 0000000..e501567 --- /dev/null +++ b/src/main/java/com/telepigeon/server/service/worry/WorryService.java @@ -0,0 +1,34 @@ +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.WorriesDto; +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 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; + + 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 worries = worryRetriever.findAllByProfile(profile); + return WorriesDto.of(worries); + } +} From 6d2638edf8c98e9668f45b49e3339cd8eaf02728 Mon Sep 17 00:00:00 2001 From: tkdwns414 Date: Wed, 5 Jun 2024 23:41:46 +0900 Subject: [PATCH 2/5] [feat] #66 create createWorry API --- .../server/controller/WorryController.java | 18 +++++++-- .../com/telepigeon/server/domain/Worry.java | 23 +++++++++++ .../dto/worry/request/WorryCreateDto.java | 10 +++++ .../dto/worry/{ => response}/WorriesDto.java | 2 +- .../server/service/worry/WorrySaver.java | 2 +- .../server/service/worry/WorryService.java | 24 +++++++++++- .../server/question/QuestionServiceTest.java | 38 +++++++++---------- .../server/roomTest/RoomServiceTest.java | 26 ++++++------- 8 files changed, 104 insertions(+), 39 deletions(-) create mode 100644 src/main/java/com/telepigeon/server/dto/worry/request/WorryCreateDto.java rename src/main/java/com/telepigeon/server/dto/worry/{ => response}/WorriesDto.java (94%) diff --git a/src/main/java/com/telepigeon/server/controller/WorryController.java b/src/main/java/com/telepigeon/server/controller/WorryController.java index f0ebfbc..a8f847d 100644 --- a/src/main/java/com/telepigeon/server/controller/WorryController.java +++ b/src/main/java/com/telepigeon/server/controller/WorryController.java @@ -1,13 +1,13 @@ package com.telepigeon.server.controller; import com.telepigeon.server.annotation.UserId; +import com.telepigeon.server.dto.worry.request.WorryCreateDto; import com.telepigeon.server.service.worry.WorryService; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; + +import java.net.URI; @RestController @RequiredArgsConstructor @@ -23,4 +23,14 @@ public ResponseEntity getWorries( ) { return ResponseEntity.ok(worryService.getWorries(userId, roomId)); } + + @PostMapping("/rooms/{roomId}/worries") + public ResponseEntity createWorry( + @UserId final Long userId, + @PathVariable final Long roomId, + @RequestBody final WorryCreateDto request + ) { + worryService.createWorry(userId, roomId, request); + return ResponseEntity.created(URI.create("/rooms/"+ roomId + "/worries")).build(); + } } diff --git a/src/main/java/com/telepigeon/server/domain/Worry.java b/src/main/java/com/telepigeon/server/domain/Worry.java index 28429f5..ae99714 100644 --- a/src/main/java/com/telepigeon/server/domain/Worry.java +++ b/src/main/java/com/telepigeon/server/domain/Worry.java @@ -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); + } } diff --git a/src/main/java/com/telepigeon/server/dto/worry/request/WorryCreateDto.java b/src/main/java/com/telepigeon/server/dto/worry/request/WorryCreateDto.java new file mode 100644 index 0000000..7e59f66 --- /dev/null +++ b/src/main/java/com/telepigeon/server/dto/worry/request/WorryCreateDto.java @@ -0,0 +1,10 @@ +package com.telepigeon.server.dto.worry.request; + +import java.util.List; + +public record WorryCreateDto ( + String name, + String content, + List times +) { +} diff --git a/src/main/java/com/telepigeon/server/dto/worry/WorriesDto.java b/src/main/java/com/telepigeon/server/dto/worry/response/WorriesDto.java similarity index 94% rename from src/main/java/com/telepigeon/server/dto/worry/WorriesDto.java rename to src/main/java/com/telepigeon/server/dto/worry/response/WorriesDto.java index 1ce2bed..5d0deec 100644 --- a/src/main/java/com/telepigeon/server/dto/worry/WorriesDto.java +++ b/src/main/java/com/telepigeon/server/dto/worry/response/WorriesDto.java @@ -1,4 +1,4 @@ -package com.telepigeon.server.dto.worry; +package com.telepigeon.server.dto.worry.response; import com.telepigeon.server.domain.Worry; diff --git a/src/main/java/com/telepigeon/server/service/worry/WorrySaver.java b/src/main/java/com/telepigeon/server/service/worry/WorrySaver.java index ad80ab8..d85d3ec 100644 --- a/src/main/java/com/telepigeon/server/service/worry/WorrySaver.java +++ b/src/main/java/com/telepigeon/server/service/worry/WorrySaver.java @@ -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); } } diff --git a/src/main/java/com/telepigeon/server/service/worry/WorryService.java b/src/main/java/com/telepigeon/server/service/worry/WorryService.java index e501567..f4c4931 100644 --- a/src/main/java/com/telepigeon/server/service/worry/WorryService.java +++ b/src/main/java/com/telepigeon/server/service/worry/WorryService.java @@ -4,12 +4,14 @@ import com.telepigeon.server.domain.Room; import com.telepigeon.server.domain.User; import com.telepigeon.server.domain.Worry; -import com.telepigeon.server.dto.worry.WorriesDto; +import com.telepigeon.server.dto.worry.request.WorryCreateDto; +import com.telepigeon.server.dto.worry.response.WorriesDto; 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; @@ -24,6 +26,7 @@ public class WorryService { 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); @@ -31,4 +34,23 @@ public WorriesDto getWorries(final Long userId, final Long roomId) { List 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); + } + } diff --git a/src/test/java/com/telepigeon/server/question/QuestionServiceTest.java b/src/test/java/com/telepigeon/server/question/QuestionServiceTest.java index 5835383..29e56e6 100644 --- a/src/test/java/com/telepigeon/server/question/QuestionServiceTest.java +++ b/src/test/java/com/telepigeon/server/question/QuestionServiceTest.java @@ -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(), @@ -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()); // } @@ -103,13 +103,13 @@ // 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 생성 로직 확인 - 모든 질문에 답장이 왔을 경우") @@ -117,13 +117,13 @@ // 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(), @@ -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()); // } @@ -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); @@ -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); @@ -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); diff --git a/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java b/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java index e8bce7c..0289a26 100644 --- a/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java +++ b/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java @@ -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", "user1@na.com", "123456", "kakao"); -// User user2 = User.create("user2", "user2@na.com", "125634", "kakao"); +// User user1 = User.save("user1", "user1@na.com", "123456", "kakao"); +// User user2 = User.save("user2", "user2@na.com", "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 From 3be21411b5bc0a2dc62c03574e939bf86c6f3315 Mon Sep 17 00:00:00 2001 From: tkdwns414 Date: Wed, 5 Jun 2024 23:54:44 +0900 Subject: [PATCH 3/5] [feat] #66 create deleteWorry API --- .../server/controller/WorryController.java | 14 ++++++++++++-- .../server/service/worry/WorryRetriever.java | 9 +++++++++ .../server/service/worry/WorryService.java | 12 +++++++++++- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/telepigeon/server/controller/WorryController.java b/src/main/java/com/telepigeon/server/controller/WorryController.java index a8f847d..8559b0e 100644 --- a/src/main/java/com/telepigeon/server/controller/WorryController.java +++ b/src/main/java/com/telepigeon/server/controller/WorryController.java @@ -2,6 +2,7 @@ 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 lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; @@ -17,7 +18,7 @@ public class WorryController { private final WorryService worryService; @GetMapping("/rooms/{roomId}/worries") - public ResponseEntity getWorries( + public ResponseEntity getWorries( @UserId final Long userId, @PathVariable final Long roomId ) { @@ -25,7 +26,7 @@ public ResponseEntity getWorries( } @PostMapping("/rooms/{roomId}/worries") - public ResponseEntity createWorry( + public ResponseEntity createWorry( @UserId final Long userId, @PathVariable final Long roomId, @RequestBody final WorryCreateDto request @@ -33,4 +34,13 @@ public ResponseEntity createWorry( worryService.createWorry(userId, roomId, request); return ResponseEntity.created(URI.create("/rooms/"+ roomId + "/worries")).build(); } + + @DeleteMapping("/worries/{worryId}") + public ResponseEntity deleteWorry( + @UserId final Long userId, + @PathVariable final Long worryId + ) { + worryService.deleteWorry(userId, worryId); + return ResponseEntity.noContent().build(); + } } diff --git a/src/main/java/com/telepigeon/server/service/worry/WorryRetriever.java b/src/main/java/com/telepigeon/server/service/worry/WorryRetriever.java index c17dab2..573a553 100644 --- a/src/main/java/com/telepigeon/server/service/worry/WorryRetriever.java +++ b/src/main/java/com/telepigeon/server/service/worry/WorryRetriever.java @@ -2,8 +2,11 @@ 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.aspectj.weaver.ast.Not; import org.springframework.stereotype.Component; import java.util.List; @@ -17,4 +20,10 @@ public class WorryRetriever { public List findAllByProfile(final Profile profile) { return worryRepository.findAllByProfile(profile); } + + public Worry findById(Long worryId) { + return worryRepository.findById(worryId).orElseThrow( + ()-> new NotFoundException(NotFoundErrorCode.WORRY_NOT_FOUND) + ); + } } diff --git a/src/main/java/com/telepigeon/server/service/worry/WorryService.java b/src/main/java/com/telepigeon/server/service/worry/WorryService.java index f4c4931..08e80bb 100644 --- a/src/main/java/com/telepigeon/server/service/worry/WorryService.java +++ b/src/main/java/com/telepigeon/server/service/worry/WorryService.java @@ -6,6 +6,8 @@ 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; @@ -47,10 +49,18 @@ public void createWorry( Worry worry = Worry.create( request.name(), request.content(), - String.join("\\|", request.times()), + 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); + } } From a00f0d5ae0747fcd0bd815ac57e1882be7bc3857 Mon Sep 17 00:00:00 2001 From: tkdwns414 Date: Wed, 5 Jun 2024 23:55:27 +0900 Subject: [PATCH 4/5] [feat] #66 rm unused import --- .../java/com/telepigeon/server/service/worry/WorryRetriever.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/telepigeon/server/service/worry/WorryRetriever.java b/src/main/java/com/telepigeon/server/service/worry/WorryRetriever.java index 573a553..049bcf6 100644 --- a/src/main/java/com/telepigeon/server/service/worry/WorryRetriever.java +++ b/src/main/java/com/telepigeon/server/service/worry/WorryRetriever.java @@ -6,7 +6,6 @@ import com.telepigeon.server.exception.code.NotFoundErrorCode; import com.telepigeon.server.repository.WorryRepository; import lombok.RequiredArgsConstructor; -import org.aspectj.weaver.ast.Not; import org.springframework.stereotype.Component; import java.util.List; From 6f2fe6b295d812b9e0e87518342bcb772283e585 Mon Sep 17 00:00:00 2001 From: tkdwns414 Date: Thu, 6 Jun 2024 13:50:18 +0900 Subject: [PATCH 5/5] =?UTF-8?q?[feat]=20#66=20validation=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/telepigeon/server/controller/WorryController.java | 3 ++- .../telepigeon/server/dto/worry/request/WorryCreateDto.java | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/telepigeon/server/controller/WorryController.java b/src/main/java/com/telepigeon/server/controller/WorryController.java index 8559b0e..1af739d 100644 --- a/src/main/java/com/telepigeon/server/controller/WorryController.java +++ b/src/main/java/com/telepigeon/server/controller/WorryController.java @@ -4,6 +4,7 @@ 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.*; @@ -29,7 +30,7 @@ public ResponseEntity getWorries( public ResponseEntity createWorry( @UserId final Long userId, @PathVariable final Long roomId, - @RequestBody final WorryCreateDto request + @RequestBody @Valid final WorryCreateDto request ) { worryService.createWorry(userId, roomId, request); return ResponseEntity.created(URI.create("/rooms/"+ roomId + "/worries")).build(); diff --git a/src/main/java/com/telepigeon/server/dto/worry/request/WorryCreateDto.java b/src/main/java/com/telepigeon/server/dto/worry/request/WorryCreateDto.java index 7e59f66..785441c 100644 --- a/src/main/java/com/telepigeon/server/dto/worry/request/WorryCreateDto.java +++ b/src/main/java/com/telepigeon/server/dto/worry/request/WorryCreateDto.java @@ -1,10 +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 times ) { }