Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
"start": "cds-serve"
},
"dependencies": {
"@cap-js/hana": ">=2.1.2",
"@sap/cds": ">=9"
},
"devDependencies": {
"@cap-js/sqlite": ">=2"
},
"cds": {
"requires": {
"messaging": true
}
},
"license": "Apache-2.0"
}
3 changes: 2 additions & 1 deletion srv/data-service.cds
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ service FlightsService {
// Custom actions and events to sync with consumers about flight seat availability
action BookingCreated ( flight: Flights:ID, date: Flights:date, seats: array of Integer);
action BookingDeleted ( flight: Flights:ID, date: Flights:date, seats: array of Integer);
event FlightsUpdated { flight: Flights:ID; date: Flights:date; free_seats: Integer; }
// Restrict payload to identity as messages can overtake each other in the ether
event FlightsUpdated { flight: Flights:ID; date: Flights:date; };
}


Expand Down
12 changes: 8 additions & 4 deletions srv/data-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ class DataService extends cds.ApplicationService { init() {

this.on ('BookingCreated', async req => {
const { flight, date, seats = [null] } = req.data
await UPDATE (Flights, { flight_ID:flight, date })
.set `occupied_seats = occupied_seats + ${seats.length}`
const confirmed = await UPDATE (Flights, { flight_ID:flight, date })
.set `occupied_seats = occupied_seats + ${seats.length}`
.where `free_seats >= ${seats.length}`
if (!confirmed) req.reject('Flight is fully booked')
this.emit('FlightsUpdated', { flight, date })
})

this.on ('BookingDeleted', async (req) => {
const { flight, date, seats = [null] } = req.data
await UPDATE (Flights, { flight_ID:flight, date })
.set `occupied_seats = occupied_seats - ${seats.length}`
await UPDATE (Flights, { flight_ID:flight, date })
.set `occupied_seats = occupied_seats - ${seats.length}`
this.emit('FlightsUpdated', { flight, date })
})

return super.init()
Expand Down