Skip to content

Commit

Permalink
Merge pull request #64 from BudgetBuddiesTeam/feat/#63
Browse files Browse the repository at this point in the history
[feat] 소비 목표 업데이트 관련 로직 수정 및 구현
  • Loading branch information
ryogaeng authored Aug 4, 2024
2 parents 8d02854 + 3814a91 commit 20b8f84
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public interface CategoryRepository extends JpaRepository<Category, Long> {
@Query(value = "SELECT c FROM Category AS c WHERE c.isDefault=TRUE OR c.user.id=:id")
List<Category> findUserCategoryByUserId(@Param("id") Long id);

@Query("SELECT c FROM Category c WHERE c.isDefault = true")
List<Category> findAllByIsDefaultTrue();
boolean existsByUserIdAndName(Long userId, String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import com.bbteam.budgetbuddies.domain.category.dto.CategoryResponseDTO;
import com.bbteam.budgetbuddies.domain.category.entity.Category;
import com.bbteam.budgetbuddies.domain.category.repository.CategoryRepository;
import com.bbteam.budgetbuddies.domain.consumptiongoal.entity.ConsumptionGoal;
import com.bbteam.budgetbuddies.domain.consumptiongoal.repository.ConsumptionGoalRepository;
import com.bbteam.budgetbuddies.domain.user.entity.User;
import com.bbteam.budgetbuddies.domain.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

Expand All @@ -22,6 +25,7 @@ public class CategoryServiceImpl implements CategoryService {
private final CategoryRepository categoryRepository;
private final UserRepository userRepository;
private final CategoryConverter categoryConverter;
private final ConsumptionGoalRepository consumptionGoalRepository;

@Override
public CategoryResponseDTO createCategory(CategoryRequestDTO categoryRequestDTO) {
Expand All @@ -32,10 +36,20 @@ public CategoryResponseDTO createCategory(CategoryRequestDTO categoryRequestDTO)
throw new IllegalArgumentException("User already has a category with the same name");
}


Category category = categoryConverter.toCategoryEntity(categoryRequestDTO, user);
Category savedCategory = categoryRepository.save(category);

// custom 카테고리 생성 -> 소비 목표 테이블에 초기 값 추가
ConsumptionGoal consumptionGoal = ConsumptionGoal.builder()
.user(user)
.category(savedCategory)
.goalMonth(LocalDate.now().withDayOfMonth(1)) // custom 카테고리를 생성한 현재 달(지금)로 설정
.goalAmount(0L)
.consumeAmount(0L)
.build();

consumptionGoalRepository.save(consumptionGoal);

return categoryConverter.toCategoryResponseDTO(savedCategory);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.time.LocalDate;
import java.util.List;

import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.UserConsumptionGoalResponse;
import org.springframework.stereotype.Component;

import com.bbteam.budgetbuddies.domain.category.entity.Category;
Expand Down Expand Up @@ -48,4 +49,13 @@ private Long sumTotalConsumptionAmount(List<ConsumptionGoalResponseDto> consumpt
private Long sumTotalGoalAmount(List<ConsumptionGoalResponseDto> consumptionGoalList) {
return consumptionGoalList.stream().reduce(0L, (sum, c2) -> sum + c2.getGoalAmount(), Long::sum);
}

public UserConsumptionGoalResponse toUserConsumptionGoalResponse(ConsumptionGoal consumptionGoal) {
return UserConsumptionGoalResponse.builder()
.categoryId(consumptionGoal.getCategory().getId())
.goalMonth(consumptionGoal.getGoalMonth())
.consumeAmount(consumptionGoal.getConsumeAmount())
.goalAmount(consumptionGoal.getGoalAmount())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.bbteam.budgetbuddies.domain.consumptiongoal.dto;

import lombok.Builder;
import lombok.Getter;

import java.time.LocalDate;

@Getter
@Builder
public class UserConsumptionGoalResponse {
private Long categoryId;
private LocalDate goalMonth;
private Long consumeAmount;
private Long goalAmount;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.bbteam.budgetbuddies.domain.user.controller;

import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.UserConsumptionGoalResponse;
import com.bbteam.budgetbuddies.domain.user.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

private final UserService userService;

@PostMapping("/{userId}/add/default-categories/consumption-goal")
public ResponseEntity<List<UserConsumptionGoalResponse>> createConsumptionGoals(@PathVariable Long userId) {
List<UserConsumptionGoalResponse> consumptionGoals = userService.createConsumptionGoalWithDefaultGoals(userId);
return ResponseEntity.ok(consumptionGoals);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.bbteam.budgetbuddies.domain.user.service;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.UserConsumptionGoalResponse;

import java.util.List;

public interface UserService {
List<UserConsumptionGoalResponse> createConsumptionGoalWithDefaultGoals(Long userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.bbteam.budgetbuddies.domain.user.service;

import com.bbteam.budgetbuddies.domain.category.entity.Category;
import com.bbteam.budgetbuddies.domain.category.repository.CategoryRepository;
import com.bbteam.budgetbuddies.domain.consumptiongoal.converter.ConsumptionGoalConverter;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.UserConsumptionGoalResponse;
import com.bbteam.budgetbuddies.domain.consumptiongoal.entity.ConsumptionGoal;
import com.bbteam.budgetbuddies.domain.consumptiongoal.repository.ConsumptionGoalRepository;
import com.bbteam.budgetbuddies.domain.user.entity.User;
import com.bbteam.budgetbuddies.domain.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

private final UserRepository userRepository;
private final CategoryRepository categoryRepository;
private final ConsumptionGoalRepository consumptionGoalRepository;
private final ConsumptionGoalConverter consumptionGoalConverter;

@Override
@Transactional
public List<UserConsumptionGoalResponse> createConsumptionGoalWithDefaultGoals(Long userId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new IllegalArgumentException("User not found"));

List<Category> defaultCategories = categoryRepository.findAllByIsDefaultTrue();
List<ConsumptionGoal> consumptionGoals = defaultCategories.stream()
.map(category -> ConsumptionGoal.builder()
.user(user)
.category(category)
.goalMonth(LocalDate.now().withDayOfMonth(1))
.consumeAmount(0L)
.goalAmount(0L)
.build())
.collect(Collectors.toList());

List<ConsumptionGoal> savedConsumptionGoals = consumptionGoalRepository.saveAll(consumptionGoals);

return savedConsumptionGoals.stream()
.map(consumptionGoalConverter::toUserConsumptionGoalResponse)
.collect(Collectors.toList());
}
}

This file was deleted.

0 comments on commit 20b8f84

Please sign in to comment.