From cb066233c9e451964ae1c273fc7f6ed995aa3d87 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 2 Mar 2020 02:43:15 +0100 Subject: [PATCH] reviews + events --- _attic/bookshop-enhanced/app/index.cds | 1 - _attic/bookshop-enhanced/db/schema.cds | 25 --- _attic/bookshop-enhanced/package.json | 29 --- _attic/bookshop-enhanced/srv/services.cds | 9 - _attic/bookshop-enhanced/tests/genres.http | 33 --- .../db/data/sap.capire.bookstore-Authors.csv | 5 - .../data/sap.capire.products-Categories.csv | 11 - .../db/data/sap.capire.products-Products.csv | 6 - .../sap.capire.products-Products_texts.csv | 4 - _attic/bookstore/db/schema.cds | 18 -- _attic/bookstore/package.json | 48 ----- _attic/bookstore/srv/_workarounds.cds | 12 -- _attic/bookstore/srv/services.cds | 35 ---- _attic/bookstore/srv/services.js | 38 ---- _attic/bookstore/tests/books.cds | 7 - _attic/bookstore/tests/localized-data.test.js | 190 ------------------ _attic/common-contacts/db/code-lists.cds | 28 --- _attic/common-contacts/db/schema.cds | 58 ------ _attic/common-contacts/index.cds | 2 - _attic/common-contacts/package.json | 10 - _attic/common-contacts/readme.md | 67 ------ _attic/common-contacts/srv/code-lists.js | 70 ------- .../common-contacts/tests/code-lists.test.js | 68 ------- _attic/common-contacts/tests/index.cds | 11 - .../db/data/sap.capire.orders-OrderItems.csv | 4 - .../db/data/sap.capire.orders-Orders.csv | 3 - _attic/orders-service/db/schema.cds | 15 -- _attic/orders-service/index.cds | 2 - _attic/orders-service/package.json | 11 - _attic/orders-service/srv/orders-service.cds | 6 - _attic/products-service/db/schema.cds | 18 -- _attic/products-service/index.cds | 2 - _attic/products-service/package.json | 16 -- _attic/products-service/srv/admin-service.cds | 7 - .../data/sap.capire.products-Categories.csv | 10 - _attic/products-service/tests/postman.json | 157 --------------- _attic/users-service/index.cds | 1 - _attic/users-service/package.json | 12 -- _attic/users-service/srv/services.cds | 7 - .../tests/genres.test.js | 30 +-- events/package.json | 21 ++ events/services.cds | 13 ++ .../srv => events}/services.js | 4 +- events/tests.http | 40 ++++ .../media-server => media}/db/data-model.cds | 0 {_attic/media-server => media}/index.cds | 0 {_attic/media-server => media}/package.json | 4 +- .../srv/media-service.cds | 0 .../srv/media-service.js | 0 package.json | 11 +- reviews/.env | 1 + .../tests/requests.http | 0 52 files changed, 103 insertions(+), 1077 deletions(-) delete mode 100644 _attic/bookshop-enhanced/app/index.cds delete mode 100644 _attic/bookshop-enhanced/db/schema.cds delete mode 100644 _attic/bookshop-enhanced/package.json delete mode 100644 _attic/bookshop-enhanced/srv/services.cds delete mode 100644 _attic/bookshop-enhanced/tests/genres.http delete mode 100644 _attic/bookstore/db/data/sap.capire.bookstore-Authors.csv delete mode 100644 _attic/bookstore/db/data/sap.capire.products-Categories.csv delete mode 100644 _attic/bookstore/db/data/sap.capire.products-Products.csv delete mode 100644 _attic/bookstore/db/data/sap.capire.products-Products_texts.csv delete mode 100644 _attic/bookstore/db/schema.cds delete mode 100644 _attic/bookstore/package.json delete mode 100644 _attic/bookstore/srv/_workarounds.cds delete mode 100644 _attic/bookstore/srv/services.cds delete mode 100644 _attic/bookstore/srv/services.js delete mode 100644 _attic/bookstore/tests/books.cds delete mode 100644 _attic/bookstore/tests/localized-data.test.js delete mode 100644 _attic/common-contacts/db/code-lists.cds delete mode 100644 _attic/common-contacts/db/schema.cds delete mode 100644 _attic/common-contacts/index.cds delete mode 100644 _attic/common-contacts/package.json delete mode 100644 _attic/common-contacts/readme.md delete mode 100644 _attic/common-contacts/srv/code-lists.js delete mode 100644 _attic/common-contacts/tests/code-lists.test.js delete mode 100644 _attic/common-contacts/tests/index.cds delete mode 100644 _attic/orders-service/db/data/sap.capire.orders-OrderItems.csv delete mode 100644 _attic/orders-service/db/data/sap.capire.orders-Orders.csv delete mode 100644 _attic/orders-service/db/schema.cds delete mode 100644 _attic/orders-service/index.cds delete mode 100644 _attic/orders-service/package.json delete mode 100644 _attic/orders-service/srv/orders-service.cds delete mode 100644 _attic/products-service/db/schema.cds delete mode 100644 _attic/products-service/index.cds delete mode 100644 _attic/products-service/package.json delete mode 100644 _attic/products-service/srv/admin-service.cds delete mode 100644 _attic/products-service/tests/data/sap.capire.products-Categories.csv delete mode 100644 _attic/products-service/tests/postman.json delete mode 100644 _attic/users-service/index.cds delete mode 100644 _attic/users-service/package.json delete mode 100644 _attic/users-service/srv/services.cds rename _attic/products-service/tests/categories.test.js => bookshop/tests/genres.test.js (66%) create mode 100644 events/package.json create mode 100644 events/services.cds rename {_attic/bookshop-enhanced/srv => events}/services.js (92%) create mode 100644 events/tests.http rename {_attic/media-server => media}/db/data-model.cds (100%) rename {_attic/media-server => media}/index.cds (100%) rename {_attic/media-server => media}/package.json (85%) rename {_attic/media-server => media}/srv/media-service.cds (100%) rename {_attic/media-server => media}/srv/media-service.js (100%) create mode 100644 reviews/.env rename _attic/bookshop-enhanced/tests/reviews.http => reviews/tests/requests.http (100%) diff --git a/_attic/bookshop-enhanced/app/index.cds b/_attic/bookshop-enhanced/app/index.cds deleted file mode 100644 index 1674468e..00000000 --- a/_attic/bookshop-enhanced/app/index.cds +++ /dev/null @@ -1 +0,0 @@ -using from '@sap/capire-bookshop/app'; diff --git a/_attic/bookshop-enhanced/db/schema.cds b/_attic/bookshop-enhanced/db/schema.cds deleted file mode 100644 index d6adb541..00000000 --- a/_attic/bookshop-enhanced/db/schema.cds +++ /dev/null @@ -1,25 +0,0 @@ -/* - In this model we demonstrate how to add Genres to Books in - as if it was an external extension. For example we use - CDS Aspects' to extend the core domain model's Books entity - as well as the AdminService. - */ - -namespace sap.capire.bookshop; -using { sap.capire.reviews.ReviewsService as external } from '@sap/capire-reviews'; -using { sap.capire.bookshop.Books } from '@sap/capire-bookshop/db/schema'; -using { sap.common.CodeList } from '@sap/cds/common'; - -// Extending Books by Reviews and Genres -extend Books with { - reviews : Composition of many external.Reviews on reviews.subject = ID; - rating : external.Reviews.rating; - genre : Association to Genres; -} - -// Hierarchical Code List for Genres -entity Genres : CodeList { - key ID : Integer; - children : Composition of many Genres on children.parent = $self; - parent : Association to Genres; -} diff --git a/_attic/bookshop-enhanced/package.json b/_attic/bookshop-enhanced/package.json deleted file mode 100644 index 94c0ae7e..00000000 --- a/_attic/bookshop-enhanced/package.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "name": "@sap/capire-bookshop-enhanced", - "version": "1.0.0", - "description": "A sample for extending a base application package, in this case bookshop, e.g. in context of verticalization or customization.", - "repository": "https://github.com/SAP-samples/cloud-cap-samples.git", - "license": "SAP SAMPLE CODE LICENSE", - "dependencies": { - "@sap/capire-bookshop": "^1.0.0", - "@sap/capire-reviews": "^1.0.0", - "@sap/cds": "latest", - "express": "*" - }, - "scripts": { - "reviews-service": "PORT=5005 cds run ../reviews-service --bind --in-memory?", - "start": "cds run --in-memory?", - "watch": "cds watch" - }, - "cds": { - "requires": { - "sap.capire.reviews.ReviewsService": { - "kind": "odata", - "model": "@sap/capire-reviews" - }, - "messaging": { - "kind": "file-based-messaging" - } - } - } -} diff --git a/_attic/bookshop-enhanced/srv/services.cds b/_attic/bookshop-enhanced/srv/services.cds deleted file mode 100644 index 7ff9f9af..00000000 --- a/_attic/bookshop-enhanced/srv/services.cds +++ /dev/null @@ -1,9 +0,0 @@ -namespace sap.capire.bookshop; - -using { AdminService } from '@sap/capire-bookshop/srv/admin-service'; -using { sap.capire.bookshop } from '../db/schema'; - -@impl:'srv/services' -extend service AdminService with { - entity Genres as projection on bookshop.Genres; -} diff --git a/_attic/bookshop-enhanced/tests/genres.http b/_attic/bookshop-enhanced/tests/genres.http deleted file mode 100644 index aecd8777..00000000 --- a/_attic/bookshop-enhanced/tests/genres.http +++ /dev/null @@ -1,33 +0,0 @@ -################################################# -# -# Genres -# - -GET http://localhost:4004/admin/Genres? -### - -POST http://localhost:4004/admin/Genres? -Content-Type: application/json - -{ "ID":100, "name":"Some Sample Genres...", "children":[ - { "ID":101, "name":"Cat", "children":[ - { "ID":102, "name":"Kitty", "children":[ - { "ID":103, "name":"Kitty Cat", "children":[ - { "ID":104, "name":"Aristocat" } ]}, - { "ID":105, "name":"Kitty Bat" } ]}, - { "ID":106, "name":"Catwoman", "children":[ - { "ID":107, "name":"Catalina" } ]} ]}, - { "ID":108, "name":"Catweazle" } -]} -### - -GET http://localhost:4004/admin/Genres(100)? -# &$expand=children -# &$expand=children($expand=children($expand=children($expand=children))) -### - -DELETE http://localhost:4004/admin/Genres(103) -### - -DELETE http://localhost:4004/admin/Genres(100) -### diff --git a/_attic/bookstore/db/data/sap.capire.bookstore-Authors.csv b/_attic/bookstore/db/data/sap.capire.bookstore-Authors.csv deleted file mode 100644 index 8c2d2f71..00000000 --- a/_attic/bookstore/db/data/sap.capire.bookstore-Authors.csv +++ /dev/null @@ -1,5 +0,0 @@ -ID;firstname;lastname;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/_attic/bookstore/db/data/sap.capire.products-Categories.csv b/_attic/bookstore/db/data/sap.capire.products-Categories.csv deleted file mode 100644 index 331e8ba2..00000000 --- a/_attic/bookstore/db/data/sap.capire.products-Categories.csv +++ /dev/null @@ -1,11 +0,0 @@ -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/_attic/bookstore/db/data/sap.capire.products-Products.csv b/_attic/bookstore/db/data/sap.capire.products-Products.csv deleted file mode 100644 index ddbd0834..00000000 --- a/_attic/bookstore/db/data/sap.capire.products-Products.csv +++ /dev/null @@ -1,6 +0,0 @@ -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/_attic/bookstore/db/data/sap.capire.products-Products_texts.csv b/_attic/bookstore/db/data/sap.capire.products-Products_texts.csv deleted file mode 100644 index fcd8a35e..00000000 --- a/_attic/bookstore/db/data/sap.capire.products-Products_texts.csv +++ /dev/null @@ -1,4 +0,0 @@ -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/_attic/bookstore/db/schema.cds b/_attic/bookstore/db/schema.cds deleted file mode 100644 index 5f82d533..00000000 --- a/_attic/bookstore/db/schema.cds +++ /dev/null @@ -1,18 +0,0 @@ -namespace sap.capire.bookstore; - -// We reuse Products, which are Books in our domain -using { sap.capire.products.Products as Books } from '@sap/capire-products'; -extend Books with { - author : Association to Authors; - rating : Decimal(2,1); -} - -// We reuse aspect Person to define Authors -using { sap.capire.contacts.Person } from '@sap/capire-contacts'; -entity Authors : Person { - key ID : UUID; - books : Association to many Books on books.author = $self; -} - -// we use enhanced currencies code lists -using from '@sap/capire-currencies'; diff --git a/_attic/bookstore/package.json b/_attic/bookstore/package.json deleted file mode 100644 index 0f18561b..00000000 --- a/_attic/bookstore/package.json +++ /dev/null @@ -1,48 +0,0 @@ -{ - "name": "@sap/capire-bookstore", - "version": "1.0.0", - "description": "A variant of the bookshop application, built on top of products-service and common reuse packages.", - "repository": "https://github.com/SAP-samples/cloud-cap-samples.git", - "license": "SAP SAMPLE CODE LICENSE", - "dependencies": { - "@sap/capire-products": "^1.0.0", - "@sap/capire-reviews": "^1.0.0", - "@sap/capire-orders": "^1.0.0", - "@sap/capire-media": "^1.0.0", - "@sap/capire-users": "^1.0.0", - "@sap/capire-contacts": "^1.0.0", - "@sap/capire-currencies": "^1.0.0", - "@sap/cds": "latest", - "express": "*" - }, - "bundledDependencies": [ - "@sap/capire-products", - "@sap/capire-reviews", - "@sap/capire-orders", - "@sap/capire-media", - "@sap/capire-users", - "@sap/capire-contacts", - "@sap/capire-currencies" - ], - "files": [ - "app", "srv", "db" - ], - "scripts": { - "start": "cds run --in-memory?", - "watch": "cds watch" - }, - "cds": { - "requires": { - "db": { - "kind": "sql" - }, - "sap.capire.media.MediaServer": { - "kind": "rest" - }, - "sap.capire.reviews.ReviewsService": { - "model": "@sap/capire-reviews", - "kind": "odata" - } - } - } -} diff --git a/_attic/bookstore/srv/_workarounds.cds b/_attic/bookstore/srv/_workarounds.cds deleted file mode 100644 index 60cc099a..00000000 --- a/_attic/bookstore/srv/_workarounds.cds +++ /dev/null @@ -1,12 +0,0 @@ -//---------------------- -// workarounds -> should be done by @cds-compiler - -annotate cds.UUID with @odata.Type: 'Edm.String'; - -using from '@sap/capire-products'; -annotate sap.capire.products.Products with { texts @odata.contained } -annotate sap.capire.products.Categories with { texts @odata.contained } - -annotate sap.capire.reviews.ReviewsService with @imported; -annotate sap.capire.reviews.Reviews with @cds.persistence.skip; -annotate sap.capire.reviews.Likes with @cds.persistence.skip; diff --git a/_attic/bookstore/srv/services.cds b/_attic/bookstore/srv/services.cds deleted file mode 100644 index b630ebe9..00000000 --- a/_attic/bookstore/srv/services.cds +++ /dev/null @@ -1,35 +0,0 @@ -namespace sap.capire.bookstore; - -// Service for all users to browse books -using { sap.capire.products } from '../db/schema'; - -service CatalogService @(path:'browse'){ - - @readonly entity Books as select from products.Products { *, - author.firstname ||' '|| author.lastname as author : String, - category.name as genre, - } excluding { createdBy, modifiedBy }; - - @readonly entity Genres as projection on products.Categories; - -} - -// Reuse AdminService from @sap/capire-products... -using { sap.capire.products.AdminService } from '@sap/capire-products'; -using { sap.capire.bookstore as my } from '../db/schema'; - -extend service AdminService with @(impl:'srv/services.js') { - entity Authors as projection on my.Authors; -} - -// Adding reviews via @sap/capire-reviews service -using { sap.capire.reviews.ReviewsService as external } from '@sap/capire-reviews'; -extend service CatalogService with { - @readonly entity Reviews as projection on external.Reviews; -} - - -// Adding images via @sap/capire-media service -using from '@sap/capire-media'; -// using from '@sap/capire-orders'; -// using from '@sap/capire-users'; diff --git a/_attic/bookstore/srv/services.js b/_attic/bookstore/srv/services.js deleted file mode 100644 index 3b6430aa..00000000 --- a/_attic/bookstore/srv/services.js +++ /dev/null @@ -1,38 +0,0 @@ -const cds = require('@sap/cds') -module.exports['sap.capire.bookstore.CatalogService'] = cds.service.impl (async (srv) => { - - const ReviewsService = await cds.connect.to ('sap.capire.reviews.ReviewsService') - const { Reviews } = ReviewsService.entities - const { Books } = srv.entities - - // delegate requests to reviews service - srv.on('READ', 'Reviews', async (req) => { - const { SELECT } = cds.ql(req) - const results = await SELECT.from (Reviews) - - // TODO: Should actually be using .where of fluent query API - if (req.query.SELECT.where) { - return results.filter (row => row.subject === req.query.SELECT.where[2].val) - } - - return results - }) - - // react on event messages from reviews service - ReviewsService.on ('reviewed', (msg) => { - console.debug ('> received message:', msg.event, msg.data) - const {subject,rating} = msg.data - const tx = cds.transaction(msg) - return tx.run (UPDATE(Books).set({rating}) .where ({ID:subject})) //.then (console.log) - }) - -}) - - -// FIXME: pls remove this... -process.env.destinations = JSON.stringify([{ - name: 'reviewsDest', - url: 'http://localhost:4005/reviews', - username: 'dummy', - password: 'dummy' -}]) diff --git a/_attic/bookstore/tests/books.cds b/_attic/bookstore/tests/books.cds deleted file mode 100644 index 4c1309d9..00000000 --- a/_attic/bookstore/tests/books.cds +++ /dev/null @@ -1,7 +0,0 @@ -using { sap.capire.products as my } from '../db/schema'; - -service BooksService { - entity Books as SELECT from my.Products; -} - -annotate cds.UUID with @odata.Type: 'Edm.String'; diff --git a/_attic/bookstore/tests/localized-data.test.js b/_attic/bookstore/tests/localized-data.test.js deleted file mode 100644 index acf35d44..00000000 --- a/_attic/bookstore/tests/localized-data.test.js +++ /dev/null @@ -1,190 +0,0 @@ -const cds = require ('@sap/cds') - -describe('Localized data on db level', ()=>{ - - let db, Books - - it ('should deploy the db schema to sqlite in-memory', async()=>{ - db = await cds.deploy (__dirname+'/books') .to ('sqlite::memory:') - expect (db.model) .toBeDefined() - Books = db.entities('sap.capire.products').Products - expect (Books) .toBeDefined() - }) - - it ('should list all books with default language', async ()=>{ - const books = await SELECT.from (Books, b=>b.title) - expect (books) .toMatchObject([ - { title: 'Wuthering Heights' }, - { title: 'Jane Eyre' }, - { title: 'The Raven' }, - { title: 'Eleonora' }, - { title: 'Catweazle' } - ]) - }) - - - it ('should read translated texts from Books_texts', async ()=>{ - const texts = await SELECT ('locale','title').from (Books+'_texts') - expect (texts) .toMatchObject ([ - { locale: 'de', title: 'Sturmhöhe' }, - { locale: 'de', title: 'Jane Eyre' }, - { locale: 'de', title: 'Eleonora' } - ]) - }) - - it ('should read translated texts from Books.texts', async ()=>{ - const book = await SELECT.one.from (Books, b=>{ - b.ID, b.title, b.texts(t=> { - t.locale, t.title - }) - }) .where ({title:'Wuthering Heights'}) - expect (book) .toMatchObject ({ - title: 'Wuthering Heights', texts:[ - {locale:'de',title:'Sturmhöhe'} - ] - }) - }) - - it ('should insert books with translated texts', async ()=>{ - const n = await INSERT.into (Books) .entries ({ ID:444, title:'A New Book', texts:[ - {locale:'de', title:'Ein Neues Buch'}, - {locale:'fr', title:'Un Nouveau Livre'}, - ]}) - expect(n).toBe(3) - }) - - it ('should delete books w/ cascaded delete to texts', async()=>{ - const n = await DELETE.from(Books) .where ({ID:444}) - expect(n).toBe(3) - }) - -}) - - -describe('Localized data on service level', ()=>{ - - let srv, Books - - it ('should serve BooksService', async()=>{ - srv = await cds.serve('BooksService').from(__dirname+'/books') - expect (srv.model) .toBeDefined() - Books = srv.entities.Books - expect (Books) .toBeDefined() - }) - - it ('should list all books with default language', async ()=>{ - const books = await srv.read (Books, b=>b.title) - expect (books) .toMatchObject([ - { title: 'Wuthering Heights' }, - { title: 'Jane Eyre' }, - { title: 'The Raven' }, - { title: 'Eleonora' }, - { title: 'Catweazle' } - ]) - }) - - it ('should read Books with translated texts', async ()=>{ - const book = await srv.run ( - SELECT.from (Books, b=>{ b.ID, b.title, b.texts(t=> { - t.locale, t.title - })}) .where ({title:'Wuthering Heights'}) - ) - expect (book) .toMatchObject ([{ - title: 'Wuthering Heights', texts:[ - {locale:'de',title:'Sturmhöhe'} - ] - }]) - }) - - it ('should do the same with convenient method', async ()=>{ - const book = await srv.read (Books, b=>{ b.ID, b.title, b.texts(t=> { - t.locale, t.title - })}) .where ({title:'Wuthering Heights'}) - expect (book) .toMatchObject ([{ - title: 'Wuthering Heights', texts:[ - {locale:'de',title:'Sturmhöhe'} - ] - }]) - }) - - it ('should read single Book with translated texts', async ()=>{ - const book = await srv.run ( - SELECT.one.from (Books, b=>{ b.ID, b.title, b.texts(t=> { - t.locale, t.title - })}) .where ({title:'Wuthering Heights'}) - ) - expect (book) .toMatchObject ({ - title: 'Wuthering Heights', texts:[ - {locale:'de',title:'Sturmhöhe'} - ] - }) - }) - - it ('should insert books with translated texts', async ()=>{ - const book = { ID:444, title:'A New Book', texts:[ - {locale:'de', title:'Ein Neues Buch'}, - {locale:'fr', title:'Un Nouveau Livre'}, - ]} - const response = await srv.create (Books) .entries (book) - expect(response).toMatchObject(book) - }) - - it ('should delete books w/ cascaded delete to texts', async()=>{ - await srv.delete('Books') .where ({ID:444}) - }) -}) - - -describe('Localized data on OData level', () => { - - const app = require('express')() - const srv = require('supertest')(app) - - it ('should serve BooksService', async ()=>{ - await cds.serve('BooksService').from(__dirname+'/books') .in (app) - }) - - it('should list all books with default language', async () => { - const books = await srv.get('/books/Books/201/title') - expect(books.body).toMatchObject({'value': 'Wuthering Heights'}) - }) - - it('should read books with translated texts', async () => { - const books = await srv.get('/books/Books/201/title'). set('Accept-Language', 'de') - expect(books.body).toMatchObject({value: 'Sturmhöhe'}) - }) - - it('should expand translated texts in Book', async () => { - const books = await srv. get('/books/Books/201?$select=title&$expand=texts($select=locale,title)') - expect(books.body).toMatchObject({ - title: 'Wuthering Heights', - texts: [ - { locale: 'de', title: 'Sturmhöhe', }, - ], - }) - }) - - const book = { - title: 'New Book', descr: 'Lorem Ipsum', - texts: [ - { locale: 'de', title: 'Neues Buch', descr: 'Dolor sit amet' }, - { locale: 'fr', title: 'Nouveau Livre', descr: 'consetetur sadipscing elitr' } - ], - } - - it('should insert books with translated texts', async () => { - const {body} = await srv.post('/books/Books').send(book) - expect(body).toMatchObject(book) - book.ID = body.ID - }) - - it ('should read the newly created book', async()=>{ - const {body} = await srv.get('/books/Books/'+book.ID+'?$expand=texts').send(book) - expect(body).toMatchObject(book) - }) - - it ('should delete books w/ cascaded delete to texts', async()=>{ - await srv.delete('/books/Books/'+book.ID) - .expect(204) - }) -}) diff --git a/_attic/common-contacts/db/code-lists.cds b/_attic/common-contacts/db/code-lists.cds deleted file mode 100644 index 1d31dc7a..00000000 --- a/_attic/common-contacts/db/code-lists.cds +++ /dev/null @@ -1,28 +0,0 @@ -using { sap } from '@sap/cds/common'; -namespace sap.common; - -/** - * The Code Lists below are designed as optional extensions to - * the base schema. Switch them on by adding an Association to - * one of the code list entities in your models or by: - * annotate sap.common.Countries with @cds.persistence.skip:false; - */ - -extend sap.common.Countries { - regions : Composition of many Regions on regions._parent = $self.code; -} -entity Regions : sap.common.CodeList { - key code : String(5); // ISO 3166-2 alpha5 codes, e.g. DE-BW - children : Composition of many Regions on children._parent = $self.code; - cities : Composition of many Cities on cities.region = $self; - _parent : String(11); -} -entity Cities : sap.common.CodeList { - key code : String(11); - region : Association to Regions; - districts : Composition of many Districts on districts.city = $self; -} -entity Districts : sap.common.CodeList { - key code : String(11); - city : Association to Cities; -} diff --git a/_attic/common-contacts/db/schema.cds b/_attic/common-contacts/db/schema.cds deleted file mode 100644 index c9a7410f..00000000 --- a/_attic/common-contacts/db/schema.cds +++ /dev/null @@ -1,58 +0,0 @@ -namespace sap.capire.contacts; - - -//-------------------------------------------------------------------------- -// Aspects - - - aspect Organization { - orgname : String(111); - } - - aspect Person { - firstname : String(111); - lastname : String(111); - prefix : String(11); - suffix : String(11); - middle : String(11); - dateOfBirth : Date; placeOfBirth : String; - dateOfDeath : Date; placeOfDeath : String; - } - - aspect PostalAddress { - street : String(222) @multiline; - postCode : String(11); - district : String(111); - city : String(111); - region : String(111); - country : String(111); - } - - aspect ContactOptions { - email : String @JSON:[{ kind:String, address: EmailAddress }]; - phone : String @JSON:[{ kind:String, number: PhoneNumber }]; - // phone : array of { kind:String; number: PhoneNumber }; - // addresses : Composition of many PostalAddress; - } - - type EmailAddress : String; - type PhoneNumber : String; - - - -//-------------------------------------------------------------------------- -// Entities - - @cds.persistence.skip:'if-unused' - entity Contacts : Person, Organization, ContactOptions { - key ID : UUID; - isOrg : Boolean; - addresses : Composition of many PostalAddresses on addresses.contact = $self; - } - - @cds.persistence.skip:'if-unused' - entity PostalAddresses : PostalAddress { - contact : Association to Contacts; - kind : String; - key ID : UUID; - } diff --git a/_attic/common-contacts/index.cds b/_attic/common-contacts/index.cds deleted file mode 100644 index 0a1d479d..00000000 --- a/_attic/common-contacts/index.cds +++ /dev/null @@ -1,2 +0,0 @@ -using from './db/code-lists'; -using from './db/schema'; diff --git a/_attic/common-contacts/package.json b/_attic/common-contacts/package.json deleted file mode 100644 index 4221f4ab..00000000 --- a/_attic/common-contacts/package.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "name": "@sap/capire-contacts", - "version": "1.0.0", - "description": "A reuse package providing common domain models and services for contacts-related data.", - "repository": "https://github.com/SAP-samples/cloud-cap-samples.git", - "license": "SAP SAMPLE CODE LICENSE", - "dependencies": { - "@sap/cds": "latest" - } -} \ No newline at end of file diff --git a/_attic/common-contacts/readme.md b/_attic/common-contacts/readme.md deleted file mode 100644 index 77ea0ed2..00000000 --- a/_attic/common-contacts/readme.md +++ /dev/null @@ -1,67 +0,0 @@ -# Common Contacts Sample - -This sample provides a reuse package with common domain models and services for contacts-related data. - -## Usage - - -#### Import to your project - - npm install @sap/capire-contacts - -> e.g. see: [bookstore](../bookstore/package.json) - -#### Reusing aspects - -Define own entities derived from the pre-defined aspects as in [_bookstore_](../bookstore/db/schema.cds): - -```swift -using { sap.capire.contacts.Person } from '@sap/capire-contacts'; -entity Authors : contacts.Person { ... } -``` - -> **Note:** All entities in this package are annotated with _`@cds.persistence.skip`:'if-unused'_, so they will be ignored if not referred to from other entities in your models. - - -#### Reusing entities - -Reuse the entities as in this example from [_users-service_](../users-service/srv/services.cds): -```swift -using { sap.capire.contacts.Contacts } from '@sap/capire-contacts'; -service UsersService @(requires:'authenticated-user') { - entity MyProfile as select from Contacts where ID=$user; - ... -} -``` - - -#### Reusing code lists - -Reuse the code lists as in [_./tests/index.cds_](./tests/index.cds): - -```swift -service Sue { ... - // expose Countries to activate provided code lists - @readonly entity Countries as projection on sap.capire.contacts.Countries; -} -``` - - - -#### Reuse code list service - -```js -const { intercept } = require ('@sap/capire-contacts/srv/code-lists') -``` - - - -## Content - - - -## Concepts - -* [Reuse of packages](https://cap.cloud.sap/docs/get-started/projects#reuse) -* Code Lists, `@sap/cds/common` and `@cds.persistence.skip`: 'if-unused' -* Using `aspects` vs `entities` diff --git a/_attic/common-contacts/srv/code-lists.js b/_attic/common-contacts/srv/code-lists.js deleted file mode 100644 index aac662b8..00000000 --- a/_attic/common-contacts/srv/code-lists.js +++ /dev/null @@ -1,70 +0,0 @@ -const cds = require ('@sap/cds') -const READ='READ', WRITE = ['CREATE','UPDATE'] - -const intercept = exports.intercept = cds.service.impl (async (srv) => { - - for (let each in srv.entities) { - - // intercept JSON-encoded elements - const jsons = await jsonsIn (srv.entities[each].elements) - if (jsons) { - srv.before (WRITE, each, ({data:row})=>{ - for (let e of jsons) if (row[e]) row[e] = JSON.stringify (row[e]) - }) - srv.after (READ, each, (row)=>{ - for (let e of jsons) if (row[e]) row[e] = JSON.parse (row[e]) - }) - } - - // intercept references - const refs = await refsIn (srv.entities[each].elements, srv.model) - if (refs) srv.after (READ, each, (rows, req)=>{ - for (let row of rows) { - for (let {element,codelist} of refs) { - const entry = codelist [row[element]] - if (entry) { - const localized = entry.texts [req.user.locale || intercept.locale] - row[element] = localized ? localized.name : entry.name - } - } - } - }) - - } - -}) - - -function jsonsIn (elements) { - const jsons=[]; for (let e in elements) { - if (elements[e]['@JSON']) jsons.push(e) - } - return jsons.length && jsons -} - -async function refsIn (elements, model) { - const refs=[]; for (let e in elements) { - const $ref = elements[e]['@ref'] - if ($ref) { - const d = model.definitions [$ref['=']] - refs.push({ - element:e, - codelist: CodeLists[d.name] || (CodeLists[d.name] = await load(d)) - }) - } - } - return refs.length && refs -} - -const load = exports.load = async (codelist) => { - const all = {} - const [entries,texts] = await Promise.all ([ - SELECT.from (codelist), - SELECT.from (codelist.elements.texts.target) - ]) - for (let {code,name,descr} of entries) all[code] = {name,descr} - for (let {code,locale,name,descr} of texts) (all[code].texts || (all[code].texts={})) [locale] = {name,descr} - return all -} - -const CodeLists = {} diff --git a/_attic/common-contacts/tests/code-lists.test.js b/_attic/common-contacts/tests/code-lists.test.js deleted file mode 100644 index ee367abf..00000000 --- a/_attic/common-contacts/tests/code-lists.test.js +++ /dev/null @@ -1,68 +0,0 @@ -const {load,intercept} = require ('../srv/code-lists') -const cds = require ('@sap/cds') - -// patch-enhance cds.ql -const select = SELECT.from('.').__proto__.__proto__, query = select.__proto__ -query.then = function (r,e) { return db.run(this) .then (r,e || ((e)=>{throw e})) } - -let db, Countries, Australia = { - name: 'Australia', descr: 'Commonwealth of Australia', texts: { - de: { name: 'Australien', descr: 'Commonwealth Australien' } - } -} - -describe ('code list tests', ()=>{ - - it ('should deploy the db schema to sqlite in-memory', async()=>{ - db = await cds.deploy (__dirname) .to ('sqlite::memory:', {silent:true,primary:true}) - Countries = db.model.entities ['sap.common.Countries'] - expect (Countries) .toBeDefined() - }) - - it ('should read Countries', async()=>{ - const countries = await SELECT ('code','name') .from (Countries) - expect (countries) .toContainEqual ({ code: 'AU', name: 'Australia' }) - }) - - it ('should read Countries_texts', async()=>{ - const countries = await SELECT ('locale','code','name') .from ('sap.common.Countries_texts') - expect (countries) .toContainEqual ({ locale: 'de', code: 'AU', name: 'Australien' }) - }) - - it ('should read code lists with translated texts', async()=>{ - const {AU} = await load (Countries) - expect (AU) .toEqual (Australia) - }) - - cds.env.singletenant = true - - it ('should serve services with localized data', async()=>{ - const { Sue:sue } = await cds.serve (__dirname) - const { Foos } = sue.entities - await sue.create (Foos) .entries ({country:'Avalon'}) - await sue.create (Foos) .entries ({country:'AU'}) - expect (await sue.read('Foos')) .toEqual ([ { ID: 1, country: 'Avalon' }, { ID: 2, country: 'AU' } ]) - }) - - it ('should resolve countries', async()=>{ - const sue = await cds.connect.to ('Sue') - await intercept (sue) - expect (await sue.read('Foos')) .toEqual ([ { ID: 1, country: 'Avalon' }, { ID: 2, country: 'Australia' } ]) - intercept.locale = 'de' - expect (await sue.read('Foos')) .toEqual ([ { ID: 1, country: 'Avalon' }, { ID: 2, country: 'Australien' } ]) - console.log (await sue.read('Foos')) - }) - - it ('should read countries with expand to translated texts', async()=>{ - const countries = await cds.read (Countries, c=>{ - c.name, c.texts (t => { - t.locale, t.name - }) - }) - console.log (countries) - }) - - it ('should disconnect from db', ()=> db.disconnect()) - //> FIXME: that should not be required! - -}) diff --git a/_attic/common-contacts/tests/index.cds b/_attic/common-contacts/tests/index.cds deleted file mode 100644 index 96adbe01..00000000 --- a/_attic/common-contacts/tests/index.cds +++ /dev/null @@ -1,11 +0,0 @@ -using { sap } from '..'; - -entity Foo { - key ID : Integer; - country : String @ref: sap.capire.contacts.Countries; -} -service Sue { - entity Foos as projection on Foo; - // expose Countries to activate provided code lists - @readonly entity Countries as projection on sap.capire.contacts.Countries; -} diff --git a/_attic/orders-service/db/data/sap.capire.orders-OrderItems.csv b/_attic/orders-service/db/data/sap.capire.orders-OrderItems.csv deleted file mode 100644 index 1b5ff074..00000000 --- a/_attic/orders-service/db/data/sap.capire.orders-OrderItems.csv +++ /dev/null @@ -1,4 +0,0 @@ -ID;amount;parent_ID;article -58040e66-1dcd-4ffb-ab10-fdce32028b79;1;7e2f2640-6866-4dcf-8f4d-3027aa831cad;201 -64e718c9-ff99-47f1-8ca3-950c850777d4;1;7e2f2640-6866-4dcf-8f4d-3027aa831cad;271 -e9641166-e050-4261-bfee-d1e797e6cb7f;2;64e718c9-ff99-47f1-8ca3-950c850777d4;252 \ No newline at end of file diff --git a/_attic/orders-service/db/data/sap.capire.orders-Orders.csv b/_attic/orders-service/db/data/sap.capire.orders-Orders.csv deleted file mode 100644 index 143da2c3..00000000 --- a/_attic/orders-service/db/data/sap.capire.orders-Orders.csv +++ /dev/null @@ -1,3 +0,0 @@ -ID;modifiedAt;createdAt;createdBy;modifiedBy;OrderNo;currency_code -7e2f2640-6866-4dcf-8f4d-3027aa831cad;;2019-01-31;john.doe@test.com;;1;EUR -64e718c9-ff99-47f1-8ca3-950c850777d4;;2019-01-30;christian.georgi@sap.com;;2;EUR \ No newline at end of file diff --git a/_attic/orders-service/db/schema.cds b/_attic/orders-service/db/schema.cds deleted file mode 100644 index f7d7b4fc..00000000 --- a/_attic/orders-service/db/schema.cds +++ /dev/null @@ -1,15 +0,0 @@ -namespace sap.capire.orders; -using { Currency, cuid, managed } from '@sap/cds/common'; - - -entity Orders : cuid, managed { - OrderNo : String @title:'Order Number'; //> readable key - Items : Composition of many OrderItems on Items.parent = $self; - currency : Currency; -} - -entity OrderItems : cuid { - parent : Association to Orders not null; - article : String; - amount : Integer; -} diff --git a/_attic/orders-service/index.cds b/_attic/orders-service/index.cds deleted file mode 100644 index 8e460ac4..00000000 --- a/_attic/orders-service/index.cds +++ /dev/null @@ -1,2 +0,0 @@ -namespace sap.capire.orders; -using from './srv/orders-service'; diff --git a/_attic/orders-service/package.json b/_attic/orders-service/package.json deleted file mode 100644 index f8341b12..00000000 --- a/_attic/orders-service/package.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "@sap/capire-orders", - "version": "1.0.0", - "description": "A reuse package providing domain models and services to submit and manage purchase orders.", - "repository": "https://github.com/SAP-samples/cloud-cap-samples.git", - "license": "SAP SAMPLE CODE LICENSE", - "dependencies": { - "@sap/cds": "latest", - "express": "*" - } -} diff --git a/_attic/orders-service/srv/orders-service.cds b/_attic/orders-service/srv/orders-service.cds deleted file mode 100644 index 766cc012..00000000 --- a/_attic/orders-service/srv/orders-service.cds +++ /dev/null @@ -1,6 +0,0 @@ -namespace sap.capire.orders; -using { sap.capire.orders as my } from '../db/schema'; - -service OrdersService { - entity Orders as projection on my.Orders; -} diff --git a/_attic/products-service/db/schema.cds b/_attic/products-service/db/schema.cds deleted file mode 100644 index 573e93ad..00000000 --- a/_attic/products-service/db/schema.cds +++ /dev/null @@ -1,18 +0,0 @@ -namespace sap.capire.products; - -using { Currency, cuid, managed, sap.common.CodeList } from '@sap/cds/common'; - -entity Products : cuid, managed { - title : localized String(111); - descr : localized String(1111); - stock : Integer; - price : Decimal(9,2); - currency : Currency; - category : Association to Categories; -} - -entity Categories : CodeList { - key ID : Integer; - parent : Association to Categories; - children : Composition of many Categories on children.parent = $self; -} diff --git a/_attic/products-service/index.cds b/_attic/products-service/index.cds deleted file mode 100644 index 581fba09..00000000 --- a/_attic/products-service/index.cds +++ /dev/null @@ -1,2 +0,0 @@ -using from './db/schema'; -using from './srv/admin-service'; diff --git a/_attic/products-service/package.json b/_attic/products-service/package.json deleted file mode 100644 index 3415c423..00000000 --- a/_attic/products-service/package.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "name": "@sap/capire-products", - "version": "1.0.0", - "description": "A reuse package providing domain models and services to manage product catalogs.", - "repository": "https://github.com/SAP-samples/cloud-cap-samples.git", - "license": "SAP SAMPLE CODE LICENSE", - "dependencies": { - "@sap/cds": "latest", - "express": "*" - }, - "files": [ - "db", - "srv", - "index.cds" - ] -} \ No newline at end of file diff --git a/_attic/products-service/srv/admin-service.cds b/_attic/products-service/srv/admin-service.cds deleted file mode 100644 index a7ebd575..00000000 --- a/_attic/products-service/srv/admin-service.cds +++ /dev/null @@ -1,7 +0,0 @@ -using { sap.capire.products as db } from '../db/schema'; -namespace sap.capire.products; - -service AdminService @(_requires:'admin') { - entity Products as projection on db.Products; - entity Categories as projection on db.Categories; -} diff --git a/_attic/products-service/tests/data/sap.capire.products-Categories.csv b/_attic/products-service/tests/data/sap.capire.products-Categories.csv deleted file mode 100644 index 20183828..00000000 --- a/_attic/products-service/tests/data/sap.capire.products-Categories.csv +++ /dev/null @@ -1,10 +0,0 @@ -ID;parent_ID;name -0;;Some Sample Categories... -1;;Cat -2;1;Kitty -3;2;Kitty Cat -4;3;Aristocat -5;2;Kitty Bat -6;1;Catwoman -7;6;Catalina -8;;Catweazle diff --git a/_attic/products-service/tests/postman.json b/_attic/products-service/tests/postman.json deleted file mode 100644 index 6fbc8a8c..00000000 --- a/_attic/products-service/tests/postman.json +++ /dev/null @@ -1,157 +0,0 @@ -{ - "info": { - "_postman_id": "0f8d4e79-a1c2-47fe-aeab-0319fb4ce180", - "name": "@sap/capire-products", - "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" - }, - "item": [ - { - "name": "Categories", - "request": { - "method": "POST", - "header": [ - { - "key": "Content-Type", - "value": "application/json" - } - ], - "body": { - "mode": "raw", - "raw": "{ \"ID\":0, \"name\":\"Some Sample Categories...\", \"children\":[\n { \"ID\":1, \"name\":\"Cat\", \"children\":[\n { \"ID\":2, \"name\":\"Kitty\", \"children\":[\n { \"ID\":3, \"name\":\"Kitty Cat\", \"children\":[\n { \"ID\":4, \"name\":\"Aristocat\" }\n ]},\n { \"ID\":5, \"name\":\"Kitty Bat\" }\n ]},\n { \"ID\":6, \"name\":\"Catwoman\", \"children\":[\n { \"ID\":7, \"name\":\"Catalina\" }\n ]}\n ] },\n { \"ID\":8, \"name\":\"Catweazle\" }\n]}\n" - }, - "url": { - "raw": "http://localhost:4004/admin/Categories", - "protocol": "http", - "host": [ - "localhost" - ], - "port": "4004", - "path": [ - "admin", - "Categories" - ] - } - }, - "response": [] - }, - { - "name": "Categories", - "request": { - "method": "GET", - "header": [], - "body": { - "mode": "raw", - "raw": "" - }, - "url": { - "raw": "http://localhost:4004/admin/Categories/0?$expand=children($expand=children($expand=children($expand=children)))", - "protocol": "http", - "host": [ - "localhost" - ], - "port": "4004", - "path": [ - "admin", - "Categories", - "0" - ], - "query": [ - { - "key": "$expand", - "value": "children($expand=children($expand=children($expand=children)))" - } - ] - } - }, - "response": [ - { - "name": "Categories", - "originalRequest": { - "method": "GET", - "header": [], - "body": { - "mode": "raw", - "raw": "" - }, - "url": { - "raw": "http://localhost:4004/admin/Categories/0?$expand=children($expand=children($expand=children($expand=children)))", - "protocol": "http", - "host": [ - "localhost" - ], - "port": "4004", - "path": [ - "admin", - "Categories", - "0" - ], - "query": [ - { - "key": "$expand", - "value": "children($expand=children($expand=children($expand=children)))" - } - ] - } - }, - "status": "OK", - "code": 200, - "_postman_previewlanguage": "json", - "header": [ - { - "key": "X-Powered-By", - "value": "Express" - }, - { - "key": "OData-Version", - "value": "4.0" - }, - { - "key": "content-type", - "value": "application/json;odata.metadata=minimal" - }, - { - "key": "Date", - "value": "Tue, 21 May 2019 19:20:24 GMT" - }, - { - "key": "Connection", - "value": "keep-alive" - }, - { - "key": "Content-Length", - "value": "767" - } - ], - "cookie": [], - "body": "{\n \"@odata.context\": \"$metadata#cats(children(children(children(children()))))/$entity\",\n \"@odata.metadataEtag\": \"W/\\\"+AAp4JKNOcr+OusjrdQo55RCfM+UHKpTh8EbhsxyPhM=\\\"\",\n \"name\": \"Some Sample Categories...\",\n \"descr\": null,\n \"ID\": 0,\n \"parent_ID\": null,\n \"children\": [\n {\n \"name\": \"Cat\",\n \"descr\": null,\n \"ID\": 1,\n \"parent_ID\": 0,\n \"children\": [\n {\n \"name\": \"Kitty\",\n \"descr\": null,\n \"ID\": 2,\n \"parent_ID\": 1,\n \"children\": [\n {\n \"name\": \"Kitty Cat\",\n \"descr\": null,\n \"ID\": 3,\n \"parent_ID\": 2,\n \"children\": [\n {\n \"name\": \"Aristocat\",\n \"descr\": null,\n \"ID\": 4,\n \"parent_ID\": 3\n }\n ]\n },\n {\n \"name\": \"Kitty Bat\",\n \"descr\": null,\n \"ID\": 5,\n \"parent_ID\": 2,\n \"children\": []\n }\n ]\n },\n {\n \"name\": \"Catwoman\",\n \"descr\": null,\n \"ID\": 6,\n \"parent_ID\": 1,\n \"children\": [\n {\n \"name\": \"Catalina\",\n \"descr\": null,\n \"ID\": 7,\n \"parent_ID\": 6,\n \"children\": []\n }\n ]\n }\n ]\n },\n {\n \"name\": \"Catweazle\",\n \"descr\": null,\n \"ID\": 8,\n \"parent_ID\": 0,\n \"children\": []\n }\n ]\n}" - } - ] - }, - { - "name": "Categories", - "request": { - "method": "DELETE", - "header": [], - "body": { - "mode": "raw", - "raw": "" - }, - "url": { - "raw": "http://localhost:4004/admin/Categories/0", - "protocol": "http", - "host": [ - "localhost" - ], - "port": "4004", - "path": [ - "admin", - "Categories", - "0" - ] - } - }, - "response": [] - } - ] - -} \ No newline at end of file diff --git a/_attic/users-service/index.cds b/_attic/users-service/index.cds deleted file mode 100644 index 8b17f004..00000000 --- a/_attic/users-service/index.cds +++ /dev/null @@ -1 +0,0 @@ -using from './srv/services'; diff --git a/_attic/users-service/package.json b/_attic/users-service/package.json deleted file mode 100644 index e3f995b4..00000000 --- a/_attic/users-service/package.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "name": "@sap/capire-users", - "version": "1.0.0", - "description": "A reuse service for users-related functions, like registration, user profile mgmt, etc.", - "repository": "https://github.com/SAP-samples/cloud-cap-samples.git", - "license": "SAP SAMPLE CODE LICENSE", - "dependencies": { - "@sap/capire-contacts": "^1.0.0", - "@sap/cds": "latest", - "express": "*" - } -} diff --git a/_attic/users-service/srv/services.cds b/_attic/users-service/srv/services.cds deleted file mode 100644 index 05e5b9ac..00000000 --- a/_attic/users-service/srv/services.cds +++ /dev/null @@ -1,7 +0,0 @@ -using { sap.capire.contacts.Contacts as RegisteredUsers } from '@sap/capire-contacts'; -namespace sap.capire.users; - -service UsersService @(requires:'authenticated-user') { - entity MyProfile as select from RegisteredUsers; - action login (); -} \ No newline at end of file diff --git a/_attic/products-service/tests/categories.test.js b/bookshop/tests/genres.test.js similarity index 66% rename from _attic/products-service/tests/categories.test.js rename to bookshop/tests/genres.test.js index 8e541a4b..c4d295f3 100644 --- a/_attic/products-service/tests/categories.test.js +++ b/bookshop/tests/genres.test.js @@ -1,16 +1,18 @@ const cds = require ('@sap/cds') +const { expect } = require('chai').use (require('chai-deep-match')) describe('reading/writing hierarchies', ()=>{ - it ('should prepare to sqlite in-memory', async()=>{ + it ('should bootstrap sqlite in-memory db', async()=>{ await cds.deploy (__dirname+'/../db') .to ('sqlite::memory:') - expect (cds.model) .toBeDefined() + expect (cds.db) .to.exist + expect (cds.db.model) .to.exist }) - it ('should insert hierarchy of categories', ()=>{ - const { Categories } = cds.entities - return INSERT.into (Categories) .entries ( - { ID:100, name:'Some Sample Categories...', children:[ + it ('should insert hierarchy of genres', ()=>{ + const { Genres } = cds.entities + return INSERT.into (Genres) .entries ( + { ID:100, name:'Some Sample Genres...', children:[ { ID:101, name:'Cat', children:[ { ID:102, name:'Kitty', children:[ { ID:103, name:'Kitty Cat', children:[ @@ -23,17 +25,17 @@ describe('reading/writing hierarchies', ()=>{ ) }) - it ('should read categories with children', async()=>{ - const { Categories } = cds.entities + it ('should read genres with children', async()=>{ + const { Genres } = cds.entities expect (await - SELECT.one.from (Categories, c=>{ + SELECT.one.from (Genres, c=>{ c.ID, c.name.as('parent'), c.children (c=>{ c.name.as('child') }) }) .where ({name:'Cat'}) - ) .toMatchObject ( + ) .to.deep.match ( { ID:101, parent:'Cat', children:[ { child:'Kitty' }, @@ -43,15 +45,15 @@ describe('reading/writing hierarchies', ()=>{ ) }) - it ('should read hierarchy of categories', async()=>{ - const { Categories } = cds.entities + it ('should read hierarchy of genres', async()=>{ + const { Genres } = cds.entities expect (await - SELECT.one.from (Categories, c=>{ + SELECT.one.from (Genres, c=>{ c.ID, c.name, c.children (c=>{ c.name },{levels:3}) }) .where ({name:'Cat'}) - ) .toMatchObject ( + ) .to.deep.match ( { ID:101, name:'Cat', children:[ { name:'Kitty', children:[ diff --git a/events/package.json b/events/package.json new file mode 100644 index 00000000..898be68e --- /dev/null +++ b/events/package.json @@ -0,0 +1,21 @@ +{ + "name": "@capire/bookshop-with-reviews", + "version": "1.0.0", + "dependencies": { + "@capire/bookshop": "*", + "@capire/reviews": "*" + }, + "cds": { + "requires": { + "db": { + "kind": "sql" + }, + "sap.capire.reviews.ReviewsService": { + "kind": "odata" + }, + "messaging": { + "kind": "file-based-messaging" + } + } + } +} \ No newline at end of file diff --git a/events/services.cds b/events/services.cds new file mode 100644 index 00000000..6cf3bd70 --- /dev/null +++ b/events/services.cds @@ -0,0 +1,13 @@ +namespace sap.capire.bookshop; +using { sap.capire.reviews.ReviewsService as external } from '@capire/reviews'; +using { sap.capire.bookshop.Books } from '@capire/bookshop/db/schema'; + +// Extending Books by Reviews +extend Books with { + reviews : Composition of many external.Reviews on reviews.subject = ID; + rating : external.Reviews.rating; +} + +using from '@capire/bookshop/srv/admin-service'; +using from '@capire/bookshop/srv/cat-service'; +annotate AdminService with @impl:'services.js'; diff --git a/_attic/bookshop-enhanced/srv/services.js b/events/services.js similarity index 92% rename from _attic/bookshop-enhanced/srv/services.js rename to events/services.js index 24503d49..261c3e9f 100644 --- a/_attic/bookshop-enhanced/srv/services.js +++ b/events/services.js @@ -2,15 +2,17 @@ const cds = require ('@sap/cds') module.exports = cds.service.impl (async()=>{ + // connect to requires services const ReviewsService = await cds.connect.to ('sap.capire.reviews.ReviewsService') const CatalogService = await cds.connect.to ('CatalogService') const db = await cds.connect.to ('db') + // import model definitions from connected services to work with subsequently const { Books } = db.entities const { Reviews } = ReviewsService.entities + // delegate requests to read reviews to ReviewsService CatalogService.impl (srv => { - // delegate requests to read reviews to ReviewsService srv.on ('READ', 'Books/reviews', (req) => { const [ subject ] = req.params const tx = ReviewsService.transaction (req) diff --git a/events/tests.http b/events/tests.http new file mode 100644 index 00000000..1476f53f --- /dev/null +++ b/events/tests.http @@ -0,0 +1,40 @@ +################################################# +# +# Reviews Service +# + + +### Request to CatalogService > delegated to ReviewsService +GET http://localhost:4004/browse/Books(201)/reviews + +### Alternative OData URL +GET http://localhost:4004/browse/Books/201/reviews + +### +GET http://localhost:4004/browse/Books(201)? +&$select=ID,title,rating +&$expand=reviews +# Note: the latter only works in case of ReviewsService in same process + + + +### ReviewsService mocked in same process +GET http://localhost:4004/reviews/Reviews? + +### +POST http://localhost:4004/reviews/Reviews +Content-Type: application/json;IEEE754Compatible=true + +{"subject":"201", "rating":"5", "title":"boo"} + + + + +### ReviewsService running as separate process +GET http://localhost:5005/reviews/Reviews? + +### +POST http://localhost:5005/reviews/Reviews +Content-Type: application/json;IEEE754Compatible=true + +{"subject":"201", "rating":"5", "title":"boo"} diff --git a/_attic/media-server/db/data-model.cds b/media/db/data-model.cds similarity index 100% rename from _attic/media-server/db/data-model.cds rename to media/db/data-model.cds diff --git a/_attic/media-server/index.cds b/media/index.cds similarity index 100% rename from _attic/media-server/index.cds rename to media/index.cds diff --git a/_attic/media-server/package.json b/media/package.json similarity index 85% rename from _attic/media-server/package.json rename to media/package.json index c144037d..9453255f 100644 --- a/_attic/media-server/package.json +++ b/media/package.json @@ -1,12 +1,10 @@ { - "name": "@sap/capire-media", + "name": "@capire/media", "version": "1.0.0", "description": "A generic platform service to manage and serve media content on behalf of other services and apps.", "repository": "https://github.com/SAP-samples/cloud-cap-samples.git", "license": "SAP SAMPLE CODE LICENSE", "dependencies": { - "@sap/cds": "latest", - "express": "*", "lokijs": "^1.5.6" }, "files": [ diff --git a/_attic/media-server/srv/media-service.cds b/media/srv/media-service.cds similarity index 100% rename from _attic/media-server/srv/media-service.cds rename to media/srv/media-service.cds diff --git a/_attic/media-server/srv/media-service.js b/media/srv/media-service.js similarity index 100% rename from _attic/media-server/srv/media-service.js rename to media/srv/media-service.js diff --git a/package.json b/package.json index dfe31f95..bd5aeee5 100644 --- a/package.json +++ b/package.json @@ -5,17 +5,22 @@ "author": "daniel.hutzel@sap.com", "private": true, "scripts": { - "bookshop": "cds watch bookshop", - "bookshop-fiori": "cds watch fiori" + "bookshop-fiori": "cds watch fiori", + "bookshop": "cds watch bookshop" }, "dependencies": { "@capire/bookshop": "file:bookshop", "@capire/common": "file:common", + "@capire/fiori": "file:fiori", "@capire/reviews": "file:reviews" }, "add-these-to-devDependencies-for-tests": { "@types/jest": "*", "jest": "*" }, - "license": "SAP SAMPLE CODE LICENSE" + "license": "SAP SAMPLE CODE LICENSE", + "devDependencies": { + "chai": "^4.2.0", + "chai-deep-match": "^1.2.1" + } } diff --git a/reviews/.env b/reviews/.env new file mode 100644 index 00000000..378d6075 --- /dev/null +++ b/reviews/.env @@ -0,0 +1 @@ +PORT = 5005 \ No newline at end of file diff --git a/_attic/bookshop-enhanced/tests/reviews.http b/reviews/tests/requests.http similarity index 100% rename from _attic/bookshop-enhanced/tests/reviews.http rename to reviews/tests/requests.http