From 6e8c81997287c64210f4db6e1db5791ad649c679 Mon Sep 17 00:00:00 2001 From: alsswl Date: Tue, 30 Apr 2024 16:49:49 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat(GoalService)=20:=20save=EC=99=80=20upd?= =?UTF-8?q?ate=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../likelion/todos/goal/GoalController.java | 13 ++++++++++++- .../likelion/todos/goal/GoalCreateRequest.java | 9 +++++++++ .../likelion/todos/goal/GoalService.java | 17 ++++++++++++++++- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/main/java/backend/likelion/todos/goal/GoalController.java b/src/main/java/backend/likelion/todos/goal/GoalController.java index 5cd3551..1852b4e 100644 --- a/src/main/java/backend/likelion/todos/goal/GoalController.java +++ b/src/main/java/backend/likelion/todos/goal/GoalController.java @@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; @RequiredArgsConstructor @RequestMapping("/goals") @@ -27,7 +28,17 @@ public ResponseEntity create( @RequestBody GoalCreateRequest request ) { // TODO [8단계] GoalCreateRequest에서 이름과 색상을 추출하여 goalService의 save 메소드를 호출하고, 생성된 Goal의 ID로 URI를 생성하여 ResponseEntity를 반환하세요. - return null; + String name = request.getName(); + String color = request.getColor(); + + Long goalId = goalService.save(name, color, memberId); + + URI uri = ServletUriComponentsBuilder.fromCurrentRequest() + .path("/{id}") + .buildAndExpand(goalId) + .toUri(); + + return ResponseEntity.created(uri).build(); } @PutMapping("/{id}") diff --git a/src/main/java/backend/likelion/todos/goal/GoalCreateRequest.java b/src/main/java/backend/likelion/todos/goal/GoalCreateRequest.java index d6c9f68..389ffb0 100644 --- a/src/main/java/backend/likelion/todos/goal/GoalCreateRequest.java +++ b/src/main/java/backend/likelion/todos/goal/GoalCreateRequest.java @@ -4,4 +4,13 @@ public record GoalCreateRequest( String name, String color ) { + + public String getName() { + return name; + } + + + public String getColor() { + return color; + } } diff --git a/src/main/java/backend/likelion/todos/goal/GoalService.java b/src/main/java/backend/likelion/todos/goal/GoalService.java index df64a50..ec239a5 100644 --- a/src/main/java/backend/likelion/todos/goal/GoalService.java +++ b/src/main/java/backend/likelion/todos/goal/GoalService.java @@ -1,9 +1,12 @@ package backend.likelion.todos.goal; +import backend.likelion.todos.common.ForbiddenException; import backend.likelion.todos.common.NotFoundException; import backend.likelion.todos.member.Member; import backend.likelion.todos.member.MemberRepository; import java.util.List; +import java.util.NoSuchElementException; + import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -16,16 +19,28 @@ public class GoalService { public Long save(String name, String color, Long memberId) { // TODO [8단계] memberId로 회원을 조회하고, 조회에 실패하면 "회원 정보가 없습니다." 예외를 발생시키세요. + Member member = memberRepository.findById(memberId) + .orElseThrow(() ->new NotFoundException("회원 정보가 없습니다.")); // TODO [8단계] 조회된 회원 정보를 사용하여 새 Goal 객체를 생성하세요. + Goal newGoal = new Goal(name,color,member); // TODO [8단계] 생성된 Goal 객체를 goalRepository에 저장하고, 저장된 Goal의 ID를 반환하세요. - return null; + goalRepository.save(newGoal); + return newGoal.getId(); } public void update(Long goalId, String name, String color, Long memberId) { // TODO [8단계] memberId로 회원을 조회하고, 조회에 실패하면 "회원 정보가 없습니다." 예외를 발생시키세요. + Member member = memberRepository.findById(memberId) + .orElseThrow(() ->new NotFoundException("회원 정보가 없습니다.")); // TODO [8단계] goalId로 목표(Goal)를 조회하고, 조회에 실패하면 "목표 정보가 없습니다." 예외를 발생시키세요. + Goal goal = goalRepository.findById(goalId) + .orElseThrow(() ->new NotFoundException("목표 정보가 없습니다.")); // TODO [8단계] 조회된 Goal의 회원 정보가 입력된 memberId와 일치하는지 검증하세요. + if(member.getId() != goal.getMember().getId()){ + throw new ForbiddenException("해당 목표에 대한 권한이 없습니다."); + } // TODO [8단계] Goal 객체의 정보를 새로운 name과 color로 업데이트하세요. + goal.update(name,color); } public void delete(Long goalId, Long memberId) { From c223d7dcf2643dadcc65bca0568d3ffa0797b350 Mon Sep 17 00:00:00 2001 From: alsswl Date: Tue, 30 Apr 2024 21:08:11 +0900 Subject: [PATCH 2/2] feat : level 8 --- .../likelion/todos/goal/GoalController.java | 23 +++++++++---------- .../likelion/todos/goal/GoalService.java | 18 +++++++++++++-- .../todos/goal/GoalUpdateRequest.java | 7 ++++++ .../backend/likelion/todos/todo/User.java | 16 +++++++++++++ 4 files changed, 50 insertions(+), 14 deletions(-) create mode 100644 src/main/java/backend/likelion/todos/todo/User.java diff --git a/src/main/java/backend/likelion/todos/goal/GoalController.java b/src/main/java/backend/likelion/todos/goal/GoalController.java index 1852b4e..2e2b1de 100644 --- a/src/main/java/backend/likelion/todos/goal/GoalController.java +++ b/src/main/java/backend/likelion/todos/goal/GoalController.java @@ -28,17 +28,10 @@ public ResponseEntity create( @RequestBody GoalCreateRequest request ) { // TODO [8단계] GoalCreateRequest에서 이름과 색상을 추출하여 goalService의 save 메소드를 호출하고, 생성된 Goal의 ID로 URI를 생성하여 ResponseEntity를 반환하세요. - String name = request.getName(); - String color = request.getColor(); - - Long goalId = goalService.save(name, color, memberId); - URI uri = ServletUriComponentsBuilder.fromCurrentRequest() - .path("/{id}") - .buildAndExpand(goalId) - .toUri(); + Long goalId = goalService.save(request.name(), request.color(), memberId); + return ResponseEntity.created(URI.create("/goals/" + goalId)).build(); - return ResponseEntity.created(uri).build(); } @PutMapping("/{id}") @@ -48,7 +41,11 @@ public ResponseEntity update( @RequestBody GoalUpdateRequest request ) { // TODO [8단계] GoalUpdateRequest에서 이름과 색상을 추출하고, id와 memberId를 함께 goalService의 update 메소드에 전달하여 Goal 정보를 업데이트한 후, ResponseEntity.ok()를 반환하세요. - return null; + String name = request.getName(); + String color = request.getColor(); + + goalService.update(id,name, color, memberId); + return ResponseEntity.ok().build(); } @DeleteMapping("/{id}") @@ -57,7 +54,8 @@ public ResponseEntity delete( @Auth Long memberId ) { // TODO [8단계] id와 memberId를 goalService의 delete 메소드에 전달하여 Goal을 삭제하고, ResponseEntity.ok()를 반환하세요. - return null; + goalService.delete(id,memberId); + return ResponseEntity.ok().build(); } @GetMapping("/my") @@ -65,6 +63,7 @@ public ResponseEntity> findMine( @Auth Long memberId ) { // TODO [8단계] memberId를 goalService의 findAllByMemberId 메소드에 전달하여 해당 회원의 모든 Goal 정보를 조회하고, 조회된 정보를 ResponseEntity.ok()에 담아 반환하세요. - return null; + List goals = goalService.findAllByMemberId(memberId); + return ResponseEntity.ok(goals); } } diff --git a/src/main/java/backend/likelion/todos/goal/GoalService.java b/src/main/java/backend/likelion/todos/goal/GoalService.java index ec239a5..d9a4bcb 100644 --- a/src/main/java/backend/likelion/todos/goal/GoalService.java +++ b/src/main/java/backend/likelion/todos/goal/GoalService.java @@ -6,6 +6,7 @@ import backend.likelion.todos.member.MemberRepository; import java.util.List; import java.util.NoSuchElementException; +import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -45,14 +46,27 @@ public void update(Long goalId, String name, String color, Long memberId) { public void delete(Long goalId, Long memberId) { // TODO [8단계] memberId로 회원을 조회하고, 조회에 실패하면 "회원 정보가 없습니다." 예외를 발생시키세요. + Member member = memberRepository.findById(memberId) + .orElseThrow(() -> new NotFoundException("회원 정보가 없습니다.")); // TODO [8단계] goalId로 목표(Goal)를 조회하고, 조회에 실패하면 "목표 정보가 없습니다." 예외를 발생시키세요. + Goal goal = goalRepository.findById(goalId) + .orElseThrow(() -> new NotFoundException("목표 정보가 없습니다.")); // TODO [8단계] 조회된 Goal의 회원 정보가 입력된 memberId와 일치하는지 검증하세요. - // TODO [8단계] 검증이 완료되면 Goal을 goalRepository에서 삭제하세요. + if (member.getId() != goal.getMember().getId()) { + throw new ForbiddenException("해당 목표에 대한 권한이 없습니다."); + // TODO [8단계] 검증이 완료되면 Goal을 goalRepository에서 삭제하세요. + } + goalRepository.delete(goal); } public List findAllByMemberId(Long memberId) { // TODO [8단계] memberId로 모든 목표(Goal)를 조회하세요. + List goals = goalRepository.findAllByMemberId(memberId); // TODO [8단계] 조회된 Goal들을 GoalResponse 리스트로 변환하여 반환하세요. - return null; + List responses = goals.stream() + .map(goal -> new GoalResponse(goal.getId(), goal.getName(), goal.getColor())) + .collect(Collectors.toList()); + + return responses; } } diff --git a/src/main/java/backend/likelion/todos/goal/GoalUpdateRequest.java b/src/main/java/backend/likelion/todos/goal/GoalUpdateRequest.java index 131d1d7..3ec1392 100644 --- a/src/main/java/backend/likelion/todos/goal/GoalUpdateRequest.java +++ b/src/main/java/backend/likelion/todos/goal/GoalUpdateRequest.java @@ -4,4 +4,11 @@ public record GoalUpdateRequest( String name, String color ) { + public String getName() { + return name; + } + + public String getColor() { + return color; + } } diff --git a/src/main/java/backend/likelion/todos/todo/User.java b/src/main/java/backend/likelion/todos/todo/User.java new file mode 100644 index 0000000..bfd0de3 --- /dev/null +++ b/src/main/java/backend/likelion/todos/todo/User.java @@ -0,0 +1,16 @@ +package backend.likelion.todos.todo; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class User { + private String id; + private String name; + + public User(String id, String name){ + this.id = id; + this.name = name; + } +}