11package my .bookshop .handlers ;
22
33import static cds .gen .adminservice .AdminService_ .ORDERS ;
4- import static cds .gen .adminservice .AdminService_ .ORDER_ITEMS ;
54import static cds .gen .my .bookshop .Bookshop_ .BOOKS ;
65
7- import java .io .BufferedReader ;
8- import java .io .IOException ;
9- import java .io .InputStream ;
10- import java .io .InputStreamReader ;
11- import java .math .BigDecimal ;
12- import java .util .ArrayList ;
13- import java .util .Arrays ;
14- import java .util .List ;
15- import java .util .UUID ;
16- import java .util .function .Supplier ;
17- import java .util .stream .Stream ;
18- import org .springframework .stereotype .Component ;
19- import com .sap .cds .Result ;
6+ import cds .gen .adminservice .AdminService ;
7+ import cds .gen .adminservice .AdminService_ ;
8+ import cds .gen .adminservice .Books ;
9+ import cds .gen .adminservice .BooksAddToOrderContext ;
10+ import cds .gen .adminservice .BooksCovers ;
11+ import cds .gen .adminservice .Books_ ;
12+ import cds .gen .adminservice .OrderItems ;
13+ import cds .gen .adminservice .OrderItems_ ;
14+ import cds .gen .adminservice .Orders ;
15+ import cds .gen .adminservice .Upload ;
16+ import cds .gen .adminservice .Upload_ ;
17+ import cds .gen .my .bookshop .Bookshop_ ;
2018import com .sap .cds .ql .Select ;
2119import com .sap .cds .ql .Update ;
2220import com .sap .cds .ql .Upsert ;
3735import com .sap .cds .services .handler .annotations .ServiceName ;
3836import com .sap .cds .services .messages .Messages ;
3937import com .sap .cds .services .persistence .PersistenceService ;
40-
41- import cds .gen .adminservice .AdminService ;
42- import cds .gen .adminservice .AdminService_ ;
43- import cds .gen .adminservice .Books ;
44- import cds .gen .adminservice .BooksAddToOrderContext ;
45- import cds .gen .adminservice .BooksCovers ;
46- import cds .gen .adminservice .Books_ ;
47- import cds .gen .adminservice .OrderItems ;
48- import cds .gen .adminservice .OrderItems_ ;
49- import cds .gen .adminservice .Orders ;
50- import cds .gen .adminservice .Upload ;
51- import cds .gen .adminservice .Upload_ ;
52- import cds .gen .my .bookshop .Bookshop_ ;
38+ import java .io .BufferedReader ;
39+ import java .io .IOException ;
40+ import java .io .InputStream ;
41+ import java .io .InputStreamReader ;
42+ import java .math .BigDecimal ;
43+ import java .util .ArrayList ;
44+ import java .util .Arrays ;
45+ import java .util .List ;
46+ import java .util .UUID ;
47+ import java .util .stream .Stream ;
5348import my .bookshop .MessageKeys ;
49+ import org .springframework .stereotype .Component ;
5450
5551/**
5652 * Custom business logic for the "Admin Service" (see admin-service.cds)
@@ -100,11 +96,11 @@ public void beforeCreateOrder(Stream<Orders> orders, EventContext context) {
10096 // calculate the actual quantity difference
10197 // FIXME this should handle book changes, currently only quantity changes are handled
10298 int diffQuantity = quantity - db .run (Select .from (Bookshop_ .ORDER_ITEMS ).columns (i -> i .quantity ()).byId (orderItem .getId ()))
103- .first (OrderItems . class ).map (i -> i .getQuantity ()).orElse (0 );
99+ .first ().map (i -> i .getQuantity ()).orElse (0 );
104100
105101 // check if enough books are available
106- Result result = db .run (Select .from (BOOKS ).columns (b -> b .ID (), b -> b .stock (), b -> b .price ()).byId (bookId ));
107- result .first (Books . class ).ifPresent (book -> {
102+ var result = db .run (Select .from (BOOKS ).columns (b -> b .ID (), b -> b .stock (), b -> b .price ()).byId (bookId ));
103+ result .first ().ifPresent (book -> {
108104 if (book .getStock () < diffQuantity ) {
109105 // Tip: you can have localized messages and use parameters in your messages
110106 messages .error (MessageKeys .BOOK_REQUIRE_STOCK , book .getStock ())
@@ -128,51 +124,43 @@ public void beforeCreateOrder(Stream<Orders> orders, EventContext context) {
128124 });
129125 }
130126
131- /**
127+ /*
132128 * Calculate the total order value preview when editing an order item
133- *
134- * @param context
135- * @param orderItem
136129 */
137- @ Before ( event = DraftService . EVENT_DRAFT_PATCH )
138- public void patchOrderItems (DraftPatchEventContext context , OrderItems orderItem ) {
130+ @ Before
131+ public void patchOrderItems (DraftPatchEventContext context , OrderItems_ ref , OrderItems orderItem ) {
139132 // check if quantity or book was updated
140133 Integer quantity = orderItem .getQuantity ();
141134 String bookId = orderItem .getBookId ();
142- String orderItemId = orderItem .getId ();
143- BigDecimal amount = calculateAmountInDraft (orderItemId , quantity , bookId );
135+ BigDecimal amount = calculateAmountInDraft (ref , quantity , bookId );
144136 if (amount != null ) {
145137 orderItem .setAmount (amount );
146138 }
147139 }
148140
149- /**
141+ /*
150142 * Calculate the total order value preview when deleting an order item from the order
151- *
152- * @param context
153143 */
154- @ Before (event = DraftService .EVENT_DRAFT_CANCEL , entity = OrderItems_ .CDS_NAME )
155- public void cancelOrderItems (DraftCancelEventContext context ) {
156- String orderItemId = (String ) analyzer .analyze (context .getCqn ()).targetKeys ().get (OrderItems .ID );
157- if (orderItemId != null ) {
158- calculateAmountInDraft (orderItemId , 0 , null );
144+ @ Before
145+ public void cancelOrderItems (DraftCancelEventContext context , OrderItems_ ref ) {
146+ if (ref .asRef ().targetSegment ().filter ().isPresent ()) {
147+ calculateAmountInDraft (ref , 0 , null );
159148 }
160149 }
161150
162- private BigDecimal calculateAmountInDraft (String orderItemId , Integer newQuantity , String newBookId ) {
151+ private BigDecimal calculateAmountInDraft (OrderItems_ ref , Integer newQuantity , String newBookId ) {
163152 Integer quantity = newQuantity ;
164153 String bookId = newBookId ;
165154 if (quantity == null && bookId == null ) {
166155 return null ; // nothing changed
167156 }
168157
169158 // get the order item that was updated (to get access to the book price, quantity and order total)
170- Result result = adminService .run (Select .from (ORDER_ITEMS )
159+ var result = adminService .run (Select .from (ref )
171160 .columns (o -> o .quantity (), o -> o .amount (),
172161 o -> o .book ().expand (b -> b .ID (), b -> b .price ()),
173- o -> o .parent ().expand (p -> p .ID (), p -> p .total ()))
174- .where (o -> o .ID ().eq (orderItemId ).and (o .IsActiveEntity ().eq (false ))));
175- OrderItems itemToPatch = result .first (OrderItems .class ).orElseThrow (notFound (MessageKeys .ORDERITEM_MISSING ));
162+ o -> o .parent ().expand (p -> p .ID (), p -> p .total ())));
163+ OrderItems itemToPatch = result .single ();
176164 BigDecimal bookPrice = null ;
177165
178166 // fallback to existing values
@@ -191,9 +179,8 @@ private BigDecimal calculateAmountInDraft(String orderItemId, Integer newQuantit
191179
192180 // get the price of the updated book ID
193181 if (bookPrice == null ) {
194- result = db .run (Select .from (BOOKS ).byId (bookId ).columns (b -> b .price ()));
195- Books book = result .first (Books .class ).orElseThrow (notFound (MessageKeys .BOOK_MISSING ));
196- bookPrice = book .getPrice ();
182+ var bookResult = db .run (Select .from (BOOKS ).byId (bookId ).columns (b -> b .price ()));
183+ bookPrice = bookResult .single ().getPrice ();
197184 }
198185
199186 // update the amount of the order item
@@ -215,9 +202,9 @@ private BigDecimal calculateAmountInDraft(String orderItemId, Integer newQuantit
215202 * @param context
216203 */
217204 @ On (entity = Books_ .CDS_NAME )
218- public void addBookToOrder (BooksAddToOrderContext context ) {
205+ public Orders addBookToOrder (BooksAddToOrderContext context ) {
219206 String orderId = context .getOrderId ();
220- List <Orders > orders = adminService .run (Select .from (ORDERS ).columns (o -> o ._all (), o -> o .Items ().expand ()).where (o -> o .ID ().eq (orderId ))).listOf ( Orders . class );
207+ List <Orders > orders = adminService .run (Select .from (ORDERS ).columns (o -> o ._all (), o -> o .Items ().expand ()).where (o -> o .ID ().eq (orderId ))).list ( );
221208 Orders order = orders .stream ().filter (p -> p .getIsActiveEntity ()).findFirst ().orElse (null );
222209
223210 // check that the order with given ID exists and is not in draft-mode
@@ -241,9 +228,9 @@ public void addBookToOrder(BooksAddToOrderContext context) {
241228 newItem .setQuantity (context .getQuantity ());
242229 order .getItems ().add (newItem );
243230
244- Orders updatedOrder = adminService .run (Update .entity (ORDERS ).data (order )).single (Orders . class );
231+ Orders updatedOrder = adminService .run (Update .entity (ORDERS ).data (order )).single ();
245232 messages .success (MessageKeys .BOOK_ADDED_ORDER );
246- context . setResult ( updatedOrder ) ;
233+ return updatedOrder ;
247234 }
248235
249236 /**
@@ -260,7 +247,7 @@ public Upload getUploadSingleton() {
260247 * @param csv
261248 */
262249 @ On
263- public void addBooksViaCsv (CdsUpdateEventContext context , Upload upload ) {
250+ public List < Upload > addBooksViaCsv (CdsUpdateEventContext context , Upload upload ) {
264251 InputStream is = upload .getCsv ();
265252 if (is != null ) {
266253 try (BufferedReader br = new BufferedReader (new InputStreamReader (is ))) {
@@ -287,7 +274,7 @@ public void addBooksViaCsv(CdsUpdateEventContext context, Upload upload) {
287274 throw new ServiceException (ErrorStatuses .SERVER_ERROR , MessageKeys .BOOK_IMPORT_INVALID_CSV , e );
288275 }
289276 }
290- context . setResult ( Arrays .asList (upload ) );
277+ return Arrays .asList (upload );
291278 }
292279
293280 @ Before (event = {CqnService .EVENT_CREATE , CqnService .EVENT_UPDATE , DraftService .EVENT_DRAFT_NEW , DraftService .EVENT_DRAFT_PATCH })
@@ -296,10 +283,6 @@ public void restoreCoversUpId(CqnStructuredTypeRef ref, BooksCovers cover) {
296283 cover .setUpId ((String ) analyzer .analyze (ref ).rootKeys ().get (Books .ID ));
297284 }
298285
299- private Supplier <ServiceException > notFound (String message ) {
300- return () -> new ServiceException (ErrorStatuses .NOT_FOUND , message );
301- }
302-
303286 private BigDecimal defaultZero (BigDecimal decimal ) {
304287 return decimal == null ? BigDecimal .valueOf (0 ) : decimal ;
305288 }
0 commit comments