From 34f5082336a491780eb149196ef8d71f74b5fc48 Mon Sep 17 00:00:00 2001 From: Park heejung Date: Tue, 22 Mar 2022 18:47:26 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20GoalService=20=EC=98=88=EC=99=B8?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20GoalContr?= =?UTF-8?q?oller=20swagger=20=EC=A0=95=EB=B3=B4=20=EC=B6=94=EA=B0=80(#13)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../goal/controller/GoalController.java | 8 ++++++ .../planshare/goal/service/GoalService.java | 28 +++++++++++++------ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/main/java/planshare/server/planshare/goal/controller/GoalController.java b/src/main/java/planshare/server/planshare/goal/controller/GoalController.java index f66236f..89df787 100644 --- a/src/main/java/planshare/server/planshare/goal/controller/GoalController.java +++ b/src/main/java/planshare/server/planshare/goal/controller/GoalController.java @@ -1,5 +1,6 @@ package planshare.server.planshare.goal.controller; +import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.*; @@ -18,6 +19,7 @@ public class GoalController { private final GoalService goalService; + @ApiOperation(value = "goal 생성 API", notes = "생성한 goal 객체 반환") @PostMapping("/create") public Goal create(@AuthenticationPrincipal CustomUserDetailsVO userDetailsVO, @RequestBody GoalForm goalForm){ @@ -27,6 +29,7 @@ public Goal create(@AuthenticationPrincipal CustomUserDetailsVO userDetailsVO, } // goal by id + @ApiOperation(value = "goal 하나 조회(by goalId) API", notes = "찾은 goal 객체 반환") @GetMapping("/read/{goalId}") public Optional readGoalById(@AuthenticationPrincipal CustomUserDetailsVO userDetailsVO, @PathVariable(name = "goalId") Long id){ @@ -34,6 +37,7 @@ public Optional readGoalById(@AuthenticationPrincipal CustomUserDetailsVO } // goal of member and name + @ApiOperation(value = "나의 goal 목록 조회 API", notes = "name 파라미터 추가시(필수x) 해당 이름의 goal 리스트 반환") @GetMapping("/read/myself") public List listOfMyselfAndName(@AuthenticationPrincipal CustomUserDetailsVO userDetailsVO, @RequestParam(required = false) String name){ @@ -43,6 +47,7 @@ public List listOfMyselfAndName(@AuthenticationPrincipal CustomUserDetails return goalService.findMyGoalsOfName(userDetailsVO, name); } + @ApiOperation(value = "특정 멤버의 goal 목록 조회 API", notes = "name 파라미터 추가시(필수x) 해당 이름의 goal 리스트 반환") @GetMapping("/read/member/{memberId}") public List listOfMemberAndName(@AuthenticationPrincipal CustomUserDetailsVO userDetailsVO, @RequestParam(required = false) String name, @@ -54,6 +59,7 @@ public List listOfMemberAndName(@AuthenticationPrincipal CustomUserDetails } // goal of name + @ApiOperation(value = "모든 멤버의 goal 목록 조회 API", notes = "name 파라미터 추가시(필수x) 해당 이름의 goal 리스트 반환") @GetMapping("/read/alluser") public List listOfName(@AuthenticationPrincipal CustomUserDetailsVO userDetailsVO, @RequestParam(required = false) String name){ @@ -64,6 +70,7 @@ public List listOfName(@AuthenticationPrincipal CustomUserDetailsVO userDe } // goal update + @ApiOperation(value = "특정 goal 내용 수정 API", notes = "수정한 goal 객체 반환") @PutMapping("/update/{goalId}") public Goal modifyGoal(@AuthenticationPrincipal CustomUserDetailsVO userDetailsVO, @RequestBody GoalForm goalForm, @@ -72,6 +79,7 @@ public Goal modifyGoal(@AuthenticationPrincipal CustomUserDetailsVO userDetailsV } // goal delete + @ApiOperation(value = "특정 goal 삭제 API", notes = "삭제 성공시 정수 1 반환 / 실패시 0 반환") @DeleteMapping("/delete/{goalId}") public int deleteGoal(@AuthenticationPrincipal CustomUserDetailsVO userDetailsVO, @PathVariable(name = "goalId") Long id){ diff --git a/src/main/java/planshare/server/planshare/goal/service/GoalService.java b/src/main/java/planshare/server/planshare/goal/service/GoalService.java index 81d2975..c209171 100644 --- a/src/main/java/planshare/server/planshare/goal/service/GoalService.java +++ b/src/main/java/planshare/server/planshare/goal/service/GoalService.java @@ -58,7 +58,11 @@ public List findMyGoalsOfName(CustomUserDetailsVO cud, String name){ public List findGoalsOfMember(Long memberId){ Optional member = memberRepository.findById(memberId); - return goalRepository.findByMember(member.get()); + if(member.isPresent()) { + return goalRepository.findByMember(member.get()); + } else { + return null; + } } /** @@ -67,7 +71,11 @@ public List findGoalsOfMember(Long memberId){ public List findGoalsOfMemberAndName(Long memberId, String name){ Optional member = memberRepository.findById(memberId); - return goalRepository.findByMemberAndName(member.get(), name); + if(member.isPresent()) { + return goalRepository.findByMemberAndName(member.get(), name); + } else { + return null; + } } /** @@ -99,13 +107,17 @@ public Optional findGoalOfId(CustomUserDetailsVO cud, Long goalId){ public Goal updateGoalName(CustomUserDetailsVO cud, Long goalId, GoalForm goalForm){ Optional member = memberRepository.findByEmail(cud.getUsername()); - Goal goal = goalRepository.findById(goalId).get(); + Optional goal = goalRepository.findById(goalId); - if(member.get().getId() == goal.getMember().getId()){ - goal.modifyGoal(goalForm.getName(), goalForm.getColor(), goalForm.getIcon(), goalForm.isVisibility()); + if(goal.isPresent() && member.get().getId() == goal.get().getMember().getId()){ + goal.get().modifyGoal(goalForm.getName(), goalForm.getColor(), goalForm.getIcon(), goalForm.isVisibility()); + + return goalRepository.save(goal.get()); + } else { + return null; } - return goalRepository.save(goal); + } /** @@ -115,9 +127,9 @@ public int deleteGoal(CustomUserDetailsVO cud, Long goalId){ int rows = 0; Optional member = memberRepository.findByEmail(cud.getUsername()); - Goal goal = goalRepository.findById(goalId).get(); + Optional goal = goalRepository.findById(goalId); - if(member.get().getId() == goal.getMember().getId()){ + if(goal.isPresent() && member.get().getId() == goal.get().getMember().getId()){ rows = goalRepository.deleteById(goalId); } return rows;