From 50e3ca79b197582e23c9415b12a2fe0b5e9bd3df Mon Sep 17 00:00:00 2001 From: Markus Ofterdinger Date: Mon, 10 Feb 2025 14:24:11 +0100 Subject: [PATCH 1/4] Enhanced Books and Authors with details map. --- db/books.cds | 2 ++ db/data/my.bookshop-Authors.csv | 20 ++++++++++---------- db/data/my.bookshop-Books.csv | 12 ++++++------ 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/db/books.cds b/db/books.cds index e7aa3928..51703f30 100644 --- a/db/books.cds +++ b/db/books.cds @@ -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 { @@ -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 diff --git a/db/data/my.bookshop-Authors.csv b/db/data/my.bookshop-Authors.csv index 84d7eb44..137f405d 100644 --- a/db/data/my.bookshop-Authors.csv +++ b/db/data/my.bookshop-Authors.csv @@ -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"]} diff --git a/db/data/my.bookshop-Books.csv b/db/data/my.bookshop-Books.csv index e5453379..4b23413b 100644 --- a/db/data/my.bookshop-Books.csv +++ b/db/data/my.bookshop-Books.csv @@ -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} From 47fbbbdd3a2cb670adaf78b288bee87d6ef613d2 Mon Sep 17 00:00:00 2001 From: Markus Ofterdinger Date: Wed, 12 Feb 2025 15:48:24 +0100 Subject: [PATCH 2/4] use pages to show big books --- .../handlers/CatalogServiceHandler.java | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/srv/src/main/java/my/bookshop/handlers/CatalogServiceHandler.java b/srv/src/main/java/my/bookshop/handlers/CatalogServiceHandler.java index b36b859e..fa34bb88 100644 --- a/srv/src/main/java/my/bookshop/handlers/CatalogServiceHandler.java +++ b/srv/src/main/java/my/bookshop/handlers/CatalogServiceHandler.java @@ -11,13 +11,17 @@ import org.springframework.stereotype.Component; +import com.sap.cds.CdsData; 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; @@ -74,6 +78,18 @@ 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 items(List items) { + items.add(CQL.get("details")); + return items; + } + }); + context.setCqn(copy); + } + /** * Invokes some validations before creating a review. * @@ -132,6 +148,16 @@ public void discountBooks(Stream books) { }); } + @After(event = CqnService.EVENT_READ) + public void bigBooks(Stream books) { + books.filter(b -> b.getDetails() != null).forEach(b -> { + int pages = (int) b.getDetails().get("pages"); + if (pages > 400) { + b.setTitle("%s -- Big Book".formatted(b.getTitle())); + } + }); + } + @After public void setIsReviewable(CdsReadEventContext context, List books) { String user = context.getUserInfo().getName(); @@ -187,7 +213,8 @@ 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()); + b.setStock( + db.run(Select.from(BOOKS).byId(b.getId()).columns(Books_::stock)).single(Books.class).getStock()); } } From 9c879354d8a8b6d09d6cf3cee79c2987237845eb Mon Sep 17 00:00:00 2001 From: Markus Ofterdinger Date: Wed, 12 Feb 2025 16:24:22 +0100 Subject: [PATCH 3/4] cleanup import --- .../main/java/my/bookshop/handlers/CatalogServiceHandler.java | 1 - 1 file changed, 1 deletion(-) diff --git a/srv/src/main/java/my/bookshop/handlers/CatalogServiceHandler.java b/srv/src/main/java/my/bookshop/handlers/CatalogServiceHandler.java index fa34bb88..fe2814ca 100644 --- a/srv/src/main/java/my/bookshop/handlers/CatalogServiceHandler.java +++ b/srv/src/main/java/my/bookshop/handlers/CatalogServiceHandler.java @@ -11,7 +11,6 @@ import org.springframework.stereotype.Component; -import com.sap.cds.CdsData; import com.sap.cds.Result; import com.sap.cds.Struct; import com.sap.cds.ql.CQL; From 7e3d897444042e26f73dabc22249057d1e0751da Mon Sep 17 00:00:00 2001 From: Markus Ofterdinger Date: Thu, 13 Feb 2025 16:12:21 +0100 Subject: [PATCH 4/4] added stock to select list --- .../java/my/bookshop/handlers/CatalogServiceHandler.java | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/srv/src/main/java/my/bookshop/handlers/CatalogServiceHandler.java b/srv/src/main/java/my/bookshop/handlers/CatalogServiceHandler.java index fe2814ca..c9a5b0e7 100644 --- a/srv/src/main/java/my/bookshop/handlers/CatalogServiceHandler.java +++ b/srv/src/main/java/my/bookshop/handlers/CatalogServiceHandler.java @@ -83,6 +83,7 @@ public void beforeReadBooks(CdsReadEventContext context) { @Override public List items(List items) { items.add(CQL.get("details")); + items.add(CQL.get("stock")); return items; } }); @@ -142,7 +143,6 @@ public void afterAddReview(BooksAddReviewContext context) { @After(event = CqnService.EVENT_READ) public void discountBooks(Stream books) { books.filter(b -> b.getTitle() != null).forEach(b -> { - loadStockIfNotSet(b); discountBooksWithMoreThan111Stock(b, featureToggles.isEnabled("discount")); }); } @@ -210,11 +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()); - } - } - }