add getMyTracks option

This commit is contained in:
Dzmitry_Tamashevich@epam.com
2020-10-13 11:23:00 +03:00
committed by Daniel Hutzel
parent 99d4da34d7
commit a917dac1b2
5 changed files with 29 additions and 58 deletions

View File

@@ -69,6 +69,8 @@ entity Invoices {
billingCountry : String(40); billingCountry : String(40);
billingPostalCode : String(40); billingPostalCode : String(40);
total : Decimal(10, 2); total : Decimal(10, 2);
invoiceItems : Association to many InvoiceItems
on invoiceItems.invoice = $self;
} }
entity InvoiceItems { entity InvoiceItems {
@@ -90,5 +92,5 @@ entity Tracks {
bytes : Integer; bytes : Integer;
unitPrice : Decimal(10, 2); unitPrice : Decimal(10, 2);
invoices : Association to many InvoiceItems invoices : Association to many InvoiceItems
on invoices.track.ID = $self.ID on invoices.track = $self
} }

View File

@@ -37,7 +37,8 @@
"content-creator" "content-creator"
], ],
"userAttributes": { "userAttributes": {
"level": 5 "level": 5,
"ID": 8
} }
}, },
"admin2": { "admin2": {
@@ -45,7 +46,8 @@
"admin" "admin"
], ],
"userAttributes": { "userAttributes": {
"level": 2 "level": 2,
"ID": 7
} }
}, },
"content-creator1": { "content-creator1": {
@@ -53,7 +55,8 @@
"content-creator" "content-creator"
], ],
"userAttributes": { "userAttributes": {
"level": 2 "level": 2,
"ID": 5
} }
}, },
"user1": { "user1": {
@@ -61,7 +64,8 @@
"user" "user"
], ],
"userAttributes": { "userAttributes": {
"level": 1 "level": 1,
"ID": 2
} }
}, },
"user0": { "user0": {
@@ -70,7 +74,7 @@
], ],
"userAttributes": { "userAttributes": {
"level": 1, "level": 1,
"email": "hholy@gmail.com" "ID": 3
} }
}, },
"*": true "*": true
@@ -78,4 +82,4 @@
} }
} }
} }
} }

View File

@@ -1,24 +1,10 @@
using {sap.capire.media.store as my} from '../db/schema'; using {sap.capire.media.store as my} from '../db/schema';
@cds.query.limit : {
default : 12,
max : 100
}
@(requires : ['identified-user']) @(requires : ['identified-user'])
service BrowseTracks { service BrowseTracks {
@readonly @readonly
entity Tracks as projection on my.Tracks; entity Tracks as projection on my.Tracks;
@(requires : ['authenticated-user'])
function getInvoicedTracks() returns array of {
ID : Tracks.ID;
trackName : Tracks.name;
genreName : my.Genres.name;
composer : Tracks.composer;
unitPrice : Tracks.unitPrice;
albumTitle : my.Albums.title;
};
@readonly @readonly
entity Genres as projection on my.Genres; entity Genres as projection on my.Genres;
} }

View File

@@ -1,35 +1,8 @@
const cds = require("@sap/cds"); const cds = require("@sap/cds");
const selectInvoicedTracksByEmail = (email) => `
select
tracks.ID,
tracks.name trackName,
tracks.composer,
tracks.unitPrice,
genre.name genreName,
album.title albumTitle
from sap_capire_media_store_Tracks tracks
join sap_capire_media_store_InvoiceItems invoiceItem
on invoiceItem.track_ID = tracks.ID
join sap_capire_media_store_Invoices invoice
on invoice.ID = invoiceItem.invoice_ID
join sap_capire_media_store_Customers customer
on customer.ID = invoice.customer_ID
join sap_capire_media_store_Genres genre
on genre.ID = tracks.genre_ID
join sap_capire_media_store_Albums album
on album.ID = tracks.album_ID
where
customer.email = '${email}'
`;
module.exports = async function () { module.exports = async function () {
const db = await cds.connect.to("db"); // connect to database service const db = await cds.connect.to("db"); // connect to database service
const { Invoices } = db.entities;
// this.before("*", (req) => {
// req.user = new Buyer();
// });
this.before("*", (req) => { this.before("*", (req) => {
console.log( console.log(
@@ -42,10 +15,20 @@ module.exports = async function () {
); );
}); });
this.on("getInvoicedTracks", async (req) => { this.on("READ", "Tracks", async (req, next) => {
const user = req.user; if (!!req._query && "my" in req._query) {
user.is("user") || req.reject(403); const myTrackEntries = await db.run(
const query = cds.parse.cql(selectInvoicedTracksByEmail(user.attr.email)); SELECT.from(Invoices)
return await db.run(query); .columns("ID")
.where({ customer_ID: req.user.attr.ID })
);
const myTrackIdsSequence = myTrackEntries.map(({ ID }) => ID).join();
const condition = cds.parse.expr(`ID in (${myTrackIdsSequence})`);
const query = SELECT.from(req.query).where(condition);
const result = await db.run(query);
result.$count = result.length;
return result;
}
return next();
}); });
}; };

View File

@@ -1,9 +1,5 @@
using {sap.capire.media.store as my} from '../db/schema'; using {sap.capire.media.store as my} from '../db/schema';
@cds.query.limit : {
default : 20,
max : 100
}
service MediaService { service MediaService {
entity Employees as projection on my.Employees; entity Employees as projection on my.Employees;
entity Customers as projection on my.Customers; entity Customers as projection on my.Customers;