From d2211b527ce41fc2a6dec9da8e74b3c6edac11b3 Mon Sep 17 00:00:00 2001 From: ryogaeng Date: Thu, 25 Jul 2024 18:36:04 +0900 Subject: [PATCH 1/8] =?UTF-8?q?[refactor]=20BaseEntity=EC=97=90=20@Getter?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/bbteam/budgetbuddies/common/BaseEntity.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/bbteam/budgetbuddies/common/BaseEntity.java b/src/main/java/com/bbteam/budgetbuddies/common/BaseEntity.java index b66eef77..912b0a22 100644 --- a/src/main/java/com/bbteam/budgetbuddies/common/BaseEntity.java +++ b/src/main/java/com/bbteam/budgetbuddies/common/BaseEntity.java @@ -19,6 +19,7 @@ @MappedSuperclass @NoArgsConstructor(access = AccessLevel.PROTECTED) @AllArgsConstructor +@Getter @SuperBuilder @SoftDelete // boolean 타입의 deleted 필드가 추가 public abstract class BaseEntity { From 71fe910b47ef1f27f0bf2978f2bf708dc0a291d6 Mon Sep 17 00:00:00 2001 From: ryogaeng Date: Thu, 25 Jul 2024 18:36:21 +0900 Subject: [PATCH 2/8] =?UTF-8?q?[feat]=20Dto=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/expense/dto/ExpenseRequestDto.java | 20 ++++++++++++++++++ .../expense/dto/ExpenseResponseDto.java | 21 +++++++++++++++++++ .../domain/expense/dto/package-info.java | 1 - 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/expense/dto/ExpenseRequestDto.java create mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/expense/dto/ExpenseResponseDto.java delete mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/expense/dto/package-info.java diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/expense/dto/ExpenseRequestDto.java b/src/main/java/com/bbteam/budgetbuddies/domain/expense/dto/ExpenseRequestDto.java new file mode 100644 index 00000000..680be344 --- /dev/null +++ b/src/main/java/com/bbteam/budgetbuddies/domain/expense/dto/ExpenseRequestDto.java @@ -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; +} \ No newline at end of file diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/expense/dto/ExpenseResponseDto.java b/src/main/java/com/bbteam/budgetbuddies/domain/expense/dto/ExpenseResponseDto.java new file mode 100644 index 00000000..79b74cf6 --- /dev/null +++ b/src/main/java/com/bbteam/budgetbuddies/domain/expense/dto/ExpenseResponseDto.java @@ -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; +} diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/expense/dto/package-info.java b/src/main/java/com/bbteam/budgetbuddies/domain/expense/dto/package-info.java deleted file mode 100644 index cd54ca0d..00000000 --- a/src/main/java/com/bbteam/budgetbuddies/domain/expense/dto/package-info.java +++ /dev/null @@ -1 +0,0 @@ -package com.bbteam.budgetbuddies.domain.expense.dto; \ No newline at end of file From 0cbb506a57822936fc6922bb143d2ebe6aa9c129 Mon Sep 17 00:00:00 2001 From: ryogaeng Date: Thu, 25 Jul 2024 18:37:03 +0900 Subject: [PATCH 3/8] =?UTF-8?q?[feat]=20ExpenseService=20=EC=9D=B8?= =?UTF-8?q?=ED=84=B0=ED=8E=98=EC=9D=B4=EC=8A=A4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/expense/service/ExpenseService.java | 8 ++++++++ .../domain/expense/service/package-info.java | 1 - 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/expense/service/ExpenseService.java delete mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/expense/service/package-info.java diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/expense/service/ExpenseService.java b/src/main/java/com/bbteam/budgetbuddies/domain/expense/service/ExpenseService.java new file mode 100644 index 00000000..7eaacf94 --- /dev/null +++ b/src/main/java/com/bbteam/budgetbuddies/domain/expense/service/ExpenseService.java @@ -0,0 +1,8 @@ +package com.bbteam.budgetbuddies.domain.expense.service; + +import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseRequestDto; +import com.bbteam.budgetbuddies.domain.expense.dto.ExpenseResponseDto; + +public interface ExpenseService { + ExpenseResponseDto createExpense(ExpenseRequestDto expenseRequestDto); +} diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/expense/service/package-info.java b/src/main/java/com/bbteam/budgetbuddies/domain/expense/service/package-info.java deleted file mode 100644 index ece79aae..00000000 --- a/src/main/java/com/bbteam/budgetbuddies/domain/expense/service/package-info.java +++ /dev/null @@ -1 +0,0 @@ -package com.bbteam.budgetbuddies.domain.expense.service; \ No newline at end of file From a25036752511af21be33ce1c2155e721621433e5 Mon Sep 17 00:00:00 2001 From: ryogaeng Date: Thu, 25 Jul 2024 19:20:35 +0900 Subject: [PATCH 4/8] =?UTF-8?q?[feat]=20ExpenseRepository=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../expense/repository/ExpenseRepository.java | 16 ++++++++++++++++ .../domain/expense/repository/package-info.java | 1 - 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/expense/repository/ExpenseRepository.java delete mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/expense/repository/package-info.java diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/expense/repository/ExpenseRepository.java b/src/main/java/com/bbteam/budgetbuddies/domain/expense/repository/ExpenseRepository.java new file mode 100644 index 00000000..9799aaaa --- /dev/null +++ b/src/main/java/com/bbteam/budgetbuddies/domain/expense/repository/ExpenseRepository.java @@ -0,0 +1,16 @@ +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 { + @Query("SELECT e FROM Expense e WHERE e.user.id = :userId AND e.category.id = :categoryId") + List findByUserIdAndCategoryId(@Param("userId") Long userId, @Param("categoryId") Long categoryId); + + @Query("SELECT e FROM Expense e WHERE e.user.id = :userId") + List findByUserId(@Param("userId") Long userId); +} diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/expense/repository/package-info.java b/src/main/java/com/bbteam/budgetbuddies/domain/expense/repository/package-info.java deleted file mode 100644 index c5184ef7..00000000 --- a/src/main/java/com/bbteam/budgetbuddies/domain/expense/repository/package-info.java +++ /dev/null @@ -1 +0,0 @@ -package com.bbteam.budgetbuddies.domain.expense.repository; \ No newline at end of file From ac9973714c99903a2051a02d298167ce5143ce3f Mon Sep 17 00:00:00 2001 From: ryogaeng Date: Thu, 25 Jul 2024 19:21:15 +0900 Subject: [PATCH 5/8] =?UTF-8?q?[feat]=20ExpenseConverter=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../expense/converter/ExpenseConverter.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/expense/converter/ExpenseConverter.java diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/expense/converter/ExpenseConverter.java b/src/main/java/com/bbteam/budgetbuddies/domain/expense/converter/ExpenseConverter.java new file mode 100644 index 00000000..ac88217c --- /dev/null +++ b/src/main/java/com/bbteam/budgetbuddies/domain/expense/converter/ExpenseConverter.java @@ -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(); + } +} + From a735f2c79f204f90a5147eb5e54e9127c8ea7274 Mon Sep 17 00:00:00 2001 From: ryogaeng Date: Thu, 25 Jul 2024 19:23:18 +0900 Subject: [PATCH 6/8] =?UTF-8?q?[docs]=20ExpenseRepository=20=EC=A3=BC?= =?UTF-8?q?=EC=84=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/expense/repository/ExpenseRepository.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/expense/repository/ExpenseRepository.java b/src/main/java/com/bbteam/budgetbuddies/domain/expense/repository/ExpenseRepository.java index 9799aaaa..c846f92e 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/expense/repository/ExpenseRepository.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/expense/repository/ExpenseRepository.java @@ -8,6 +8,8 @@ import java.util.List; public interface ExpenseRepository extends JpaRepository { + + // 추후 적용 예정 @Query("SELECT e FROM Expense e WHERE e.user.id = :userId AND e.category.id = :categoryId") List findByUserIdAndCategoryId(@Param("userId") Long userId, @Param("categoryId") Long categoryId); From 4c2ece0b78a971f33228cbf286754f18fb6546b0 Mon Sep 17 00:00:00 2001 From: ryogaeng Date: Thu, 25 Jul 2024 19:27:15 +0900 Subject: [PATCH 7/8] =?UTF-8?q?[feat]=20ExpenseController=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../expense/controller/ExpenseController.java | 37 +++++++++++++++++++ .../expense/controller/package-info.java | 1 - 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/expense/controller/ExpenseController.java delete mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/expense/controller/package-info.java diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/expense/controller/ExpenseController.java b/src/main/java/com/bbteam/budgetbuddies/domain/expense/controller/ExpenseController.java new file mode 100644 index 00000000..159c6fc9 --- /dev/null +++ b/src/main/java/com/bbteam/budgetbuddies/domain/expense/controller/ExpenseController.java @@ -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 createExpense( + @Parameter(description = "user_id, category_id, amount, description, expenseDate") + @RequestBody ExpenseRequestDto expenseRequestDto) { + ExpenseResponseDto response = expenseService.createExpense(expenseRequestDto); + return ResponseEntity.ok(response); + } +} diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/expense/controller/package-info.java b/src/main/java/com/bbteam/budgetbuddies/domain/expense/controller/package-info.java deleted file mode 100644 index cd648f5a..00000000 --- a/src/main/java/com/bbteam/budgetbuddies/domain/expense/controller/package-info.java +++ /dev/null @@ -1 +0,0 @@ -package com.bbteam.budgetbuddies.domain.expense.controller; \ No newline at end of file From b3b7fe499c68a18b7c641d6e312b29f61a29f6b1 Mon Sep 17 00:00:00 2001 From: ryogaeng Date: Thu, 25 Jul 2024 19:27:41 +0900 Subject: [PATCH 8/8] =?UTF-8?q?[feat]=20ExpenseService,=20Impl=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../expense/service/ExpenseService.java | 3 ++ .../expense/service/ExpenseServiceImpl.java | 36 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/expense/service/ExpenseServiceImpl.java diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/expense/service/ExpenseService.java b/src/main/java/com/bbteam/budgetbuddies/domain/expense/service/ExpenseService.java index 7eaacf94..cbcddb72 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/expense/service/ExpenseService.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/expense/service/ExpenseService.java @@ -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); diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/expense/service/ExpenseServiceImpl.java b/src/main/java/com/bbteam/budgetbuddies/domain/expense/service/ExpenseServiceImpl.java new file mode 100644 index 00000000..b4675ebe --- /dev/null +++ b/src/main/java/com/bbteam/budgetbuddies/domain/expense/service/ExpenseServiceImpl.java @@ -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); + } +}