Skip to content

Commit

Permalink
[feat] ExpenseService, Impl 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ryogaeng committed Jul 25, 2024
1 parent 4c2ece0 commit b3b7fe4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseRequestDto;
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseResponseDto;
import com.bbteam.budgetbuddies.domain.category.dto.CategoryResponseDTO;

import java.util.List;

public interface ExpenseService {
ExpenseResponseDto createExpense(ExpenseRequestDto expenseRequestDto);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.bbteam.budgetbuddies.domain.expense.service;

import com.bbteam.budgetbuddies.domain.category.entity.Category;
import com.bbteam.budgetbuddies.domain.category.repository.CategoryRepository;
import com.bbteam.budgetbuddies.domain.expense.converter.ExpenseConverter;
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseRequestDto;
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseResponseDto;
import com.bbteam.budgetbuddies.domain.expense.entity.Expense;
import com.bbteam.budgetbuddies.domain.expense.repository.ExpenseRepository;
import com.bbteam.budgetbuddies.domain.user.entity.User;
import com.bbteam.budgetbuddies.domain.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ExpenseServiceImpl implements ExpenseService {

private final ExpenseRepository expenseRepository;
private final UserRepository userRepository;
private final CategoryRepository categoryRepository;
private final ExpenseConverter expenseConverter;

@Override
public ExpenseResponseDto createExpense(ExpenseRequestDto expenseRequestDto) {
User user = userRepository.findById(expenseRequestDto.getUserId())
.orElseThrow(() -> new IllegalArgumentException("Invalid user ID"));
Category category = categoryRepository.findById(expenseRequestDto.getCategoryId())
.orElseThrow(() -> new IllegalArgumentException("Invalid category ID"));

Expense expense = expenseConverter.toExpenseEntity(expenseRequestDto, user, category);
expenseRepository.save(expense);

return expenseConverter.toExpenseResponseDto(expense);
}
}

0 comments on commit b3b7fe4

Please sign in to comment.