Skip to content

Commit 3244a5d

Browse files
change repository to fit railway database
1 parent 5eb27b1 commit 3244a5d

File tree

4 files changed

+115
-64
lines changed

4 files changed

+115
-64
lines changed

backend/src/main/java/com/caesarjlee/caesarfinancialtracker/repositories/CategoryRepository.java

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,53 @@
99
import java.util.*;
1010

1111
public interface CategoryRepository extends JpaRepository<CategoryEntity, Long> {
12-
Optional<CategoryEntity> findByIdAndProfileId(Long id, Long profileId);
13-
boolean existsByNameAndTypeAndProfileId(String name, String type, Long profileId);
14-
List<CategoryEntity> findByProfileId(Long profileId);
15-
Page<CategoryEntity> findByProfileId(Long profileId, Pageable pageable);
16-
Page<CategoryEntity> findByTypeAndProfileId(String type, Long profileId, Pageable pageable);
12+
/*
13+
SELECT * FROM cft_categories
14+
WHERE id = ? AND profile_id = ?
15+
*/
16+
Optional <CategoryEntity> findByIdAndProfileId(Long id, Long profileId);
17+
/*
18+
SELECT EXISTS(
19+
SELECT 1 FROM cft_categories
20+
WHERE name = ? AND type = ? AND profile_id = ?
21+
)
22+
*/
23+
boolean existsByNameAndTypeAndProfileId(String name, String type, Long profileId);
24+
/*
25+
SELECT * FROM cft_categories
26+
WHERE profile_id = ?
27+
*/
28+
List <CategoryEntity> findByProfileId(Long profileId);
29+
/*
30+
SELECT * FROM cft_categories
31+
WHERE profile_id = ?
32+
ORDER BY ? ASC/DESC
33+
LIMIT ? OFFSET ?
34+
*/
35+
Page <CategoryEntity> findByProfileId(Long profileId, Pageable pageable);
36+
/*
37+
SELECT * FROM cft_categories
38+
WHERE type = ? AND profile_id = ?
39+
ORDER BY ? ASC/DESC
40+
LIMIT ? OFFSET ?
41+
*/
42+
Page <CategoryEntity> findByTypeAndProfileId(String type, Long profileId, Pageable pageable);
43+
/*
44+
SELECT * FROM cft_categories
45+
WHERE profile_id = ?
46+
AND (? IS NULL OR type = ?)
47+
AND (? IS NULL OR LOWER(name) LIKE LOWER(CONCAT('%', ?, '%')))
48+
ORDER BY ? ASC/DESC
49+
LIMIT ? OFFSET ?
50+
*/
1751
@Query(
18-
value = """
19-
SELECT * FROM cft_categories c
20-
WHERE c.profile_id = :profileId
21-
AND (:type IS NULL OR LOWER(c.type) = :type)
22-
AND (:keyword IS NULL OR LOWER(c.name) LIKE :keyword)
23-
""",
24-
countQuery = """
25-
SELECT COUNT(*) FROM cft_categories c
26-
WHERE c.profile_id = :profileId
27-
AND (:type IS NULL OR LOWER(c.type) = :type)
28-
AND (:keyword IS NULL OR LOWER(c.name) LIKE :keyword)
29-
""",
30-
nativeQuery = true
52+
"""
53+
SELECT c FROM CategoryEntity c
54+
WHERE c.profile.id = :profileId
55+
AND (:type IS NULL OR c.type = :type)
56+
AND (:name IS NULL OR LOWER(c.name) LIKE LOWER(CONCAT('%', :name, '%')))
57+
"""
3158
)
32-
Page<CategoryEntity> search(@Param("profileId") Long profileId, @Param("type") String type,
33-
@Param("keyword") String keyword, Pageable pageable);
59+
Page <CategoryEntity> search(@Param("profileId") Long profileId, @Param("type") String type,
60+
@Param("keyword") String keyword, Pageable pageable);
3461
}

backend/src/main/java/com/caesarjlee/caesarfinancialtracker/repositories/ProfileRepository.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@
77
import java.util.Optional;
88

