-
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
- Loading branch information
Showing
14 changed files
with
544 additions
and
140 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
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
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
42 changes: 42 additions & 0 deletions
42
src/main/java/com/bbteam/budgetbuddies/domain/expense/controller/ExpenseApi.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,42 @@ | ||
package com.bbteam.budgetbuddies.domain.expense.controller; | ||
|
||
import java.time.LocalDate; | ||
|
||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseRequestDto; | ||
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseResponseDto; | ||
import com.bbteam.budgetbuddies.domain.expense.dto.MonthlyExpenseCompactResponseDto; | ||
|
||
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; | ||
|
||
public interface ExpenseApi { | ||
@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))), | ||
@ApiResponse(responseCode = "AUTH004", description = "access 토큰 만료", content = @Content(schema = @Schema(implementation = ApiResponse.class))), | ||
@ApiResponse(responseCode = "AUTH006", description = "access 토큰 모양이 이상함", content = @Content(schema = @Schema(implementation = ApiResponse.class))) | ||
}) | ||
ResponseEntity<ExpenseResponseDto> createExpense( | ||
@Parameter(description = "user_id, category_id, amount, description, expenseDate") | ||
ExpenseRequestDto expenseRequestDto); | ||
|
||
@Operation(summary = "월별 소비 조회", description = "무한 스크롤을 통한 조회로 예상하여 Slice를 통해서 조회") | ||
@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))), | ||
@ApiResponse(responseCode = "AUTH004", description = "access 토큰 만료", content = @Content(schema = @Schema(implementation = ApiResponse.class))), | ||
@ApiResponse(responseCode = "AUTH006", description = "access 토큰 모양이 이상함", content = @Content(schema = @Schema(implementation = ApiResponse.class))) | ||
}) | ||
ResponseEntity<MonthlyExpenseCompactResponseDto> findExpensesForMonth( | ||
Pageable pageable, | ||
Long userId, | ||
LocalDate date); | ||
} |
60 changes: 35 additions & 25 deletions
60
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 |
---|---|---|
@@ -1,37 +1,47 @@ | ||
package com.bbteam.budgetbuddies.domain.expense.controller; | ||
|
||
import java.time.LocalDate; | ||
|
||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.repository.query.Param; | ||
import org.springframework.format.annotation.DateTimeFormat; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseRequestDto; | ||
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseResponseDto; | ||
import com.bbteam.budgetbuddies.domain.expense.dto.MonthlyExpenseCompactResponseDto; | ||
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); | ||
} | ||
public class ExpenseController implements ExpenseApi { | ||
private final ExpenseService expenseService; | ||
|
||
@Override | ||
@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); | ||
} | ||
|
||
@Override | ||
@GetMapping("/{userId}") | ||
public ResponseEntity<MonthlyExpenseCompactResponseDto> findExpensesForMonth(Pageable pageable, | ||
@PathVariable @Param("userId") Long userId, | ||
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) { | ||
|
||
return ResponseEntity.ok(expenseService.getMonthlyExpense(pageable, userId, date)); | ||
} | ||
} |
72 changes: 51 additions & 21 deletions
72
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 |
---|---|---|
@@ -1,34 +1,64 @@ | ||
package com.bbteam.budgetbuddies.domain.expense.converter; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
import org.springframework.data.domain.Slice; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.bbteam.budgetbuddies.domain.category.entity.Category; | ||
import com.bbteam.budgetbuddies.domain.expense.dto.CompactExpenseResponseDto; | ||
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseRequestDto; | ||
import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseResponseDto; | ||
import com.bbteam.budgetbuddies.domain.expense.dto.MonthlyExpenseCompactResponseDto; | ||
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(); | ||
} | ||
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(); | ||
} | ||
|
||
public MonthlyExpenseCompactResponseDto toMonthlyExpenseCompactResponseDto(Slice<Expense> expenseSlice, | ||
LocalDate startOfMonth) { | ||
List<CompactExpenseResponseDto> compactResponseList = expenseSlice.getContent().stream() | ||
.map(this::toExpenseCompactResponseDto).toList(); | ||
|
||
return MonthlyExpenseCompactResponseDto | ||
.builder() | ||
.expenseMonth(startOfMonth) | ||
.currentPage(expenseSlice.getPageable().getPageNumber()) | ||
.hasNext(expenseSlice.hasNext()) | ||
.expenseList(compactResponseList) | ||
.build(); | ||
} | ||
|
||
private CompactExpenseResponseDto toExpenseCompactResponseDto(Expense expense) { | ||
return CompactExpenseResponseDto.builder() | ||
.expenseId(expense.getId()) | ||
.description(expense.getDescription()) | ||
.amount(expense.getAmount()) | ||
.expenseDate(expense.getExpenseDate()) | ||
.build(); | ||
} | ||
} | ||
|
Oops, something went wrong.