Skip to content

Commit

Permalink
Merge pull request #121 from BudgetBuddiesTeam/dev
Browse files Browse the repository at this point in the history
[v4] consumptionGoal 공통 응답 처리
  • Loading branch information
m3k0813 authored Aug 15, 2024
2 parents e43842b + a397439 commit 440b935
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -1,70 +1,86 @@
package com.bbteam.budgetbuddies.domain.consumptiongoal.controller;

import java.time.LocalDate;
import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;

import com.bbteam.budgetbuddies.apiPayload.ApiResponse;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AllConsumptionCategoryResponseDto;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionAnalysisResponseDto;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalListRequestDto;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseListDto;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.PeerInfoResponseDto;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopCategoryConsumptionDto;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.TopGoalCategoryResponseDto;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;

public interface ConsumptionGoalApi {

@Operation(summary = "또래들이 가장 큰 계획을 세운 카테고리 조회 Top4 API", description = "특정 사용자의 소비 목표 카테고리별 소비 목표 금액을 조회하는 API 입니다.")
@ApiResponses(value = {@ApiResponse(responseCode = "COMMON200", description = "OK, 성공")})
@ApiResponses(value = {
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공")})
@Parameters({@Parameter(name = "userId", description = "로그인 한 유저 아이디"),
@Parameter(name = "peerAgeStart", description = "또래나이 시작 범위"),
@Parameter(name = "peerAgeEnd", description = "또래나이 끝 범위"),
@Parameter(name = "peerGender", description = "또래 성별")})
ResponseEntity<?> getTopConsumptionGoalCategories(Long userId, int peerAgeStart, int peerAgeEnd, String peerGender);
ApiResponse<List<TopGoalCategoryResponseDto>> getTopConsumptionGoalCategories(
Long userId, int peerAgeStart, int peerAgeEnd, String peerGender);

@Operation(summary = "또래들이 가장 많이 계획한 카테고리와 평균 금액 및 내 목표금액 차이 조회 API", description = "특정 사용자의 또래 소비 카테고리별 평균 목표 금액을 조회하는 API 입니다.")
@ApiResponses(value = {@ApiResponse(responseCode = "COMMON200", description = "OK, 성공")})
@ApiResponses(value = {
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공")})
@Parameters({@Parameter(name = "userId", description = "로그인 한 유저 아이디"),
@Parameter(name = "peerAgeStart", description = "또래나이 시작 범위"),
@Parameter(name = "peerAgeEnd", description = "또래나이 끝 범위"),
@Parameter(name = "peerGender", description = "또래 성별")})
ResponseEntity<?> getAllConsumptionGoalCategories(Long userId, int peerAgeStart, int peerAgeEnd, String peerGender);
ApiResponse<List<AllConsumptionCategoryResponseDto>> getAllConsumptionGoalCategories(Long userId, int peerAgeStart,
int peerAgeEnd, String peerGender);

@Operation(summary = "또래나이와 성별 조회 API", description = "또래나이와 성별을 조회하는 API 입니다.")
@ApiResponses(value = {@ApiResponse(responseCode = "COMMON200", description = "OK, 성공")})
@ApiResponses(value = {
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공")})
@Parameters({@Parameter(name = "userId", description = "로그인 한 유저 아이디"),
@Parameter(name = "peerAgeStart", description = "또래나이 시작 범위"),
@Parameter(name = "peerAgeEnd", description = "또래나이 끝 범위"),
@Parameter(name = "peerGender", description = "또래 성별")})
ResponseEntity<?> getPeerInfo(Long userId, int peerAgeStart, int peerAgeEnd, String peerGender);
ApiResponse<PeerInfoResponseDto> getPeerInfo(Long userId, int peerAgeStart, int peerAgeEnd, String peerGender);

@Operation(summary = "소비 목표 조회 API", description = "date={yyyy-MM-dd} 형식의 query string을 통해서 사용자의 목표 달을 조회하는 API 입니다.")
@Parameters({@Parameter(name = "date", description = "yyyy-MM-dd 형식으로 목표 달의 소비를 조회")})
ResponseEntity<ConsumptionGoalResponseListDto> findUserConsumptionGoal(LocalDate date, Long userId);
ApiResponse<ConsumptionGoalResponseListDto> findUserConsumptionGoal(LocalDate date, Long userId);

@Operation(summary = "이번 달 소비 목표 수정 API", description = "다른 달의 소비 목표를 업데이트하는 것은 불가능하고 오직 이번 달의 소비 목표만 업데이트 하는 API 입니다.")
ResponseEntity<ConsumptionGoalResponseListDto> updateOrElseGenerateConsumptionGoal(Long userId,
ConsumptionGoalListRequestDto consumptionGoalListRequestDto);

@Operation(summary = "또래들이 가장 많이한 소비 카테고리 조회 Top3 API", description = "특정 사용자의 또래 소비 카테고리별 소비 건 수을 조회하는 API 입니다.")
@ApiResponses(value = {@ApiResponse(responseCode = "COMMON200", description = "OK, 성공")})
@ApiResponses(value = {
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공")})
@Parameters({@Parameter(name = "userId", description = "로그인 한 유저 아이디"),
@Parameter(name = "peerAgeStart", description = "또래나이 시작 범위"),
@Parameter(name = "peerAgeEnd", description = "또래나이 끝 범위"),
@Parameter(name = "peerGender", description = "또래 성별")})
ResponseEntity<?> getTopConsumptionCategories(Long userId, int peerAgeStart, int peerAgeEnd, String peerGender);
ApiResponse<List<TopCategoryConsumptionDto>> getTopConsumptionCategories(Long userId, int peerAgeStart,
int peerAgeEnd, String peerGender);

@Operation(summary = "또래들이 가장 많이한 소비 카테고리와 평균 금액 및 내 소비금액 차이 조회 API", description = "특정 사용자의 또래 소비 카테고리별 평균 소비 금액을 조회하는 API 입니다.")
@ApiResponses(value = {@ApiResponse(responseCode = "COMMON200", description = "OK, 성공")})
@ApiResponses(value = {
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공")})
@Parameters({@Parameter(name = "userId", description = "로그인 한 유저 아이디"),
@Parameter(name = "peerAgeStart", description = "또래나이 시작 범위"),
@Parameter(name = "peerAgeEnd", description = "또래나이 끝 범위"),
@Parameter(name = "peerGender", description = "또래 성별")})
ResponseEntity<?> getAllConsumptionCategories(Long userId, int peerAgeStart, int peerAgeEnd, String peerGender);
ApiResponse<List<AllConsumptionCategoryResponseDto>> getAllConsumptionCategories(Long userId, int peerAgeStart,
int peerAgeEnd, String peerGender);

@Operation(summary = "또래들이 가장 큰 목표로 세운 카테고리와 그 카테고리에서 이번주 사용한 금액 조회 API", description = "특정 사용자의 또래 소비 카테고리별 이번주 소비 금액을 조회하는 API 입니다.")
@ApiResponses(value = {@ApiResponse(responseCode = "COMMON200", description = "OK, 성공")})
ResponseEntity<?> getTopCategoryAndConsumptionAmount(@PathVariable Long userId);
@ApiResponses(value = {
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공")})
ApiResponse<ConsumptionAnalysisResponseDto> getTopCategoryAndConsumptionAmount(@PathVariable Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bbteam.budgetbuddies.apiPayload.ApiResponse;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.AllConsumptionCategoryResponseDto;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionAnalysisResponseDto;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalListRequestDto;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.ConsumptionGoalResponseListDto;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.PeerInfoResponseDto;
Expand All @@ -32,43 +34,45 @@ public class ConsumptionGoalController implements ConsumptionGoalApi {

@Override
@GetMapping("/top-categories/top-goal/4")
public ResponseEntity<?> getTopConsumptionGoalCategories(@RequestParam(name = "userId") Long userId,
public ApiResponse<List<TopGoalCategoryResponseDto>> getTopConsumptionGoalCategories(
@RequestParam(name = "userId") Long userId,
@RequestParam(name = "peerAgeStart", defaultValue = "0") int peerAgeStart,
@RequestParam(name = "peerAgeEnd", defaultValue = "0") int peerAgeEnd,
@RequestParam(name = "peerGender", defaultValue = "none") String peerGender) {
List<TopGoalCategoryResponseDto> topCategoriesList = consumptionGoalService.getTopConsumptionGoalCategories(
List<TopGoalCategoryResponseDto> response = consumptionGoalService.getTopConsumptionGoalCategories(
userId, peerAgeStart, peerAgeEnd, peerGender);
return ResponseEntity.ok(topCategoriesList);
return ApiResponse.onSuccess(response);
}

@GetMapping("/top-categories/top-goal")
public ResponseEntity<?> getAllConsumptionGoalCategories(@RequestParam(name = "userId") Long userId,
public ApiResponse<List<AllConsumptionCategoryResponseDto>> getAllConsumptionGoalCategories(
@RequestParam(name = "userId") Long userId,
@RequestParam(name = "peerAgeStart", defaultValue = "0") int peerAgeStart,
@RequestParam(name = "peerAgeEnd", defaultValue = "0") int peerAgeEnd,
@RequestParam(name = "peerGender", defaultValue = "none") String peerGender) {
List<AllConsumptionCategoryResponseDto> response = consumptionGoalService.getAllConsumptionGoalCategories(
userId,
peerAgeStart, peerAgeEnd, peerGender);
return ResponseEntity.ok(response);
return ApiResponse.onSuccess(response);
}

@Override
@GetMapping("/peer-info")
public ResponseEntity<?> getPeerInfo(@RequestParam(name = "userId") Long userId,
public ApiResponse<PeerInfoResponseDto> getPeerInfo(@RequestParam(name = "userId") Long userId,
@RequestParam(name = "peerAgeStart", defaultValue = "0") int peerAgeStart,
@RequestParam(name = "peerAgeEnd", defaultValue = "0") int peerAgeEnd,
@RequestParam(name = "peerGender", defaultValue = "none") String peerGender) {
PeerInfoResponseDto response = consumptionGoalService.getPeerInfo(userId, peerAgeStart, peerAgeEnd, peerGender);
return ResponseEntity.ok(response);
return ApiResponse.onSuccess(response);
}

@GetMapping("/{userId}")
public ResponseEntity<ConsumptionGoalResponseListDto> findUserConsumptionGoal(
public ApiResponse<ConsumptionGoalResponseListDto> findUserConsumptionGoal(
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date, @PathVariable Long userId) {

ConsumptionGoalResponseListDto response = consumptionGoalService.findUserConsumptionGoalList(userId, date);

return ResponseEntity.ok(response);
return ApiResponse.onSuccess(response);
}

@Override
Expand All @@ -81,28 +85,31 @@ public ResponseEntity<ConsumptionGoalResponseListDto> updateOrElseGenerateConsum
}

@GetMapping("/top-categories/top-consumption/3")
public ResponseEntity<?> getTopConsumptionCategories(@RequestParam(name = "userId") Long userId,
public ApiResponse<List<TopCategoryConsumptionDto>> getTopConsumptionCategories(
@RequestParam(name = "userId") Long userId,
@RequestParam(name = "peerAgeStart", defaultValue = "0") int peerAgeStart,
@RequestParam(name = "peerAgeEnd", defaultValue = "0") int peerAgeEnd,
@RequestParam(name = "peerGender", defaultValue = "none") String peerGender) {
List<TopCategoryConsumptionDto> topCategoriesList = consumptionGoalService.getTopConsumptionCategories(userId,
List<TopCategoryConsumptionDto> response = consumptionGoalService.getTopConsumptionCategories(userId,
peerAgeStart, peerAgeEnd, peerGender);
return ResponseEntity.ok(topCategoriesList);
return ApiResponse.onSuccess(response);
}

@GetMapping("/top-categories/top-consumption")
public ResponseEntity<?> getAllConsumptionCategories(@RequestParam(name = "userId") Long userId,
public ApiResponse<List<AllConsumptionCategoryResponseDto>> getAllConsumptionCategories(
@RequestParam(name = "userId") Long userId,
@RequestParam(name = "peerAgeStart", defaultValue = "0") int peerAgeStart,
@RequestParam(name = "peerAgeEnd", defaultValue = "0") int peerAgeEnd,
@RequestParam(name = "peerGender", defaultValue = "none") String peerGender) {
List<AllConsumptionCategoryResponseDto> response = consumptionGoalService.getAllConsumptionCategories(userId,
peerAgeStart, peerAgeEnd, peerGender);
return ResponseEntity.ok(response);
return ApiResponse.onSuccess(response);
}

@GetMapping("/top-categories/top-consumption/{userId}")
public ResponseEntity<?> getTopCategoryAndConsumptionAmount(@PathVariable Long userId) {
return ResponseEntity.ok(consumptionGoalService.getTopCategoryAndConsumptionAmount(userId));
public ApiResponse<ConsumptionAnalysisResponseDto> getTopCategoryAndConsumptionAmount(@PathVariable Long userId) {
ConsumptionAnalysisResponseDto response = consumptionGoalService.getTopCategoryAndConsumptionAmount(userId);
return ApiResponse.onSuccess(response);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ public ConsumptionGoalResponseDto toConsumptionGoalResponseDto(ConsumptionGoal c

public ConsumptionGoalResponseListDto toConsumptionGoalResponseListDto(
List<ConsumptionGoalResponseDto> consumptionGoalList, LocalDate goalMonth) {
Long totalGoalAmount = sumTotalGoalAmount(consumptionGoalList);
Long totalConsumptionAmount = sumTotalConsumptionAmount(consumptionGoalList);

return ConsumptionGoalResponseListDto.builder()
.goalMonth(goalMonth)
.totalGoalAmount(sumTotalGoalAmount(consumptionGoalList))
.totalConsumptionAmount(sumTotalConsumptionAmount(consumptionGoalList))
.totalGoalAmount(totalGoalAmount)
.totalConsumptionAmount(totalConsumptionAmount)
.totalRemainingBalance(totalGoalAmount - totalConsumptionAmount)
.consumptionGoalList(consumptionGoalList)
.build();

}

private Long sumTotalConsumptionAmount(List<ConsumptionGoalResponseDto> consumptionGoalList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ public class ConsumptionGoalResponseDto {
private Long categoryId;
private Long goalAmount;
private Long consumeAmount;
private Long remainingBalance;

@Builder
public ConsumptionGoalResponseDto(String categoryName, Long categoryId, Long goalAmount, Long consumeAmount) {
this.categoryName = categoryName;
this.categoryId = categoryId;
this.goalAmount = goalAmount;
this.consumeAmount = consumeAmount;
this.remainingBalance = goalAmount - consumeAmount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ public class ConsumptionGoalResponseListDto {
private LocalDate goalMonth;
private Long totalGoalAmount;
private Long totalConsumptionAmount;
private Long totalRemainingBalance;
private List<ConsumptionGoalResponseDto> consumptionGoalList;
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,12 @@ public class ConsumptionGoalServiceImpl implements ConsumptionGoalService {
private final UserRepository userRepository;

private final ConsumptionGoalConverter consumptionGoalConverter;

private int peerAgeStart;
private int peerAgeEnd;
private Gender peerGender;

private final LocalDate currentMonth = LocalDate.now().withDayOfMonth(1);
private final LocalDate startOfWeek = LocalDate.now().with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
private final LocalDate endOfWeek = LocalDate.now().with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
private int peerAgeStart;
private int peerAgeEnd;
private Gender peerGender;

@Override
@Transactional(readOnly = true)
Expand Down Expand Up @@ -405,7 +403,10 @@ public ConsumptionGoalResponseListDto findUserConsumptionGoalList(Long userId, L
updateGoalMapWithPreviousMonth(userId, goalMonth, goalMap);
updateGoalMapWithCurrentMonth(userId, goalMonth, goalMap);

return consumptionGoalConverter.toConsumptionGoalResponseListDto(new ArrayList<>(goalMap.values()), goalMonth);
List<ConsumptionGoalResponseDto> consumptionGoalList = new ArrayList<>(goalMap.values());

return consumptionGoalConverter.toConsumptionGoalResponseListDto(
orderByRemainingBalanceDescending(consumptionGoalList), goalMonth);
}

private Map<Long, ConsumptionGoalResponseDto> initializeGoalMap(Long userId) {
Expand All @@ -431,6 +432,13 @@ private void updateGoalMap(Long userId, LocalDate month, Map<Long, ConsumptionGo
.forEach(goal -> goalMap.put(goal.getCategoryId(), goal));
}

private List<ConsumptionGoalResponseDto> orderByRemainingBalanceDescending(
List<ConsumptionGoalResponseDto> consumptionGoalList) {
return consumptionGoalList.stream()
.sorted(Comparator.comparingLong(ConsumptionGoalResponseDto::getRemainingBalance).reversed())
.toList();
}

@Override
@Transactional
public void recalculateConsumptionAmount(Expense expense, ExpenseUpdateRequestDto request, User user) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ ResponseEntity<ExpenseResponseDto> createExpense(
@GetMapping("/{userId}/{expenseId}")
ResponseEntity<ExpenseResponseDto> findExpense(@Param("userId") Long userId, @Param("expenseId") Long expenseId);

@Operation(summary = "단일 소비 업데이트하기", description = "소비 아이디와 카테고리 아이디, amount(소비 금액)을 body에 담아서 소비를 업데이트")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "AUTH003", description = "access 토큰을 주세요!", content = @Content(schema = @Schema(implementation = ApiResponse.class))),
@ApiResponse(responseCode = "AUTH004", description = "access 토큰 만료", content = @Content(schema = @Schema(implementation = ApiResponse.class))),
@ApiResponse(responseCode = "AUTH006", description = "access 토큰 모양이 이상함", content = @Content(schema = @Schema(implementation = ApiResponse.class)))})
@GetMapping("/{userId}/{expenseId}")
@PostMapping("/{userId}")
ResponseEntity<ExpenseResponseDto> updateExpense(@PathVariable @Param("userId") Long userId, @RequestBody ExpenseUpdateRequestDto request);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.time.LocalDateTime;

import com.fasterxml.jackson.annotation.JsonFormat;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -16,5 +18,7 @@ public class CompactExpenseResponseDto {
private Long expenseId;
private String description;
private Long amount;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Seoul")
private LocalDateTime expenseDate;
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package com.bbteam.budgetbuddies.domain.expense.dto;

import java.time.LocalDateTime;

import com.fasterxml.jackson.annotation.JsonFormat;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ExpenseRequestDto {
private Long userId;
private Long categoryId;
private Long amount;
private String description;
private LocalDateTime expenseDate;
private Long userId;
private Long categoryId;
private Long amount;
private String description;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime expenseDate;
}
Loading

0 comments on commit 440b935

Please sign in to comment.