-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/com/bbteam/budgetbuddies/domain/expense/service/ExpenseServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |