add getMyTracks option
This commit is contained in:
committed by
Daniel Hutzel
parent
99d4da34d7
commit
a917dac1b2
@@ -69,6 +69,8 @@ entity Invoices {
|
||||
billingCountry : String(40);
|
||||
billingPostalCode : String(40);
|
||||
total : Decimal(10, 2);
|
||||
invoiceItems : Association to many InvoiceItems
|
||||
on invoiceItems.invoice = $self;
|
||||
}
|
||||
|
||||
entity InvoiceItems {
|
||||
@@ -90,5 +92,5 @@ entity Tracks {
|
||||
bytes : Integer;
|
||||
unitPrice : Decimal(10, 2);
|
||||
invoices : Association to many InvoiceItems
|
||||
on invoices.track.ID = $self.ID
|
||||
on invoices.track = $self
|
||||
}
|
||||
|
||||
@@ -37,7 +37,8 @@
|
||||
"content-creator"
|
||||
],
|
||||
"userAttributes": {
|
||||
"level": 5
|
||||
"level": 5,
|
||||
"ID": 8
|
||||
}
|
||||
},
|
||||
"admin2": {
|
||||
@@ -45,7 +46,8 @@
|
||||
"admin"
|
||||
],
|
||||
"userAttributes": {
|
||||
"level": 2
|
||||
"level": 2,
|
||||
"ID": 7
|
||||
}
|
||||
},
|
||||
"content-creator1": {
|
||||
@@ -53,7 +55,8 @@
|
||||
"content-creator"
|
||||
],
|
||||
"userAttributes": {
|
||||
"level": 2
|
||||
"level": 2,
|
||||
"ID": 5
|
||||
}
|
||||
},
|
||||
"user1": {
|
||||
@@ -61,7 +64,8 @@
|
||||
"user"
|
||||
],
|
||||
"userAttributes": {
|
||||
"level": 1
|
||||
"level": 1,
|
||||
"ID": 2
|
||||
}
|
||||
},
|
||||
"user0": {
|
||||
@@ -70,7 +74,7 @@
|
||||
],
|
||||
"userAttributes": {
|
||||
"level": 1,
|
||||
"email": "hholy@gmail.com"
|
||||
"ID": 3
|
||||
}
|
||||
},
|
||||
"*": true
|
||||
@@ -78,4 +82,4 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,24 +1,10 @@
|
||||
using {sap.capire.media.store as my} from '../db/schema';
|
||||
|
||||
@cds.query.limit : {
|
||||
default : 12,
|
||||
max : 100
|
||||
}
|
||||
@(requires : ['identified-user'])
|
||||
service BrowseTracks {
|
||||
@readonly
|
||||
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
|
||||
entity Genres as projection on my.Genres;
|
||||
}
|
||||
|
||||
@@ -1,35 +1,8 @@
|
||||
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 () {
|
||||
const db = await cds.connect.to("db"); // connect to database service
|
||||
|
||||
// this.before("*", (req) => {
|
||||
// req.user = new Buyer();
|
||||
// });
|
||||
const { Invoices } = db.entities;
|
||||
|
||||
this.before("*", (req) => {
|
||||
console.log(
|
||||
@@ -42,10 +15,20 @@ module.exports = async function () {
|
||||
);
|
||||
});
|
||||
|
||||
this.on("getInvoicedTracks", async (req) => {
|
||||
const user = req.user;
|
||||
user.is("user") || req.reject(403);
|
||||
const query = cds.parse.cql(selectInvoicedTracksByEmail(user.attr.email));
|
||||
return await db.run(query);
|
||||
this.on("READ", "Tracks", async (req, next) => {
|
||||
if (!!req._query && "my" in req._query) {
|
||||
const myTrackEntries = await db.run(
|
||||
SELECT.from(Invoices)
|
||||
.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();
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
using {sap.capire.media.store as my} from '../db/schema';
|
||||
|
||||
@cds.query.limit : {
|
||||
default : 20,
|
||||
max : 100
|
||||
}
|
||||
service MediaService {
|
||||
entity Employees as projection on my.Employees;
|
||||
entity Customers as projection on my.Customers;
|
||||
|
||||
Reference in New Issue
Block a user