-
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.
Browse files
Browse the repository at this point in the history
[feat] 소비 내역 추가 API
- Loading branch information
Showing
12 changed files
with
178 additions
and
4 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
37 changes: 37 additions & 0 deletions
37
src/main/java/com/bbteam/budgetbuddies/domain/expense/controller/ExpenseController.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,37 @@ | ||
package com.bbteam.budgetbuddies.domain.expense.controller; | ||
|
||
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseRequestDto; | ||
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseResponseDto; | ||
import com.bbteam.budgetbuddies.domain.expense.service.ExpenseService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/expenses") | ||
public class ExpenseController { | ||
|
||
private final ExpenseService expenseService; | ||
|
||
@Operation(summary = "소비 내역 추가", description = "사용자가 소비 내역을 추가합니다.") | ||
@ApiResponses({ | ||
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"), | ||
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "AUTH003", description = "access 토큰을 주세요!", content = @Content(schema = @Schema(implementation = ApiResponse.class))), | ||
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "AUTH004", description = "access 토큰 만료", content = @Content(schema = @Schema(implementation = ApiResponse.class))), | ||
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "AUTH006", description = "access 토큰 모양이 이상함", content = @Content(schema = @Schema(implementation = ApiResponse.class))) | ||
}) | ||
@PostMapping("/add") | ||
public ResponseEntity<ExpenseResponseDto> createExpense( | ||
@Parameter(description = "user_id, category_id, amount, description, expenseDate") | ||
@RequestBody ExpenseRequestDto expenseRequestDto) { | ||
ExpenseResponseDto response = expenseService.createExpense(expenseRequestDto); | ||
return ResponseEntity.ok(response); | ||
} | ||
} |
1 change: 0 additions & 1 deletion
1
src/main/java/com/bbteam/budgetbuddies/domain/expense/controller/package-info.java
This file was deleted.
Oops, something went wrong.
34 changes: 34 additions & 0 deletions
34
src/main/java/com/bbteam/budgetbuddies/domain/expense/converter/ExpenseConverter.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,34 @@ | ||
package com.bbteam.budgetbuddies.domain.expense.converter; | ||
|
||
import com.bbteam.budgetbuddies.domain.category.entity.Category; | ||
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.user.entity.User; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class ExpenseConverter { | ||
|
||
public Expense toExpenseEntity(ExpenseRequestDto expenseRequestDto, User user, Category category) { | ||
return Expense.builder() | ||
.user(user) | ||
.category(category) | ||
.amount(expenseRequestDto.getAmount()) | ||
.description(expenseRequestDto.getDescription()) | ||
.expenseDate(expenseRequestDto.getExpenseDate()) | ||
.build(); | ||
} | ||
|
||
public ExpenseResponseDto toExpenseResponseDto(Expense expense) { | ||
return ExpenseResponseDto.builder() | ||
.expenseId(expense.getId()) | ||
.userId(expense.getUser().getId()) | ||
.categoryId(expense.getCategory().getId()) | ||
.amount(expense.getAmount()) | ||
.description(expense.getDescription()) | ||
.expenseDate(expense.getExpenseDate()) | ||
.build(); | ||
} | ||
} | ||
|
20 changes: 20 additions & 0 deletions
20
src/main/java/com/bbteam/budgetbuddies/domain/expense/dto/ExpenseRequestDto.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,20 @@ | ||
package com.bbteam.budgetbuddies.domain.expense.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class ExpenseRequestDto { | ||
private Long userId; | ||
private Long categoryId; | ||
private Long amount; | ||
private String description; | ||
private LocalDateTime expenseDate; | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/bbteam/budgetbuddies/domain/expense/dto/ExpenseResponseDto.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,21 @@ | ||
package com.bbteam.budgetbuddies.domain.expense.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class ExpenseResponseDto { | ||
private Long expenseId; | ||
private Long userId; | ||
private Long categoryId; | ||
private Long amount; | ||
private String description; | ||
private LocalDateTime expenseDate; | ||
} |
1 change: 0 additions & 1 deletion
1
src/main/java/com/bbteam/budgetbuddies/domain/expense/dto/package-info.java
This file was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
src/main/java/com/bbteam/budgetbuddies/domain/expense/repository/ExpenseRepository.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,18 @@ | ||
package com.bbteam.budgetbuddies.domain.expense.repository; | ||
|
||
import com.bbteam.budgetbuddies.domain.expense.entity.Expense; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
import java.util.List; | ||
|
||
public interface ExpenseRepository extends JpaRepository<Expense, Long> { | ||
|
||
// 추후 적용 예정 | ||
@Query("SELECT e FROM Expense e WHERE e.user.id = :userId AND e.category.id = :categoryId") | ||
List<Expense> findByUserIdAndCategoryId(@Param("userId") Long userId, @Param("categoryId") Long categoryId); | ||
|
||
@Query("SELECT e FROM Expense e WHERE e.user.id = :userId") | ||
List<Expense> findByUserId(@Param("userId") Long userId); | ||
} |
1 change: 0 additions & 1 deletion
1
src/main/java/com/bbteam/budgetbuddies/domain/expense/repository/package-info.java
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
src/main/java/com/bbteam/budgetbuddies/domain/expense/service/ExpenseService.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,11 @@ | ||
package com.bbteam.budgetbuddies.domain.expense.service; | ||
|
||
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); | ||
} |
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); | ||
} | ||
} |
1 change: 0 additions & 1 deletion
1
src/main/java/com/bbteam/budgetbuddies/domain/expense/service/package-info.java
This file was deleted.
Oops, something went wrong.