diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/category/repository/CategoryRepository.java b/src/main/java/com/bbteam/budgetbuddies/domain/category/repository/CategoryRepository.java index c6b571bb..fb64bd42 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/category/repository/CategoryRepository.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/category/repository/CategoryRepository.java @@ -12,5 +12,7 @@ public interface CategoryRepository extends JpaRepository { @Query(value = "SELECT c FROM Category AS c WHERE c.isDefault=TRUE OR c.user.id=:id") List findUserCategoryByUserId(@Param("id") Long id); + @Query("SELECT c FROM Category c WHERE c.isDefault = true") + List findAllByIsDefaultTrue(); boolean existsByUserIdAndName(Long userId, String name); } \ No newline at end of file diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/category/service/CategoryServiceImpl.java b/src/main/java/com/bbteam/budgetbuddies/domain/category/service/CategoryServiceImpl.java index 2df03410..23f37b74 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/category/service/CategoryServiceImpl.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/category/service/CategoryServiceImpl.java @@ -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; @@ -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) { @@ -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); } diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/ConsumptionGoalConverter.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/ConsumptionGoalConverter.java index cff4f4d3..2def3e93 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/ConsumptionGoalConverter.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/converter/ConsumptionGoalConverter.java @@ -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; @@ -48,4 +49,13 @@ private Long sumTotalConsumptionAmount(List consumpt private Long sumTotalGoalAmount(List 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(); + } } diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/UserConsumptionGoalResponse.java b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/UserConsumptionGoalResponse.java new file mode 100644 index 00000000..71aa00e4 --- /dev/null +++ b/src/main/java/com/bbteam/budgetbuddies/domain/consumptiongoal/dto/UserConsumptionGoalResponse.java @@ -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; +} \ No newline at end of file diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/user/controller/UserController.java b/src/main/java/com/bbteam/budgetbuddies/domain/user/controller/UserController.java new file mode 100644 index 00000000..2f517ecc --- /dev/null +++ b/src/main/java/com/bbteam/budgetbuddies/domain/user/controller/UserController.java @@ -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> createConsumptionGoals(@PathVariable Long userId) { + List consumptionGoals = userService.createConsumptionGoalWithDefaultGoals(userId); + return ResponseEntity.ok(consumptionGoals); + } +} diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/user/controller/package-info.java b/src/main/java/com/bbteam/budgetbuddies/domain/user/controller/package-info.java deleted file mode 100644 index f734f3e8..00000000 --- a/src/main/java/com/bbteam/budgetbuddies/domain/user/controller/package-info.java +++ /dev/null @@ -1 +0,0 @@ -package com.bbteam.budgetbuddies.domain.user.controller; \ No newline at end of file diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/user/service/UserService.java b/src/main/java/com/bbteam/budgetbuddies/domain/user/service/UserService.java new file mode 100644 index 00000000..c8da6a48 --- /dev/null +++ b/src/main/java/com/bbteam/budgetbuddies/domain/user/service/UserService.java @@ -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 createConsumptionGoalWithDefaultGoals(Long userId); +} diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/user/service/UserServiceImpl.java b/src/main/java/com/bbteam/budgetbuddies/domain/user/service/UserServiceImpl.java new file mode 100644 index 00000000..db6efe96 --- /dev/null +++ b/src/main/java/com/bbteam/budgetbuddies/domain/user/service/UserServiceImpl.java @@ -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 createConsumptionGoalWithDefaultGoals(Long userId) { + User user = userRepository.findById(userId) + .orElseThrow(() -> new IllegalArgumentException("User not found")); + + List defaultCategories = categoryRepository.findAllByIsDefaultTrue(); + List 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 savedConsumptionGoals = consumptionGoalRepository.saveAll(consumptionGoals); + + return savedConsumptionGoals.stream() + .map(consumptionGoalConverter::toUserConsumptionGoalResponse) + .collect(Collectors.toList()); + } +} diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/user/service/package-info.java b/src/main/java/com/bbteam/budgetbuddies/domain/user/service/package-info.java deleted file mode 100644 index ddb7edf6..00000000 --- a/src/main/java/com/bbteam/budgetbuddies/domain/user/service/package-info.java +++ /dev/null @@ -1 +0,0 @@ -package com.bbteam.budgetbuddies.domain.user.service; \ No newline at end of file