Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions db/books.cds
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ entity Books : cuid, managed {
reviews : Association to many Reviews
on reviews.book = $self;
isReviewable : TechnicalBooleanFlag not null default true;
details : Map;
}

entity Authors : cuid, managed {
Expand All @@ -33,6 +34,7 @@ entity Authors : cuid, managed {
placeOfDeath : String;
books : Association to many Books
on books.author = $self;
details : Map;
}

// annotations for Data Privacy
Expand Down
20 changes: 10 additions & 10 deletions db/data/my.bookshop-Authors.csv
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
ID;NAME;DATEOFBIRTH;PLACEOFBIRTH;DATEOFDEATH;PLACEOFDEATH
335c7bcd-b826-4f14-a788-e0bf6738617a;Emily Brontë;1818-07-30;Thornton, Yorkshire;1848-12-19;Haworth, Yorkshire
e3da2c2e-72ee-45d5-8def-52964c7b252a;Charlote Brontë;1818-04-21;Thornton, Yorkshire;1855-03-31;Haworth, Yorkshire
e7643aae-2d2f-4656-bb2d-1328ad3c8045;Edgar Allen Poe;1809-01-19;Boston, Massachusetts;1849-10-07;Baltimore, Maryland
3c081d9d-abda-4da9-8b6a-4f4555bb26bc;Richard Carpenter;1929-08-14;King’s Lynn, Norfolk;2012-02-26;Hertfordshire, England
b834ddb0-613a-4edf-8d47-7d80989e1325;Jane Austen;1775-12-16;Steventon, United Kingdom;1817-07-18;Winchester, United Kingdom
a57f75fa-2bda-47b5-ab4d-b644570f29cd;F. Scott Fitzgerald;1896-09-24;Saint Paul, Minnesota;1940-12-21;Los Angeles, California
b22f5293-7eea-49bb-9ee7-17c5de81f1df;Harper Lee;1926-04-28;Monroeville, Alabama;2016-02-19;Monroeville, Alabama
1d2ec887-cbf1-491e-943e-33a2b4f39a6f;Gabriel García Márquez;1927-03-06;Aracataca, Columbia;2014-04-17;Mexico City, Mexico
c0526b1a-9a75-4a43-9133-163325cbbd2b;Truman Capote;1924-09-30;New Orleans, Louisiana;1984-08-25;Los Angeles, California
ID;NAME;DATEOFBIRTH;PLACEOFBIRTH;DATEOFDEATH;PLACEOFDEATH;DETAILS
335c7bcd-b826-4f14-a788-e0bf6738617a;Emily Brontë;1818-07-30;Thornton, Yorkshire;1848-12-19;Haworth, Yorkshire;{"pseudonyms": ["Ellis Bell"]}
e3da2c2e-72ee-45d5-8def-52964c7b252a;Charlote Brontë;1818-04-21;Thornton, Yorkshire;1855-03-31;Haworth, Yorkshire;{"pseudonyms": ["Currer Bell"]}
e7643aae-2d2f-4656-bb2d-1328ad3c8045;Edgar Allen Poe;1809-01-19;Boston, Massachusetts;1849-10-07;Baltimore, Maryland;{"nickname": "The Tomahawk Man"}
3c081d9d-abda-4da9-8b6a-4f4555bb26bc;Richard Carpenter;1929-08-14;King’s Lynn, Norfolk;2012-02-26;Hertfordshire, England;{"nickname": "Kip"}
b834ddb0-613a-4edf-8d47-7d80989e1325;Jane Austen;1775-12-16;Steventon, United Kingdom;1817-07-18;Winchester, United Kingdom;{"pseudonyms": ["By a Lady"], "nickname": "Jenny"}
a57f75fa-2bda-47b5-ab4d-b644570f29cd;F. Scott Fitzgerald;1896-09-24;Saint Paul, Minnesota;1940-12-21;Los Angeles, California;{"nickname": "Scottie"}
b22f5293-7eea-49bb-9ee7-17c5de81f1df;Harper Lee;1926-04-28;Monroeville, Alabama;2016-02-19;Monroeville, Alabama;{"nickname": "Nelle"}
1d2ec887-cbf1-491e-943e-33a2b4f39a6f;Gabriel García Márquez;1927-03-06;Aracataca, Columbia;2014-04-17;Mexico City, Mexico;{"nickname": "Gabo"}
c0526b1a-9a75-4a43-9133-163325cbbd2b;Truman Capote;1924-09-30;New Orleans, Louisiana;1984-08-25;Los Angeles, California;{"nickname": "Bulldog", "pseudonyms": ["Buddy"]}
12 changes: 6 additions & 6 deletions db/data/my.bookshop-Books.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ID;TITLE;DESCR;AUTHOR_ID;STOCK;PRICE;CURRENCY_CODE;GENRE_ID;RATING;ISBN
f846b0b9-01d4-4f6d-82a4-d79204f62278;Wuthering Heights;"Wuthering Heights, Emily Brontë's only novel, was published in 1847 under the pseudonym ""Ellis Bell"". It was written between October 1845 and June 1846. Wuthering Heights and Anne Brontë's Agnes Grey were accepted by publisher Thomas Newby before the success of their sister Charlotte's novel Jane Eyre. After Emily's death, Charlotte edited the manuscript of Wuthering Heights and arranged for the edited version to be published as a posthumous second edition in 1850.";335c7bcd-b826-4f14-a788-e0bf6738617a;12;11.11;GBP;103;4.5;979-8698267973
9b084139-0b1e-43b6-b12a-7b3669d75f02;Jane Eyre;"Jane Eyre /ɛər/ (originally published as Jane Eyre: An Autobiography) is a novel by English writer Charlotte Brontë, published under the pen name ""Currer Bell"", on 16 October 1847, by Smith, Elder & Co. of London. The first American edition was published the following year by Harper & Brothers of New York. Primarily a bildungsroman, Jane Eyre follows the experiences of its eponymous heroine, including her growth to adulthood and her love for Mr. Rochester, the brooding master of Thornfield Hall. The novel revolutionised prose fiction in that the focus on Jane's moral and spiritual development is told through an intimate, first-person narrative, where actions and events are coloured by a psychological intensity. The book contains elements of social criticism, with a strong sense of Christian morality at its core and is considered by many to be ahead of its time because of Jane's individualistic character and how the novel approaches the topics of class, sexuality, religion and feminism.";e3da2c2e-72ee-45d5-8def-52964c7b252a;11;12.34;GBP;103;3.0;979-8598716472
51061ce3-ddde-4d70-a2dc-6314afbcc73e;The Raven;"“The Raven"" is a narrative poem by American writer Edgar Allan Poe. First published in January 1845, the poem is often noted for its musicality, stylized language, and supernatural atmosphere. It tells of a talking raven's mysterious visit to a distraught lover, tracing the man's slow fall into madness. The lover, often identified as being a student, is lamenting the loss of his love, Lenore. Sitting on a bust of Pallas, the raven seems to further distress the protagonist with its constant repetition of the word ""Nevermore"". The poem makes use of folk, mythological, religious, and classical references.";e7643aae-2d2f-4656-bb2d-1328ad3c8045;333;13.13;USD;117;2.5;978-1092909747
aebdfc8a-0dfa-4468-bd36-48aabd65e663;Eleonora;"""Eleonora"" is a short story by Edgar Allan Poe, first published in 1842 in Philadelphia in the literary annual The Gift. It is often regarded as somewhat autobiographical and has a relatively ""happy"" ending.";e7643aae-2d2f-4656-bb2d-1328ad3c8045;555;14;USD;117;1.0;979-8669820985
4a519e61-3c3a-4bd9-ab12-d7e0c5329933;Catweazle;Catweazle is a British fantasy television series, starring Geoffrey Bayldon in the title role, and created by Richard Carpenter for London Weekend Television. The first series, produced and directed by Quentin Lawrence, was screened in the UK on ITV in 1970. The second series, directed by David Reid and David Lane, was shown in 1971. Each series had thirteen episodes, most but not all written by Carpenter, who also published two books based on the scripts.;3c081d9d-abda-4da9-8b6a-4f4555bb26bc;22;15;EUR;110;4.0;978-3473523023
ID;TITLE;DESCR;AUTHOR_ID;STOCK;PRICE;CURRENCY_CODE;GENRE_ID;RATING;ISBN;DETAILS
f846b0b9-01d4-4f6d-82a4-d79204f62278;Wuthering Heights;"Wuthering Heights, Emily Brontë's only novel, was published in 1847 under the pseudonym ""Ellis Bell"". It was written between October 1845 and June 1846. Wuthering Heights and Anne Brontë's Agnes Grey were accepted by publisher Thomas Newby before the success of their sister Charlotte's novel Jane Eyre. After Emily's death, Charlotte edited the manuscript of Wuthering Heights and arranged for the edited version to be published as a posthumous second edition in 1850.";335c7bcd-b826-4f14-a788-e0bf6738617a;12;11.11;GBP;103;4.5;979-8698267973;{"pages": 446}
9b084139-0b1e-43b6-b12a-7b3669d75f02;Jane Eyre;"Jane Eyre /ɛər/ (originally published as Jane Eyre: An Autobiography) is a novel by English writer Charlotte Brontë, published under the pen name ""Currer Bell"", on 16 October 1847, by Smith, Elder & Co. of London. The first American edition was published the following year by Harper & Brothers of New York. Primarily a bildungsroman, Jane Eyre follows the experiences of its eponymous heroine, including her growth to adulthood and her love for Mr. Rochester, the brooding master of Thornfield Hall. The novel revolutionised prose fiction in that the focus on Jane's moral and spiritual development is told through an intimate, first-person narrative, where actions and events are coloured by a psychological intensity. The book contains elements of social criticism, with a strong sense of Christian morality at its core and is considered by many to be ahead of its time because of Jane's individualistic character and how the novel approaches the topics of class, sexuality, religion and feminism.";e3da2c2e-72ee-45d5-8def-52964c7b252a;11;12.34;GBP;103;3.0;979-8598716472;{"pages": 290}
51061ce3-ddde-4d70-a2dc-6314afbcc73e;The Raven;"“The Raven"" is a narrative poem by American writer Edgar Allan Poe. First published in January 1845, the poem is often noted for its musicality, stylized language, and supernatural atmosphere. It tells of a talking raven's mysterious visit to a distraught lover, tracing the man's slow fall into madness. The lover, often identified as being a student, is lamenting the loss of his love, Lenore. Sitting on a bust of Pallas, the raven seems to further distress the protagonist with its constant repetition of the word ""Nevermore"". The poem makes use of folk, mythological, religious, and classical references.";e7643aae-2d2f-4656-bb2d-1328ad3c8045;333;13.13;USD;117;2.5;978-1092909747;{"pages": 46}
aebdfc8a-0dfa-4468-bd36-48aabd65e663;Eleonora;"""Eleonora"" is a short story by Edgar Allan Poe, first published in 1842 in Philadelphia in the literary annual The Gift. It is often regarded as somewhat autobiographical and has a relatively ""happy"" ending.";e7643aae-2d2f-4656-bb2d-1328ad3c8045;555;14;USD;117;1.0;979-8669820985;{"pages": 29}
4a519e61-3c3a-4bd9-ab12-d7e0c5329933;Catweazle;Catweazle is a British fantasy television series, starring Geoffrey Bayldon in the title role, and created by Richard Carpenter for London Weekend Television. The first series, produced and directed by Quentin Lawrence, was screened in the UK on ITV in 1970. The second series, directed by David Reid and David Lane, was shown in 1971. Each series had thirteen episodes, most but not all written by Carpenter, who also published two books based on the scripts.;3c081d9d-abda-4da9-8b6a-4f4555bb26bc;22;15;EUR;110;4.0;978-3473523023;{"pages": 224}
33 changes: 26 additions & 7 deletions srv/src/main/java/my/bookshop/handlers/CatalogServiceHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

import com.sap.cds.Result;
import com.sap.cds.Struct;
import com.sap.cds.ql.CQL;
import com.sap.cds.ql.Insert;
import com.sap.cds.ql.Select;
import com.sap.cds.ql.Update;
import com.sap.cds.ql.cqn.CqnAnalyzer;
import com.sap.cds.ql.cqn.CqnSelect;
import com.sap.cds.ql.cqn.CqnSelectListItem;
import com.sap.cds.ql.cqn.Modifier;
import com.sap.cds.reflect.CdsModel;
import com.sap.cds.services.ErrorStatuses;
import com.sap.cds.services.ServiceException;
Expand Down Expand Up @@ -74,6 +77,19 @@ class CatalogServiceHandler implements EventHandler {
this.analyzer = CqnAnalyzer.create(model);
}

@Before(entity = Books_.CDS_NAME)
public void beforeReadBooks(CdsReadEventContext context) {
CqnSelect copy = CQL.copy(context.getCqn(), new Modifier() {
@Override
public List<CqnSelectListItem> items(List<CqnSelectListItem> items) {
items.add(CQL.get("details"));
items.add(CQL.get("stock"));
return items;
}
});
context.setCqn(copy);
}

/**
* Invokes some validations before creating a review.
*
Expand Down Expand Up @@ -127,11 +143,20 @@ public void afterAddReview(BooksAddReviewContext context) {
@After(event = CqnService.EVENT_READ)
public void discountBooks(Stream<Books> books) {
books.filter(b -> b.getTitle() != null).forEach(b -> {
loadStockIfNotSet(b);
discountBooksWithMoreThan111Stock(b, featureToggles.isEnabled("discount"));
});
}

@After(event = CqnService.EVENT_READ)
public void bigBooks(Stream<Books> books) {
books.filter(b -> b.getDetails() != null).forEach(b -> {
int pages = (int) b.getDetails().get("pages");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if pages are not set, but null? This will crash the request. There is no guarantee that pages are set, especially for new books.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it with a new book and it worked, because details is already null and the filter catches this already. With the current bookshop I don't think it breaks here. Ok, we can add an additional null check for pages.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I find this a strange concept here in general. Isn't the idea behind cds.Map that the data is generic and arbitrary and thus I can't really write business logic on it? From a modelling perspective I'd say a better practice would be to model pages explicitly. I get that this is a showcase of the feature, but I somehow fear that this example might lead to inappropriate usage. @agoerler what do you think?

if (pages > 400) {
b.setTitle("%s -- Big Book".formatted(b.getTitle()));
}
});
}

@After
public void setIsReviewable(CdsReadEventContext context, List<Books> books) {
String user = context.getUserInfo().getName();
Expand Down Expand Up @@ -185,10 +210,4 @@ private void discountBooksWithMoreThan111Stock(Books b, boolean premium) {
}
}

private void loadStockIfNotSet(Books b) {
if (b.getId() != null && b.getStock() == null) {
b.setStock(db.run(Select.from(BOOKS).byId(b.getId()).columns(Books_::stock)).single(Books.class).getStock());
}
}

}