11package com .caesarjlee .caesarfinancialtracker .services ;
22
3- import com .caesarjlee .caesarfinancialtracker .dtos .records .*;
43import com .caesarjlee .caesarfinancialtracker .dtos .imports .ImportResponse ;
4+ import com .caesarjlee .caesarfinancialtracker .dtos .records .*;
55import com .caesarjlee .caesarfinancialtracker .entities .*;
66import com .caesarjlee .caesarfinancialtracker .enumerations .*;
77import com .caesarjlee .caesarfinancialtracker .exceptions .categories .CategoryNotFoundException ;
8- import com .caesarjlee .caesarfinancialtracker .exceptions .records .*;
98import com .caesarjlee .caesarfinancialtracker .exceptions .pages .PageSizeException ;
9+ import com .caesarjlee .caesarfinancialtracker .exceptions .records .*;
1010import com .caesarjlee .caesarfinancialtracker .repositories .*;
1111import com .caesarjlee .caesarfinancialtracker .utilities .*;
1212
1818import java .time .LocalDate ;
1919import java .util .List ;
2020import java .util .stream .Collectors ;
21-
2221import lombok .RequiredArgsConstructor ;
2322
2423@ Service
@@ -30,11 +29,11 @@ public class RecordService {
3029 private final ImportFiles importFiles ;
3130 private final ExportFiles exportFiles ;
3231
33- //helpers
32+ // helpers
3433 private RecordResponse toResponse (RecordEntity entity ) {
35- return new RecordResponse (entity .getId (), entity .getName (), entity .getType (), entity .getIcon (), entity . getDate (),
36- entity .getPrice (), entity .getDescription (), entity .getCreatedAt (),
37- entity .getUpdatedAt (), entity .getCategory ().getId ());
34+ return new RecordResponse (entity .getId (), entity .getName (), entity .getType (), entity .getIcon (),
35+ entity . getDate (), entity .getPrice (), entity .getDescription (), entity .getCreatedAt (),
36+ entity .getUpdatedAt (), entity .getCategory ().getId ());
3837 }
3938
4039 private RecordOrders validOrder (String order ) {
@@ -52,12 +51,12 @@ private Pageable validPage(String order, int page, int size) {
5251 }
5352
5453 private void validate (LocalDate start , LocalDate end , BigDecimal low , BigDecimal high ) {
55- //dates
54+ // dates
5655 if (end != null && end .isAfter (LocalDate .now ()))
5756 throw new InvalidRecordDateException (end + " must be <= " + LocalDate .now ());
5857 if (start != null && end != null && start .isAfter (end ))
5958 throw new InvalidRecordDateException (start + " must be <= " + end );
60- //prices
59+ // prices
6160 if (low != null && low .compareTo (BigDecimal .ZERO ) < 0 )
6261 throw new InvalidRecordPriceException (low + " must be >= 0" );
6362 if (high != null && high .compareTo (BigDecimal .ZERO ) < 0 )
@@ -66,15 +65,15 @@ private void validate(LocalDate start, LocalDate end, BigDecimal low, BigDecimal
6665 throw new InvalidRecordPriceException (low + " must be <= " + high );
6766 }
6867
69- private static String toKeyword (String keyword ){
68+ private static String toKeyword (String keyword ) {
7069 return keyword == null || keyword .isBlank () ? null : "%" + keyword .toLowerCase () + "%" ;
7170 }
7271
73- private static String toType (String type ){
72+ private static String toType (String type ) {
7473 return type == null || type .isBlank () || "all" .equalsIgnoreCase (type ) ? null : type .toLowerCase ();
7574 }
7675
77- //crud
76+ // crud
7877 public RecordResponse create (RecordRequest request ) {
7978 Long profileId = profileService .getCurrentProfile ().getId ();
8079 CategoryEntity category =
@@ -98,23 +97,26 @@ public Page<RecordResponse> read(String order, String type, String keyword, Long
9897 LocalDate dateEnd , BigDecimal priceLow , BigDecimal priceHigh , int page , int size ) {
9998 validate (dateStart , dateEnd , priceLow , priceHigh );
10099 return recordRepository
101- .search (profileService .getCurrentProfile ().getId (), toKeyword (keyword ), toType (type ), categoryId , dateStart , dateEnd , priceLow ,
102- priceHigh , validPage (order , page , size ))
100+ .search (profileService .getCurrentProfile ().getId (), toKeyword (keyword ), toType (type ), categoryId , dateStart ,
101+ dateEnd , priceLow , priceHigh , validPage (order , page , size ))
103102 .map (this ::toResponse );
104103 }
105104
106- public List <RecordResponse > readAll (String type , LocalDate dateStart , LocalDate dateEnd , BigDecimal priceLow , BigDecimal priceHigh , List <Long > categories , String keyword ){
105+ public List <RecordResponse > readAll (String type , LocalDate dateStart , LocalDate dateEnd , BigDecimal priceLow ,
106+ BigDecimal priceHigh , List <Long > categories , String keyword ) {
107107 validate (dateStart , dateEnd , priceLow , priceHigh );
108- boolean skipCategories = categories == null || categories .isEmpty ();
109- return recordRepository .searchAll (profileService .getCurrentProfile ().getId (), toType (type ), dateStart , dateEnd , priceLow , priceHigh , skipCategories , skipCategories ? List .of (-1L ) : categories , keyword )
108+ List <Long > categoryFilter = (categories == null || categories .isEmpty ()) ? null : categories ;
109+ return recordRepository
110+ .searchAll (profileService .getCurrentProfile ().getId (), toType (type ), dateStart , dateEnd , priceLow ,
111+ priceHigh , categoryFilter , keyword )
110112 .stream ()
111113 .map (this ::toResponse )
112114 .collect (Collectors .toList ());
113115 }
114116
115117 public RecordResponse update (Long id , RecordRequest request ) {
116118 RecordEntity entity = recordRepository .findByIdAndProfileId (id , profileService .getCurrentProfile ().getId ())
117- .orElseThrow (() -> new RecordNotFoundException (request .name ()));
119+ .orElseThrow (() -> new RecordNotFoundException (request .name ()));
118120 entity .setName (request .name ());
119121 entity .setType (request .type () == null ? entity .getType () : request .type ());
120122 entity .setIcon (request .icon () == null ? entity .getIcon () : request .icon ());
@@ -125,13 +127,13 @@ public RecordResponse update(Long id, RecordRequest request) {
125127 request .categoryId () == null
126128 ? entity .getCategory ()
127129 : categoryRepository .findById (request .categoryId ())
128- .orElseThrow (() -> new CategoryNotFoundException (Long .toString (request .categoryId ()))));
130+ .orElseThrow (() -> new CategoryNotFoundException (Long .toString (request .categoryId ()))));
129131 return toResponse (recordRepository .save (entity ));
130132 }
131133
132134 public void delete (Long id ) {
133135 recordRepository .delete (recordRepository .findByIdAndProfileId (id , profileService .getCurrentProfile ().getId ())
134- .orElseThrow (() -> new RecordNotFoundException (Long .toString (id ))));
136+ .orElseThrow (() -> new RecordNotFoundException (Long .toString (id ))));
135137 }
136138
137139 public ImportResponse importRecord (MultipartFile file ) {
0 commit comments