-
Notifications
You must be signed in to change notification settings - Fork 0
[#22] Feature : 목표 생성, 수정, 완료, 삭제, 목록 조회 및 완료 목록 조회 API #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
80cb826
67b43ae
adbcc50
38c1128
e1959f0
80496cc
d54e00e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package com.project.poorlex.api.controller; | ||
|
|
||
| import com.project.poorlex.api.service.GoalService; | ||
| import com.project.poorlex.dto.goal.*; | ||
| import com.project.poorlex.exception.ApiResponse; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/goal") | ||
| @RequiredArgsConstructor | ||
| public class GoalController { | ||
|
|
||
| private final GoalService goalService; | ||
|
|
||
| @PostMapping | ||
| public ApiResponse<GoalCreateResponse> createGoal( | ||
| @RequestBody GoalCreateRequest request) { | ||
|
|
||
| return ApiResponse.ok(goalService.createGoal(request)); | ||
| } | ||
|
|
||
| @GetMapping | ||
| public ApiResponse<List<GoalListResponse>> getAllGoals(@RequestParam String oauthId) { | ||
|
|
||
| return ApiResponse.ok(goalService.getAllGoals(oauthId)); | ||
| } | ||
|
|
||
| @GetMapping("/completedList") | ||
| public ApiResponse<List<GoalListResponse>> getAllCompletedGoals(@RequestParam String oauthId) { | ||
|
|
||
| return ApiResponse.ok(goalService.getAllCompletedGoals(oauthId)); | ||
| } | ||
|
|
||
| @PutMapping("/{id}") | ||
| public ApiResponse<GoalUpdateResponse> updateGoal( | ||
| @PathVariable Long id, | ||
| @RequestBody GoalUpdateRequest request) { | ||
|
|
||
| return ApiResponse.ok(goalService.updateGoal(request, id)); | ||
| } | ||
|
|
||
| @PutMapping("/complete/{id}") | ||
| public ApiResponse<GoalUpdateResponse> completeGoal(@PathVariable Long id, @RequestBody GoalUpdateRequest request) { | ||
|
|
||
| return ApiResponse.ok(goalService.goalCompleted(id, request)); | ||
| } | ||
|
|
||
| @DeleteMapping("/{id}") | ||
| public ApiResponse<?> deleteGoal(@PathVariable Long id) { | ||
|
|
||
| return ApiResponse.ok(goalService.deleteGoal(id)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| package com.project.poorlex.api.service; | ||
|
|
||
| import com.project.poorlex.domain.goal.Goal; | ||
| import com.project.poorlex.domain.goal.GoalRepository; | ||
| import com.project.poorlex.domain.member.Member; | ||
| import com.project.poorlex.dto.goal.*; | ||
| import com.project.poorlex.exception.goal.GoalCustomException; | ||
| import com.project.poorlex.exception.goal.GoalErrorCode; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class GoalService { | ||
|
|
||
| private final GoalRepository goalRepository; | ||
|
|
||
| private final AuthService authService; | ||
|
|
||
| public GoalCreateResponse createGoal(GoalCreateRequest request) { | ||
|
|
||
| Member member = authService.findMemberFromToken(); | ||
|
|
||
| Goal goal = Goal.builder() | ||
| .member(member) | ||
| .name(request.getName()) | ||
| .amount(request.getAmount()) | ||
| .currentAmount(request.getCurrentAmount()) | ||
| .startDate(request.getStartDate()) | ||
| .endDate(request.getEndDate()) | ||
| .isCompleted(request.isCompleted()) | ||
| .build(); | ||
|
|
||
| goalRepository.save(goal); | ||
|
|
||
| return GoalCreateResponse.of(goal); | ||
| } | ||
|
|
||
| public List<GoalListResponse> getAllGoals(String oauthId) { | ||
|
|
||
|
|
||
| List<Goal> goalList = goalRepository.findAllByMemberId_OauthIdAndIsCompletedFalse(oauthId); | ||
|
|
||
| return GoalListResponse.of(goalList); | ||
| } | ||
|
|
||
| public List<GoalListResponse> getAllCompletedGoals(String oauthId) { | ||
|
|
||
| List<Goal> goalList = goalRepository.findAllByMemberId_OauthIdAndIsCompletedTrue(oauthId); | ||
|
|
||
| return GoalListResponse.of(goalList); | ||
| } | ||
|
|
||
| public GoalUpdateResponse updateGoal(GoalUpdateRequest request, Long id) { | ||
|
|
||
| Member member = authService.findMemberFromToken(); | ||
| Goal existingGoalId = goalRepository.findById(id) | ||
| .orElseThrow(() -> new GoalCustomException(GoalErrorCode.GOAL_NOT_FOUND)); | ||
|
|
||
| Goal updatedGoal = Goal.builder() | ||
| .id(existingGoalId.getId()) | ||
| .member(member) | ||
| .amount(request.getAmount()) | ||
| .currentAmount(request.getCurrentAmount()) | ||
| .name(request.getName()) | ||
| .startDate(request.getStartDate()) | ||
| .endDate(request.getEndDate()) | ||
| .isCompleted(false) | ||
| .build(); | ||
|
|
||
| goalRepository.save(updatedGoal); | ||
|
|
||
| return GoalUpdateResponse.of(updatedGoal); | ||
| } | ||
|
|
||
| public GoalUpdateResponse goalCompleted(Long id, GoalUpdateRequest request) { | ||
|
|
||
| Goal existingGoalId = goalRepository.findById(id) | ||
| .orElseThrow(() -> new GoalCustomException(GoalErrorCode.GOAL_NOT_FOUND)); | ||
| Goal completedGoal = Goal.builder() | ||
| .id(existingGoalId.getId()) | ||
| .amount(request.getAmount()) | ||
| .currentAmount(request.getCurrentAmount()) | ||
| .name(request.getName()) | ||
| .startDate(request.getStartDate()) | ||
| .endDate(request.getEndDate()) | ||
| .isCompleted(true) | ||
| .build(); | ||
|
|
||
| goalRepository.save(completedGoal); | ||
|
|
||
| return GoalUpdateResponse.of(completedGoal); | ||
| } | ||
|
|
||
| public boolean deleteGoal(Long id) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| Goal existingGoalId = goalRepository.findById(id) | ||
| .orElseThrow(() -> new GoalCustomException(GoalErrorCode.GOAL_NOT_FOUND)); | ||
|
|
||
| goalRepository.deleteById(existingGoalId.getId()); | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.project.poorlex.dto.goal; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonFormat; | ||
| import java.time.LocalDateTime; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @AllArgsConstructor | ||
| @NoArgsConstructor | ||
| @Getter | ||
| @Builder | ||
| public class GoalCreateRequest { | ||
|
|
||
| private String oauthId; | ||
| private String name; | ||
| private int amount; | ||
| private int currentAmount; | ||
| @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 목표 입력 및 수정할 때 받는 Time이 사용되는 곳이 있나요?!
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 목표 시작일, 종료일 수정하는 경우가 있다고 생각해서 사용했습니다 ..! 수정이 필요할까요 ? ! |
||
| private LocalDateTime startDate; | ||
| @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") | ||
| private LocalDateTime endDate; | ||
| private boolean isCompleted; | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.project.poorlex.dto.goal; | ||
|
|
||
| import com.project.poorlex.domain.goal.Goal; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| @Getter | ||
| @Builder | ||
| public class GoalCreateResponse { | ||
|
|
||
| Long id; | ||
|
|
||
| String message; | ||
|
|
||
| public static GoalCreateResponse of(Goal goal) { | ||
|
|
||
| return GoalCreateResponse.builder() | ||
| .id(goal.getId()) | ||
| .message("목표가 생성되었습니다.") | ||
| .build(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package com.project.poorlex.dto.goal; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonFormat; | ||
| import java.time.LocalDateTime; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import com.project.poorlex.domain.goal.Goal; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| @Getter | ||
| @Builder | ||
| public class GoalListResponse { | ||
|
|
||
| private Long id; | ||
|
|
||
| String name; | ||
|
|
||
| String message; | ||
|
|
||
| private int amount; | ||
|
|
||
| private int currentAmount; | ||
|
|
||
| @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") | ||
| private LocalDateTime startDate; | ||
|
|
||
| @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") | ||
| private LocalDateTime endDate; | ||
|
|
||
| private boolean isCompleted; | ||
|
|
||
| public static List<GoalListResponse> of(List<Goal> goals) { | ||
|
|
||
| List<GoalListResponse> goalListResponseList = new ArrayList<>(); | ||
|
|
||
| if (goals != null) { | ||
| for (Goal goal : goals) { | ||
| goalListResponseList.add(GoalListResponse.builder() | ||
| .id(goal.getId()) | ||
| .name(goal.getName()) | ||
| .amount(goal.getAmount()) | ||
| .currentAmount(goal.getCurrentAmount()) | ||
| .startDate(goal.getStartDate()) | ||
| .endDate(goal.getEndDate()) | ||
| .isCompleted(goal.isCompleted()) | ||
| .message("리스트가 조회되었습니다.") | ||
| .build()); | ||
| } | ||
| } | ||
| return goalListResponseList; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.project.poorlex.dto.goal; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonFormat; | ||
| import java.time.LocalDateTime; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| @Getter | ||
| @Builder | ||
| public class GoalUpdateRequest { | ||
|
|
||
| private String oauthId; | ||
|
|
||
| private Long id; | ||
|
|
||
| private String name; | ||
|
|
||
| private int amount; | ||
|
|
||
| private int currentAmount; | ||
|
|
||
| @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") | ||
| private LocalDateTime startDate; | ||
|
|
||
| @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") | ||
| private LocalDateTime endDate; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package com.project.poorlex.dto.goal; | ||
|
|
||
| import com.project.poorlex.domain.goal.Goal; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| @Getter | ||
| @Builder | ||
| public class GoalUpdateResponse { | ||
|
|
||
| Long id; | ||
| String message; | ||
|
|
||
| public static GoalUpdateResponse of(Goal goal) { | ||
|
|
||
| return GoalUpdateResponse.builder() | ||
| .id(goal.getId()) | ||
| .message("목표가 수정되었습니다.") | ||
| .build(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.project.poorlex.exception.goal; | ||
|
|
||
|
|
||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| @Getter | ||
| public class GoalCustomException extends RuntimeException { | ||
|
|
||
| private final GoalErrorCode goalErrorCode; | ||
| public GoalCustomException(GoalErrorCode goalErrorCode) { | ||
| super(goalErrorCode.getDescription()); | ||
| this.goalErrorCode = goalErrorCode; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.project.poorlex.exception.goal; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public enum GoalErrorCode { | ||
|
|
||
| GOAL_NOT_FOUND("목표명이 존재하지 않습니다."); | ||
|
|
||
| private final String description; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
goalRepository변경부분이 없어 에러가 뜨네요!커밋 확인해보셔야할거 같아요!