-
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.
* docs: 최근 설정 목표 금액 api 스웨거 문서 작성 * feat: 최근 목표 금액 조회 dto 작성 * feat: 최근 목표 금액 조회 controller 정의 * test: jpa named rule 최근 목표 금액 조회 메서드 단위 테스트 * test: jpa 메서드명 규칙 -> query dsl 메서드로 수정 * feat: user_id 기반 최근 목표 금액 데이터 조회 메서드 추가 * test: jpa query factory 의존성 주입 * test: 최근 목표 금액 없는 경우 테스트 * feat: 최근 목표 금액 조회 서비스 구현 * feat: 최근 목표 금액 조회 dto 변환 mapper 메서드 추가 * feat: 최근 목표 금액 조회 use case 작성
- Loading branch information
1 parent
07c6057
commit 079a61a
Showing
10 changed files
with
209 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
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
20 changes: 20 additions & 0 deletions
20
...src/main/java/kr/co/pennyway/api/apis/ledger/service/RecentTargetAmountSearchService.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 kr.co.pennyway.api.apis.ledger.service; | ||
|
||
import kr.co.pennyway.domain.domains.target.domain.TargetAmount; | ||
import kr.co.pennyway.domain.domains.target.service.TargetAmountService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class RecentTargetAmountSearchService { | ||
private final TargetAmountService targetAmountService; | ||
|
||
@Transactional(readOnly = true) | ||
public Integer readRecentTargetAmount(Long userId) { | ||
return targetAmountService.readRecentTargetAmount(userId) | ||
.map(TargetAmount::getAmount) | ||
.orElse(-1); | ||
} | ||
} |
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
5 changes: 5 additions & 0 deletions
5
...in/java/kr/co/pennyway/domain/domains/target/repository/TargetAmountCustomRepository.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,7 +1,12 @@ | ||
package kr.co.pennyway.domain.domains.target.repository; | ||
|
||
import kr.co.pennyway.domain.domains.target.domain.TargetAmount; | ||
|
||
import java.time.LocalDate; | ||
import java.util.Optional; | ||
|
||
public interface TargetAmountCustomRepository { | ||
Optional<TargetAmount> findRecentOneByUserId(Long userId); | ||
|
||
boolean existsByUserIdThatMonth(Long userId, LocalDate date); | ||
} |
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
110 changes: 110 additions & 0 deletions
110
...st/java/kr/co/pennyway/domain/domains/target/repository/RecentTargetAmountSearchTest.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,110 @@ | ||
package kr.co.pennyway.domain.domains.target.repository; | ||
|
||
import kr.co.pennyway.domain.config.ContainerMySqlTestConfig; | ||
import kr.co.pennyway.domain.config.JpaConfig; | ||
import kr.co.pennyway.domain.config.TestJpaConfig; | ||
import kr.co.pennyway.domain.domains.user.domain.User; | ||
import kr.co.pennyway.domain.domains.user.repository.UserRepository; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; | ||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; | ||
import org.springframework.jdbc.core.namedparam.SqlParameterSource; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@Slf4j | ||
@DataJpaTest(properties = {"spring.jpa.hibernate.ddl-auto=create"}) | ||
@ContextConfiguration(classes = JpaConfig.class) | ||
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) | ||
@ActiveProfiles("test") | ||
@Import(TestJpaConfig.class) | ||
public class RecentTargetAmountSearchTest extends ContainerMySqlTestConfig { | ||
private final Collection<MockTargetAmount> mockTargetAmounts = List.of( | ||
MockTargetAmount.of(10000, true, LocalDateTime.of(2021, 1, 1, 0, 0, 0), LocalDateTime.of(2021, 1, 1, 0, 0, 0)), | ||
MockTargetAmount.of(-1, false, LocalDateTime.of(2022, 3, 1, 0, 0, 0), LocalDateTime.of(2022, 3, 1, 0, 0, 0)), | ||
MockTargetAmount.of(20000, true, LocalDateTime.of(2022, 5, 1, 0, 0, 0), LocalDateTime.of(2022, 5, 1, 0, 0, 0)), | ||
MockTargetAmount.of(30000, true, LocalDateTime.of(2023, 7, 1, 0, 0, 0), LocalDateTime.of(2023, 7, 1, 0, 0, 0)), | ||
MockTargetAmount.of(-1, false, LocalDateTime.of(2024, 1, 1, 0, 0, 0), LocalDateTime.of(2024, 1, 1, 0, 0, 0)), | ||
MockTargetAmount.of(-1, true, LocalDateTime.of(2024, 2, 1, 0, 0, 0), LocalDateTime.of(2024, 2, 1, 0, 0, 0)) | ||
); | ||
private final Collection<MockTargetAmount> mockTargetAmountsMinus = List.of( | ||
MockTargetAmount.of(-1, true, LocalDateTime.of(2022, 3, 1, 0, 0, 0), LocalDateTime.of(2022, 3, 1, 0, 0, 0)), | ||
MockTargetAmount.of(-1, false, LocalDateTime.of(2024, 1, 1, 0, 0, 0), LocalDateTime.of(2024, 1, 1, 0, 0, 0)), | ||
MockTargetAmount.of(-1, false, LocalDateTime.of(2024, 1, 1, 0, 0, 0), LocalDateTime.of(2024, 1, 1, 0, 0, 0)), | ||
MockTargetAmount.of(-1, true, LocalDateTime.of(2024, 1, 1, 0, 0, 0), LocalDateTime.of(2024, 1, 1, 0, 0, 0)) | ||
); | ||
@Autowired | ||
private UserRepository userRepository; | ||
@Autowired | ||
private TargetAmountRepository targetAmountRepository; | ||
; | ||
@Autowired | ||
private NamedParameterJdbcTemplate jdbcTemplate; | ||
|
||
@Test | ||
@DisplayName("사용자의 가장 최근 목표 금액을 조회할 수 있다.") | ||
@Transactional | ||
public void 가장_최근_사용자_목표_금액_조회() { | ||
// given | ||
User user = userRepository.save(User.builder().username("jayang").name("Yang").phone("010-0000-0000").build()); | ||
bulkInsertTargetAmount(user, mockTargetAmounts); | ||
|
||
// when - then | ||
targetAmountRepository.findRecentOneByUserId(user.getId()) | ||
.ifPresentOrElse( | ||
targetAmount -> assertEquals(targetAmount.getAmount(), 30000), | ||
() -> Assertions.fail("최근 목표 금액이 존재하지 않습니다.") | ||
); | ||
} | ||
|
||
@Test | ||
@DisplayName("사용자의 가장 최근 목표 금액이 존재하지 않으면 Optional.empty()를 반환한다.") | ||
@Transactional | ||
public void 가장_최근_사용자_목표_금액_미존재() { | ||
// given | ||
User user = userRepository.save(User.builder().username("jayang").name("Yang").phone("010-0000-0000").build()); | ||
bulkInsertTargetAmount(user, mockTargetAmountsMinus); | ||
|
||
// when - then | ||
targetAmountRepository.findRecentOneByUserId(user.getId()) | ||
.ifPresentOrElse( | ||
targetAmount -> Assertions.fail("최근 목표 금액이 존재합니다."), | ||
() -> log.info("최근 목표 금액이 존재하지 않습니다.") | ||
); | ||
} | ||
|
||
private void bulkInsertTargetAmount(User user, Collection<MockTargetAmount> targetAmounts) { | ||
String sql = String.format(""" | ||
INSERT INTO `%s` (amount, is_read, user_id, created_at, updated_at) | ||
VALUES (:amount, true, :userId, :createdAt, :updatedAt) | ||
""", "target_amount"); | ||
SqlParameterSource[] params = targetAmounts.stream() | ||
.map(mockTargetAmount -> new MapSqlParameterSource() | ||
.addValue("amount", mockTargetAmount.amount) | ||
.addValue("userId", user.getId()) | ||
.addValue("createdAt", mockTargetAmount.createdAt) | ||
.addValue("updatedAt", mockTargetAmount.updatedAt)) | ||
.toArray(SqlParameterSource[]::new); | ||
jdbcTemplate.batchUpdate(sql, params); | ||
} | ||
|
||
private record MockTargetAmount(int amount, boolean isRead, LocalDateTime createdAt, LocalDateTime updatedAt) { | ||
public static MockTargetAmount of(int amount, boolean isRead, LocalDateTime createdAt, LocalDateTime updatedAt) { | ||
return new MockTargetAmount(amount, isRead, createdAt, updatedAt); | ||
} | ||
} | ||
} |