Skip to content

Commit

Permalink
Merge pull request #144 from BudgetBuddiesTeam/dev
Browse files Browse the repository at this point in the history
[v7] 소비 및 카테고리 관련 로직 리팩토링 & 무중단 배포 구축
  • Loading branch information
ryogaeng authored Aug 19, 2024
2 parents 468e99d + d1aa222 commit d439fdb
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 34 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ jobs:
- name: Ensure resource directory exists
run: mkdir -p ./src/main/resources

- name : injection-yml
run : echo -E "${{ secrets.YML }}" > ./src/main/resources/application.yml
- name: injection-yml
run: echo -E "${{ secrets.YML }}" > ./src/main/resources/application.yml

- name: Log in to Docker Hub
uses: docker/login-action@v3
Expand All @@ -43,4 +43,4 @@ jobs:
key: ${{ secrets.PRIVATE_KEY }}
host: ${{ secrets.HOST_DEV }}
username: ${{ secrets.USERNAME }}
script: ${{ secrets.SCRIPT }}
script: ${{ secrets.DEPLOY_SCRIPT }}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public Category toCategoryEntity(CategoryRequestDTO categoryRequestDTO, User use
.name(categoryRequestDTO.getName())
.isDefault(categoryRequestDTO.getIsDefault())
.user(user)
.deleted(false)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public interface ExpenseApi {
@ApiResponse(responseCode = "AUTH004", description = "access 토큰 만료", content = @Content(schema = @Schema(implementation = ApiResponse.class))),
@ApiResponse(responseCode = "AUTH006", description = "access 토큰 모양이 이상함", content = @Content(schema = @Schema(implementation = ApiResponse.class)))})
ResponseEntity<ExpenseResponseDto> createExpense(
@Parameter(description = "user_id, category_id, amount, description, expenseDate") ExpenseRequestDto expenseRequestDto);
@Parameter(description = "user_id") @PathVariable Long userId,
@Parameter(description = "category_id, amount, description, expenseDate") @RequestBody ExpenseRequestDto expenseRequestDto
);

@Operation(summary = "월별 소비 조회", description = "무한 스크롤을 통한 조회로 예상하여 Slice를 통해서 조회")
@ApiResponses({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public class ExpenseController implements ExpenseApi {
private final ExpenseService expenseService;

@Override
@PostMapping("/add")
@PostMapping("/add/{userId}")
public ResponseEntity<ExpenseResponseDto> createExpense(
@Parameter(description = "user_id, category_id, amount, description, expenseDate")
@RequestBody ExpenseRequestDto expenseRequestDto) {
ExpenseResponseDto response = expenseService.createExpense(expenseRequestDto);
@Parameter(description = "user_id") @PathVariable Long userId,
@Parameter(description = "category_id, amount, description, expenseDate") @RequestBody ExpenseRequestDto expenseRequestDto) {
ExpenseResponseDto response = expenseService.createExpense(userId, expenseRequestDto);
return ResponseEntity.ok(response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
@NoArgsConstructor
@AllArgsConstructor
public class ExpenseRequestDto {
private Long userId;
private Long categoryId;
private Long amount;
private String description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.bbteam.budgetbuddies.domain.expense.dto.MonthlyExpenseCompactResponseDto;

public interface ExpenseService {
ExpenseResponseDto createExpense(ExpenseRequestDto expenseRequestDto);
ExpenseResponseDto createExpense(Long userId, ExpenseRequestDto expenseRequestDto);

MonthlyExpenseCompactResponseDto getMonthlyExpense(Long userId, LocalDate localDate);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public class ExpenseServiceImpl implements ExpenseService {
private final ConsumptionGoalService consumptionGoalService;

@Override
public ExpenseResponseDto createExpense(ExpenseRequestDto expenseRequestDto) {
User user = userRepository.findById(expenseRequestDto.getUserId())
public ExpenseResponseDto createExpense(Long userId, ExpenseRequestDto expenseRequestDto) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new IllegalArgumentException("Invalid user ID"));
Category category = categoryRepository.findById(expenseRequestDto.getCategoryId())
.orElseThrow(() -> new IllegalArgumentException("Invalid category ID"));
Expand All @@ -54,27 +54,7 @@ public ExpenseResponseDto createExpense(ExpenseRequestDto expenseRequestDto) {
Case 2)
!default && 키테고리 테이블의 UserId 컬럼의 값이 나와 맞으면 (= custom category)
*/
else if (!category.getIsDefault() && category.getUser().getId().equals(expenseRequestDto.getUserId())) {
// custom category
} else {
throw new IllegalArgumentException("User and category are not matched properly.");
}

/*
case 1)
- 카테고리 ID가 1~10 사이 && default => DB의 immutable 필드인 default category
- DB 관리 이슈로 category에 default 카테고리의 중복이 발생할 경우, 이를 대비하기 위해 1<= id <= 10 조건도 추가
*/
if (expenseRequestDto.getCategoryId() >= 1 && expenseRequestDto.getCategoryId() <= 10
&& category.getIsDefault()) {
// category.setUser(user);
// default category
}
/*
Case 2)
!default && 키테고리 테이블의 UserId 컬럼의 값이 나와 맞으면 (= custom cateogory)
*/
else if (!category.getIsDefault() && category.getUser().getId().equals(expenseRequestDto.getUserId())) {
else if (!category.getIsDefault() && category.getUser().getId().equals(userId)) {
// custom category
} else {
throw new IllegalArgumentException("User and category are not matched properly.");
Expand All @@ -84,7 +64,7 @@ else if (!category.getIsDefault() && category.getUser().getId().equals(expenseRe
expenseRepository.save(expense);

// 소비 목표 업데이트
consumptionGoalService.updateConsumeAmount(expenseRequestDto.getUserId(), expenseRequestDto.getCategoryId(),
consumptionGoalService.updateConsumeAmount(userId, expenseRequestDto.getCategoryId(),
expenseRequestDto.getAmount());

return expenseConverter.toExpenseResponseDto(expense);
Expand Down

0 comments on commit d439fdb

Please sign in to comment.