@@ -41,13 +41,7 @@ public FindByIdShoppingListRes findById(final Long id,
4141 final User user ,
4242 final FindByIdProductListReq request ,
4343 final Pageable pageable ) {
44- var findById = this .shoppingListRepository .findByIdAndUser (id , user );
45-
46- if (findById .isEmpty ()) {
47- throw new NotFoundException (getLocalMessage ().getMessage ("error.record-not-exist" ));
48- }
49-
50- final var shoppingList = findById .get ();
44+ final var shoppingList = getShoppingList (id , user );
5145 final var productListPage = findAllProductList (shoppingList , user , request , pageable );
5246 final var response = ShoppingListMapper .toFindByIdShoppingListRes (shoppingList );
5347 final var totalUnitsPerProducts = IntegerUtil .newAtomicReference (response .getTotalUnitsPerProducts ());
@@ -86,14 +80,7 @@ public FindByIdProductShoppingListRest findByIdProduct(Integer id, Long productI
8680 }
8781
8882 public void deleteProducts (Long id , DeleteProductsShoppingListReq request , User user ) {
89- var shoppingListOptional = this .shoppingListRepository .findByIdAndUser (id , user );
90-
91- if (shoppingListOptional .isEmpty ()) {
92- throw new NotFoundException (getLocalMessage ().getMessage ("error.record-not-exist" ));
93- }
94-
95- var shoppingList = shoppingListOptional .get ();
96-
83+ final var shoppingList = getShoppingList (id , user );
9784 var productsShoppingList = this .productHasShoppingListRepository .findAllByShoppingListIdAndUserAndProductIdIn (id ,
9885 user ,
9986 request .getProductsId ());
@@ -126,16 +113,10 @@ public SaveShoppingListRes save(SaveShoppingListReq request, User user) {
126113 }
127114
128115 public UpdateShoppingListRes update (Long id , UpdateShoppingListReq request , User user ) {
129- var shoppingListOptional = this .shoppingListRepository .findByIdAndUser (id , user );
130-
131- if (shoppingListOptional .isEmpty ()) {
132- throw new NotFoundException (getLocalMessage ().getMessage ("error.record-not-exist" ));
133- }
116+ final var shoppingList = getShoppingList (id , user );
134117
135118 updateProducts (id , request .getProducts (), user );
136119
137- var shoppingList = shoppingListOptional .get ();
138-
139120 shoppingList .setName (request .getName ());
140121
141122 var update = this .shoppingListRepository .save (shoppingList );
@@ -144,13 +125,7 @@ public UpdateShoppingListRes update(Long id, UpdateShoppingListReq request, User
144125 }
145126
146127 public void delete (Long id , User user ) {
147- var shoppingListOptional = this .shoppingListRepository .findByIdAndUser (id , user );
148-
149- if (shoppingListOptional .isEmpty ()) {
150- throw new NotFoundException (getLocalMessage ().getMessage ("error.record-not-exist" ));
151- }
152-
153- var shoppingList = shoppingListOptional .get ();
128+ final var shoppingList = getShoppingList (id , user );
154129
155130 this .productHasShoppingListRepository .deleteAll (shoppingList .getProductHasShoppingList ());
156131 this .shoppingListRepository .delete (shoppingList );
@@ -160,13 +135,9 @@ public FindByIdProductListRes findByIdProductList(final Long id,
160135 final User user ,
161136 final FindByIdProductListReq request ,
162137 final Pageable pageable ) {
163- var shoppingListOptional = this .shoppingListRepository .findByIdAndUser (id , user );
164-
165- if (shoppingListOptional .isEmpty ()) {
166- throw new NotFoundException (getLocalMessage ().getMessage ("error.record-not-exist" ));
167- }
138+ final var shoppingList = getShoppingList (id , user );
168139
169- return findAllProductList (shoppingListOptional . get () , user , request , pageable );
140+ return findAllProductList (shoppingList , user , request , pageable );
170141 }
171142
172143 private FindByIdProductListRes findAllProductList (final ShoppingList shoppingList ,
@@ -211,4 +182,14 @@ private void updateProducts(Long shoppingListId,
211182 });
212183 }
213184
185+ private ShoppingList getShoppingList (final Long id , final User user ) {
186+ final var shoppingListOptional = this .shoppingListRepository .findByIdAndUser (id , user );
187+
188+ if (shoppingListOptional .isEmpty ()) {
189+ throw new NotFoundException (getLocalMessage ().getMessage ("error.record-not-exist" ));
190+ }
191+
192+ return shoppingListOptional .get ();
193+ }
194+
214195}
0 commit comments