Skip to content

Commit

Permalink
Merge pull request #40 from BudgetBuddiesTeam/feat/#38
Browse files Browse the repository at this point in the history
[feat] 소비 내역 추가 API
  • Loading branch information
ryogaeng authored Jul 25, 2024
2 parents 2aa74e5 + b3b7fe4 commit 236e32a
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
@MappedSuperclass
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Getter
@SuperBuilder
@SoftDelete // boolean 타입의 deleted 필드가 추가
public abstract class BaseEntity {
Expand Down
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);
}
}

This file was deleted.

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();
}
}

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;
}
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;
}

This file was deleted.

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);
}

This file was deleted.

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);
}
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);
}
}

This file was deleted.

0 comments on commit 236e32a

Please sign in to comment.