99
public interface ProfileRepository extends JpaRepository<ProfileEntity, Long> {
10-
//SELECT * FROM cft_profiles WHERE email = ?
10+
/*
11+
SELECT * FROM cft_profiles
12+
WHERE email = ?
13+
*/
1114
Optional<ProfileEntity> findByEmail(String email);
12-
13-
//SELECT EXISTS(SELECT 1 FROM cft_profiles p WHERE p.email = ?)
15+
/*
16+
SELECT EXISTS(
17+
SELECT 1 FROM cft_profiles
18+
WHERE email = ?
19+
)
20+
*/
1421
boolean existsByEmail(String email);
1522
}

backend/src/main/java/com/caesarjlee/caesarfinancialtracker/repositories/RecordRepository.java

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,42 @@
1111
import org.springframework.data.repository.query.Param;
1212

1313
public interface RecordRepository extends JpaRepository <RecordEntity, Long> {
14+
/*
15+
SELECT * FROM cft_records
16+
WHERE id = ? AND profile_id = ?
17+
*/
1418
Optional <RecordEntity> findByIdAndProfileId(Long id, Long profileId);
19+
/*
20+
SELECT * FROM cft_records
21+
WHERE profile_id = ?
22+
*/
1523
List <RecordEntity> findByProfileId(Long profileId);
16-
@Query(
17-
value = """
18-
SELECT r.* FROM cft_records r
19-
JOIN cft_categories c ON c.id = r.category_id
20-
WHERE r.profile_id = :profileId
21-
AND (:keyword IS NULL OR LOWER(r.name) LIKE :keyword)
22-
AND (:type IS NULL OR LOWER(c.type) = :type)
23-
AND (CAST(:categoryId AS BIGINT) IS NULL OR r.category_id = :categoryId)
24-
AND (CAST(:dateStart AS DATE) IS NULL OR r.date >= :dateStart)
25-
AND (CAST(:dateEnd AS DATE) IS NULL OR r.date <= :dateEnd)
26-
AND (CAST(:priceLow AS NUMERIC) IS NULL OR r.price >= :priceLow)
27-
AND (CAST(:priceHigh AS NUMERIC) IS NULL OR r.price <= :priceHigh)
28-
""",
29-
countQuery = """
30-
SELECT COUNT(*) FROM cft_records r
24+
/*
25+
SELECT r.* FROM cft_records r
3126
JOIN cft_categories c ON c.id = r.category_id
32-
WHERE r.profile_id = :profileId
33-
AND (:keyword IS NULL OR LOWER(r.name) LIKE :keyword)
34-
AND (:type IS NULL OR LOWER(c.type) = :type)
35-
AND (CAST(:categoryId AS BIGINT) IS NULL OR r.category_id = :categoryId)
36-
AND (CAST(:dateStart AS DATE) IS NULL OR r.date >= :dateStart)
37-
AND (CAST(:dateEnd AS DATE) IS NULL OR r.date <= :dateEnd)
38-
AND (CAST(:priceLow AS NUMERIC) IS NULL OR r.price >= :priceLow)
39-
AND (CAST(:priceHigh AS NUMERIC) IS NULL OR r.price <= :priceHigh)
40-
""",
41-
nativeQuery = true
27+
WHERE r.profile_id = ?
28+
AND (? IS NULL OR LOWER(r.name) LIKE LOWER(CONCAT('%', ?, '%')))
29+
AND (? IS NULL OR c.type = ?)
30+
AND (? IS NULL OR c.category_id = ?)
31+
AND (? IS NULL OR r.date >= ?)
32+
AND (? IS NULL OR r.date <= ?)
33+
AND (? IS NULL OR r.price >= ?)
34+
AND (? IS NULL OR r.price <= ?)
35+
ORDER BY ? ASC/DESC
36+
LIMIT ? OFFSET ?
37+
*/
38+
@Query(
39+
"""
40+
SELECT r FROM RecordEntity r
41+
WHERE r.profile.id = :profileId
42+
AND (:name IS NULL OR LOWER(r.name) LIKE LOWER(CONCAT('%', :name, '%')))
43+
AND (:type IS NULL OR r.category.type = :type)
44+
AND (:categoryId IS NULL OR r.category.id = :categoryId)
45+
AND (:dateStart IS NULL OR r.date >= :dateStart)
46+
AND (:dateEnd IS NULL OR r.date <= :dateEnd)
47+
AND (:priceLow IS NULL OR r.price >= :priceLow)
48+
AND (:priceHigh IS NULL OR r.price <= :priceHigh)
49+
"""
4250
)
4351
Page<RecordEntity> search(@Param("profileId") Long profileId,
4452
@Param("keyword") String keyword,
@@ -49,20 +57,31 @@ Page<RecordEntity> search(@Param("profileId") Long profileId,
4957
@Param("priceLow") BigDecimal priceLow,
5058
@Param("priceHigh") BigDecimal priceHigh,
5159
Pageable pageable);
52-
@Query(
53-
value = """
54-
SELECT r.* FROM cft_records r
60+
/*
61+
SELECT r.* FROM cft_records r
5562
JOIN cft_categories c ON c.id = r.category_id
56-
WHERE r.profile_id = :profileId
57-
AND (:type IS NULL OR LOWER(c.type) = :type)
58-
AND (CAST(:dateStart AS DATE) IS NULL OR r.date >= :dateStart)
59-
AND (CAST(:dateEnd AS DATE) IS NULL OR r.date <= :dateEnd)
60-
AND (CAST(:priceLow AS NUMERIC) IS NULL OR r.price >= :priceLow)
61-
AND (CAST(:priceHigh AS NUMERIC) IS NULL OR r.price <= :priceHigh)
62-
AND (:skipCategories = true OR r.category_id IN (:categories))
63-
AND (:keyword IS NULL OR LOWER(r.name) LIKE :keyword)
64-
""",
65-
nativeQuery = true
63+
WHERE r.profile_id = ?
64+
AND (? IS NULL OR c.type = ?)
65+
AND (? IS NULL OR r.date >= ?)
66+
AND (? IS NULL OR r.date <= ?)
67+
AND (? IS NULL OR r.price >= ?)
68+
AND (? IS NULL OR r.price <= ?)
69+
AND (? IS NULL OR r.category_id IN (?, ...))
70+
AND (? IS NULL OR LOWER(r.name) LIKE LOWER(CONCAT('%', ?, '%')))
71+
*/
72+
@Query(
73+
"""
74+
SELECT r FROM RecordEntity r
75+
WHERE r.profile.id = :profileId
76+
AND (:type IS NULL OR r.category.type = :type)
77+
AND (:dateStart IS NULL OR r.date >= :dateStart)
78+
AND (:dateEnd IS NULL OR r.date <= :dateEnd)
79+
AND (:priceLow IS NULL OR r.price >= :priceLow)
80+
AND (:priceHigh IS NULL OR r.price <= :priceHigh)
81+
AND (:#{#categories == null || #categories.isEmpty()} = true
82+
OR r.category.id IN :categories)
83+
AND (:name IS NULL OR LOWER(r.name) LIKE LOWER(CONCAT('%', :name, '%')))
84+
"""
6685
)
6786
List <RecordEntity> searchAll(@Param("profileId") Long profileId,
6887
@Param("type") String type,

backend/src/main/resources/application.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
spring:
2-
profiles:
3-
active: ${SPRING_PROFILES_ACTIVE:local} #render injects this, and load local configurations 4 development
42
jackson:
53
property-naming-strategy: SNAKE_CASE
64
datasource:
7-
driver-class-name: ${SPRING_DATASOURCE_DRIVER_CLASS_NAME}
5+
driver-class-name: com.mysql.cj.jdbc.Driver
86
url: ${SPRING_DATASOURCE_URL}
97
username: ${SPRING_DATASOURCE_USERNAME}
108
password: ${SPRING_DATASOURCE_PASSWORD}

0 commit comments

Comments
 (0)