Skip to content

Commit

Permalink
feat: GoalService 예외처리 추가 및 GoalController swagger 정보 추가(#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
heej-ng committed Mar 22, 2022
1 parent db9869c commit 34f5082
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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.*;
Expand All @@ -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){
Expand All @@ -27,13 +29,15 @@ public Goal create(@AuthenticationPrincipal CustomUserDetailsVO userDetailsVO,
}

// goal by id
@ApiOperation(value = "goal 하나 조회(by goalId) API", notes = "찾은 goal 객체 반환")
@GetMapping("/read/{goalId}")
public Optional<Goal> readGoalById(@AuthenticationPrincipal CustomUserDetailsVO userDetailsVO,
@PathVariable(name = "goalId") Long id){
return goalService.findGoalOfId(userDetailsVO, id);
}

// goal of member and name
@ApiOperation(value = "나의 goal 목록 조회 API", notes = "name 파라미터 추가시(필수x) 해당 이름의 goal 리스트 반환")
@GetMapping("/read/myself")
public List<Goal> listOfMyselfAndName(@AuthenticationPrincipal CustomUserDetailsVO userDetailsVO,
@RequestParam(required = false) String name){
Expand All @@ -43,6 +47,7 @@ public List<Goal> listOfMyselfAndName(@AuthenticationPrincipal CustomUserDetails
return goalService.findMyGoalsOfName(userDetailsVO, name);
}

@ApiOperation(value = "특정 멤버의 goal 목록 조회 API", notes = "name 파라미터 추가시(필수x) 해당 이름의 goal 리스트 반환")
@GetMapping("/read/member/{memberId}")
public List<Goal> listOfMemberAndName(@AuthenticationPrincipal CustomUserDetailsVO userDetailsVO,
@RequestParam(required = false) String name,
Expand All @@ -54,6 +59,7 @@ public List<Goal> listOfMemberAndName(@AuthenticationPrincipal CustomUserDetails
}

// goal of name
@ApiOperation(value = "모든 멤버의 goal 목록 조회 API", notes = "name 파라미터 추가시(필수x) 해당 이름의 goal 리스트 반환")
@GetMapping("/read/alluser")
public List<Goal> listOfName(@AuthenticationPrincipal CustomUserDetailsVO userDetailsVO,
@RequestParam(required = false) String name){
Expand All @@ -64,6 +70,7 @@ public List<Goal> listOfName(@AuthenticationPrincipal CustomUserDetailsVO userDe
}

// goal update
@ApiOperation(value = "특정 goal 내용 수정 API", notes = "수정한 goal 객체 반환")
@PutMapping("/update/{goalId}")
public Goal modifyGoal(@AuthenticationPrincipal CustomUserDetailsVO userDetailsVO,
@RequestBody GoalForm goalForm,
Expand All @@ -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){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ public List<Goal> findMyGoalsOfName(CustomUserDetailsVO cud, String name){
public List<Goal> findGoalsOfMember(Long memberId){
Optional<Member> member = memberRepository.findById(memberId);

return goalRepository.findByMember(member.get());
if(member.isPresent()) {
return goalRepository.findByMember(member.get());
} else {
return null;
}
}

/**
Expand All @@ -67,7 +71,11 @@ public List<Goal> findGoalsOfMember(Long memberId){
public List<Goal> findGoalsOfMemberAndName(Long memberId, String name){
Optional<Member> member = memberRepository.findById(memberId);

return goalRepository.findByMemberAndName(member.get(), name);
if(member.isPresent()) {
return goalRepository.findByMemberAndName(member.get(), name);
} else {
return null;
}
}

/**
Expand Down Expand Up @@ -99,13 +107,17 @@ public Optional<Goal> findGoalOfId(CustomUserDetailsVO cud, Long goalId){
public Goal updateGoalName(CustomUserDetailsVO cud, Long goalId, GoalForm goalForm){

Optional<Member> member = memberRepository.findByEmail(cud.getUsername());
Goal goal = goalRepository.findById(goalId).get();
Optional<Goal> 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);

}

/**
Expand All @@ -115,9 +127,9 @@ public int deleteGoal(CustomUserDetailsVO cud, Long goalId){

int rows = 0;
Optional<Member> member = memberRepository.findByEmail(cud.getUsername());
Goal goal = goalRepository.findById(goalId).get();
Optional<Goal> 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;
Expand Down

0 comments on commit 34f5082

Please sign in to comment.