From 1e28cb217fe4088ed47105addf98e507d6cae76a Mon Sep 17 00:00:00 2001 From: d045778 Date: Mon, 23 Sep 2019 13:19:24 +0200 Subject: [PATCH] final state of exercise 1 --- .../db/csv/sap.capire.bookstore-Authors.csv | 5 +++ .../csv/sap.capire.bookstore-OrderItems.csv | 4 +++ .../db/csv/sap.capire.bookstore-Orders.csv | 3 ++ .../db/csv/sap.capire.products-Categories.csv | 11 ++++++ .../db/csv/sap.capire.products-Products.csv | 6 ++++ .../sap.capire.products-Products_texts.csv | 4 +++ .../db/csv/sap.common-Currencies.csv | 7 ++++ .../db/csv/sap.common-Currencies_texts.csv | 13 +++++++ packages/bookstore/db/schema.cds | 35 +++++++++++++++++++ packages/bookstore/package.json | 1 + packages/bookstore/srv/services.cds | 17 +++++++++ packages/bookstore/srv/services.js | 18 ++++++++++ 12 files changed, 124 insertions(+) create mode 100755 packages/bookstore/db/csv/sap.capire.bookstore-Authors.csv create mode 100755 packages/bookstore/db/csv/sap.capire.bookstore-OrderItems.csv create mode 100755 packages/bookstore/db/csv/sap.capire.bookstore-Orders.csv create mode 100755 packages/bookstore/db/csv/sap.capire.products-Categories.csv create mode 100755 packages/bookstore/db/csv/sap.capire.products-Products.csv create mode 100755 packages/bookstore/db/csv/sap.capire.products-Products_texts.csv create mode 100755 packages/bookstore/db/csv/sap.common-Currencies.csv create mode 100755 packages/bookstore/db/csv/sap.common-Currencies_texts.csv create mode 100644 packages/bookstore/db/schema.cds create mode 100644 packages/bookstore/srv/services.cds create mode 100644 packages/bookstore/srv/services.js diff --git a/packages/bookstore/db/csv/sap.capire.bookstore-Authors.csv b/packages/bookstore/db/csv/sap.capire.bookstore-Authors.csv new file mode 100755 index 00000000..f97bfeed --- /dev/null +++ b/packages/bookstore/db/csv/sap.capire.bookstore-Authors.csv @@ -0,0 +1,5 @@ +ID;name;dateOfBirth;placeOfBirth;dateOfDeath;placeOfDeath +101;Emily Brontë;1818-07-30;Thornton, Yorkshire;1848-12-19;Haworth, Yorkshire +107;Charlotte Brontë;1818-04-21;Thornton, Yorkshire;1855-03-31;Haworth, Yorkshire +150;Edgar Allen Poe;1809-01-19;Boston, Massachusetts;1849-10-07;Baltimore, Maryland +170;Richard Carpenter;1929-08-14;King’s Lynn, Norfolk;2012-02-26;Hertfordshire, England \ No newline at end of file diff --git a/packages/bookstore/db/csv/sap.capire.bookstore-OrderItems.csv b/packages/bookstore/db/csv/sap.capire.bookstore-OrderItems.csv new file mode 100755 index 00000000..0bc449ec --- /dev/null +++ b/packages/bookstore/db/csv/sap.capire.bookstore-OrderItems.csv @@ -0,0 +1,4 @@ +ID;amount;parent_ID;book_ID;netAmount +301;1;401;201;11.11 +302;1;401;271;15 +303;2;402;252;28 \ No newline at end of file diff --git a/packages/bookstore/db/csv/sap.capire.bookstore-Orders.csv b/packages/bookstore/db/csv/sap.capire.bookstore-Orders.csv new file mode 100755 index 00000000..57268a25 --- /dev/null +++ b/packages/bookstore/db/csv/sap.capire.bookstore-Orders.csv @@ -0,0 +1,3 @@ +ID;modifiedAt;createdAt;createdBy;modifiedBy;OrderNo;currency_code +401;;2019-01-31;john.doe@test.com;;1;EUR +402;;2019-01-30;christian.georgi@sap.com;;2;EUR \ No newline at end of file diff --git a/packages/bookstore/db/csv/sap.capire.products-Categories.csv b/packages/bookstore/db/csv/sap.capire.products-Categories.csv new file mode 100755 index 00000000..331e8ba2 --- /dev/null +++ b/packages/bookstore/db/csv/sap.capire.products-Categories.csv @@ -0,0 +1,11 @@ +ID;parent_ID;name +1;;Poetry +2;;Biography +3;;Fantasy +4;;Science Fiction +5;;Romance +6;;Mystery +7;;Thriller +8;;Dystopia +9;;Tragedy +10;;Novel diff --git a/packages/bookstore/db/csv/sap.capire.products-Products.csv b/packages/bookstore/db/csv/sap.capire.products-Products.csv new file mode 100755 index 00000000..ddbd0834 --- /dev/null +++ b/packages/bookstore/db/csv/sap.capire.products-Products.csv @@ -0,0 +1,6 @@ +ID;title;descr;author_ID;stock;price;currency_code;category_ID +201;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.";101;12;11.11;GBP;9 +207;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.";107;11;12.34;GBP;10 +251;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.";150;333;13.13;USD;1 +252;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.";150;555;14;USD;5 +271;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.;170;22;15;EUR;3 \ No newline at end of file diff --git a/packages/bookstore/db/csv/sap.capire.products-Products_texts.csv b/packages/bookstore/db/csv/sap.capire.products-Products_texts.csv new file mode 100755 index 00000000..fcd8a35e --- /dev/null +++ b/packages/bookstore/db/csv/sap.capire.products-Products_texts.csv @@ -0,0 +1,4 @@ +ID;locale;title;descr +201;de;Sturmhöhe;Sturmhöhe (Originaltitel: Wuthering Heights) ist der einzige Roman der englischen Schriftstellerin Emily Brontë (1818–1848). Der 1847 unter dem Pseudonym Ellis Bell veröffentlichte Roman wurde vom viktorianischen Publikum weitgehend abgelehnt, heute gilt er als ein Klassiker der britischen Romanliteratur des 19. Jahrhunderts. +207;de;Jane Eyre;Jane Eyre. Eine Autobiographie (Originaltitel: Jane Eyre. An Autobiography), erstmals erschienen im Jahr 1847 unter dem Pseudonym Currer Bell, ist der erste veröffentlichte Roman der britischen Autorin Charlotte Brontë und ein Klassiker der viktorianischen Romanliteratur des 19. Jahrhunderts. Der Roman erzählt in Form einer Ich-Erzählung die Lebensgeschichte von Jane Eyre (ausgesprochen /ˌdʒeɪn ˈɛə/), die nach einer schweren Kindheit eine Stelle als Gouvernante annimmt und sich in ihren Arbeitgeber verliebt, jedoch immer wieder um ihre Freiheit und Selbstbestimmung kämpfen muss. Als klein, dünn, blass, stets schlicht dunkel gekleidet und mit strengem Mittelscheitel beschrieben, gilt die Heldin des Romans Jane Eyre nicht zuletzt aufgrund der Kino- und Fernsehversionen der melodramatischen Romanvorlage als die bekannteste englische Gouvernante der Literaturgeschichte +252;de;Eleonora;“Eleonora” ist eine Erzählung von Edgar Allan Poe. Sie wurde 1841 erstveröffentlicht. In ihr geht es um das Paradox der Treue in der Treulosigkeit. \ No newline at end of file diff --git a/packages/bookstore/db/csv/sap.common-Currencies.csv b/packages/bookstore/db/csv/sap.common-Currencies.csv new file mode 100755 index 00000000..1493b31e --- /dev/null +++ b/packages/bookstore/db/csv/sap.common-Currencies.csv @@ -0,0 +1,7 @@ +code;symbol;name;descr +EUR;€;Euro;European Euro +USD;$;US Dollar;United States Dollar +CAD;$;Canadian Dollar;Canadian Dollar +AUD;$;Australian Dollar;Australian Dollar +GBP;£;Pound;Great Britian Pound +ILS;₪;Shekel;Israeli New Shekel \ No newline at end of file diff --git a/packages/bookstore/db/csv/sap.common-Currencies_texts.csv b/packages/bookstore/db/csv/sap.common-Currencies_texts.csv new file mode 100755 index 00000000..4d2ead51 --- /dev/null +++ b/packages/bookstore/db/csv/sap.common-Currencies_texts.csv @@ -0,0 +1,13 @@ +code;locale;name;descr +EUR;de;Euro;European Euro +USD;de;US-Dollar;United States Dollar +CAD;de;Kanadischer Dollar;Kanadischer Dollar +AUD;de;Australischer Dollar;Australischer Dollar +GBP;de;Pfund;Britische Pfund +ILS;de;Schekel;Israelische Schekel +EUR;fr;euro;de la Zone euro +USD;fr;dollar;dollar des États-Unis +CAD;fr;dollar canadien;dollar canadien +AUD;fr;dollar australien;dollar australien +GBP;fr;livre sterling;pound sterling +ILS;fr;Shekel;shekel israelien \ No newline at end of file diff --git a/packages/bookstore/db/schema.cds b/packages/bookstore/db/schema.cds new file mode 100644 index 00000000..c35acbad --- /dev/null +++ b/packages/bookstore/db/schema.cds @@ -0,0 +1,35 @@ +namespace sap.capire.bookstore; + +// We reuse Products, which are Books in our domain +using { sap.capire.products.Products as Books } from '@sap/capire-products'; +using { Currency, managed } from '@sap/cds/common'; + +extend Books with { + author : Association to Authors; +} + +entity Authors : managed { + key ID : Integer; + name : String(111); + dateOfBirth : Date; + dateOfDeath : Date; + placeOfBirth : String; + placeOfDeath : String; + books : Association to many Books on books.author = $self; +} + +entity Orders : managed { + key ID : Integer; + OrderNo : String @title:'Order Number'; //> readable key + Items : Composition of many OrderItems on Items.parent = $self; + total : Decimal(9,2) @readonly; + currency : Currency; +} + +entity OrderItems { + key ID : Integer; + parent : Association to Orders; + book : Association to Books; + amount : Integer; + netAmount : Decimal(9,2); +} \ No newline at end of file diff --git a/packages/bookstore/package.json b/packages/bookstore/package.json index c921bdaf..7b2cf268 100644 --- a/packages/bookstore/package.json +++ b/packages/bookstore/package.json @@ -8,6 +8,7 @@ "node": "^8.9" }, "dependencies": { + "@sap/capire-products": "^1.0.0", "@sap/cds": "^3.17.4", "express": "^4.17.1" }, diff --git a/packages/bookstore/srv/services.cds b/packages/bookstore/srv/services.cds new file mode 100644 index 00000000..bd5b9dc9 --- /dev/null +++ b/packages/bookstore/srv/services.cds @@ -0,0 +1,17 @@ +namespace sap.capire.bookstore; + +using { sap.capire.products.Products, sap.capire.bookstore as my } from '../db/schema'; + +service CatalogService { + @readonly entity Books as projection on Products { + *, category.name as category, author.name + } excluding { createdBy, modifiedBy }; + + @insertonly entity Orders as projection on my.Orders; +} + +// Reuse services from @sap/capire-products... +using { sap.capire.products.AdminService } from '@sap/capire-products'; +extend service AdminService with { + entity Authors as projection on my.Authors; +} \ No newline at end of file diff --git a/packages/bookstore/srv/services.js b/packages/bookstore/srv/services.js new file mode 100644 index 00000000..b6c6edea --- /dev/null +++ b/packages/bookstore/srv/services.js @@ -0,0 +1,18 @@ +const cds = require('@sap/cds') +module.exports = async (srv) => { + + const { Books } = srv.entities + // Check all amounts against stock before activating + srv.before(['CREATE', 'UPDATE'], 'Orders', (req) => { + const tx = cds.transaction(req), order = req.data + return Promise.all(order.Items.map(each => tx.run( + UPDATE(Books).where({ ID: each.book_ID }) + .and(`stock >=`, each.amount) + .set(`stock -=`, each.amount) + ).then(affectedRows => { + if (!affectedRows) { + req.error(409, `${each.amount} exceeds stock for book #${each.book_ID}`) + } + }))) + }) +} \ No newline at end of file