|
11 | 11 |
|
12 | 12 | import org.springframework.stereotype.Component; |
13 | 13 |
|
| 14 | +import com.sap.cds.CdsData; |
14 | 15 | import com.sap.cds.Result; |
15 | 16 | import com.sap.cds.Struct; |
| 17 | +import com.sap.cds.ql.CQL; |
16 | 18 | import com.sap.cds.ql.Insert; |
17 | 19 | import com.sap.cds.ql.Select; |
18 | 20 | import com.sap.cds.ql.Update; |
19 | 21 | import com.sap.cds.ql.cqn.CqnAnalyzer; |
20 | 22 | import com.sap.cds.ql.cqn.CqnSelect; |
| 23 | +import com.sap.cds.ql.cqn.CqnSelectListItem; |
| 24 | +import com.sap.cds.ql.cqn.Modifier; |
21 | 25 | import com.sap.cds.reflect.CdsModel; |
22 | 26 | import com.sap.cds.services.ErrorStatuses; |
23 | 27 | import com.sap.cds.services.ServiceException; |
@@ -74,6 +78,18 @@ class CatalogServiceHandler implements EventHandler { |
74 | 78 | this.analyzer = CqnAnalyzer.create(model); |
75 | 79 | } |
76 | 80 |
|
| 81 | + @Before(entity = Books_.CDS_NAME) |
| 82 | + public void beforeReadBooks(CdsReadEventContext context) { |
| 83 | + CqnSelect copy = CQL.copy(context.getCqn(), new Modifier() { |
| 84 | + @Override |
| 85 | + public List<CqnSelectListItem> items(List<CqnSelectListItem> items) { |
| 86 | + items.add(CQL.get("details")); |
| 87 | + return items; |
| 88 | + } |
| 89 | + }); |
| 90 | + context.setCqn(copy); |
| 91 | + } |
| 92 | + |
77 | 93 | /** |
78 | 94 | * Invokes some validations before creating a review. |
79 | 95 | * |
@@ -132,6 +148,16 @@ public void discountBooks(Stream<Books> books) { |
132 | 148 | }); |
133 | 149 | } |
134 | 150 |
|
| 151 | + @After(event = CqnService.EVENT_READ) |
| 152 | + public void bigBooks(Stream<Books> books) { |
| 153 | + books.filter(b -> b.getDetails() != null).forEach(b -> { |
| 154 | + int pages = (int) b.getDetails().get("pages"); |
| 155 | + if (pages > 400) { |
| 156 | + b.setTitle("%s -- Big Book".formatted(b.getTitle())); |
| 157 | + } |
| 158 | + }); |
| 159 | + } |
| 160 | + |
135 | 161 | @After |
136 | 162 | public void setIsReviewable(CdsReadEventContext context, List<Books> books) { |
137 | 163 | String user = context.getUserInfo().getName(); |
@@ -187,7 +213,8 @@ private void discountBooksWithMoreThan111Stock(Books b, boolean premium) { |
187 | 213 |
|
188 | 214 | private void loadStockIfNotSet(Books b) { |
189 | 215 | if (b.getId() != null && b.getStock() == null) { |
190 | | - b.setStock(db.run(Select.from(BOOKS).byId(b.getId()).columns(Books_::stock)).single(Books.class).getStock()); |
| 216 | + b.setStock( |
| 217 | + db.run(Select.from(BOOKS).byId(b.getId()).columns(Books_::stock)).single(Books.class).getStock()); |
191 | 218 | } |
192 | 219 | } |
193 | 220 |
|
|
0 commit comments