Skip to content

Commit

Permalink
Fix flight manager
Browse files Browse the repository at this point in the history
  • Loading branch information
ghareeb-falazi committed Nov 24, 2024
1 parent e57cfff commit d400ecf
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 10 deletions.
29 changes: 21 additions & 8 deletions lib/basic-flight-booking-manager/basic-flight-booking-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ class BasicFlightBookingManager extends Contract {
return new BasicFlightBookingManagerContext();
}

/**
*
* @param {BasicFlightBookingManagerContext} ctx
*/
async InitLedger(ctx) {
await this.createFlight(ctx, "FLY-1", 200, 500);
}

/**
*
* @param {BasicFlightBookingManagerContext} ctx
Expand Down Expand Up @@ -101,8 +109,6 @@ class BasicFlightBookingManager extends Contract {
return -1;
}



/**
*
* @param {BasicFlightBookingManagerContext} ctx
Expand All @@ -127,6 +133,10 @@ class BasicFlightBookingManager extends Contract {
return booking != null && booking != undefined;
}

/**
*
* @param {BasicFlightBookingManagerContext} ctx
*/
async queryClientBalance(ctx) {
let client = ctx.clientIdentity.getID();
let clientBalance = await ctx.clientBalances.getClientBalance(client);
Expand Down Expand Up @@ -214,16 +224,19 @@ class BasicFlightBookingManager extends Contract {
async bookSeat(ctx, flightId, seatNumber) {
let clientId = ctx.clientIdentity.getID();
let isSeatAvailable = await this.isSeatAvailable(ctx, flightId, seatNumber);
let hasReservation = await this.hasReservation(ctx, flightId, clientId);
let balance = await this.queryClientBalance(ctx);
let price = await this.querySeatPrice(ctx, flightId);

if(isSeatAvailable && !hasReservation && balance >= price) {
await this._deductFromClientBalance(ctx, price);
let booking = Booking.createInstance(clientId, flightId, seatNumber);
await ctx.bookingList.addBooking(booking);
if(isSeatAvailable) {
if (balance >= price) {
await this._deductFromClientBalance(ctx, price);
let booking = Booking.createInstance(clientId, flightId, seatNumber);
await ctx.bookingList.addBooking(booking);
} else {
throw new Error('Cannot reserve the seat: insufficient client balance!');
}
} else {
throw new Error('Cannot reserve the seat!');
throw new Error('Cannot reserve the seat: the seat is unavailable!');
}
}

Expand Down
4 changes: 4 additions & 0 deletions lib/basic-flight-booking-manager/booking-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

'use strict';

const crypto = require('crypto');
const StateList = require('../../ledger-api/statelist');
const Booking = require('./booking');

Expand Down Expand Up @@ -44,6 +45,9 @@ class BookingList extends StateList {
* @returns {Promise<Booking>}
*/
async getBooking(clientId, flightId) {
clientId = crypto.createHash('sha256')
.update(clientId)
.digest('hex');
let key = Booking.makeKey([clientId, flightId]);
if (this.cache.has(key)) {
return this.cache.get(key);
Expand Down
4 changes: 4 additions & 0 deletions lib/basic-flight-booking-manager/booking.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'use strict';

const State = require('../../ledger-api/state');
const crypto = require('crypto');

class Booking extends State {

Expand Down Expand Up @@ -71,6 +72,9 @@ class Booking extends State {
* @param {number} seatNumber
*/
static createInstance(clientId, flightId, seatNumber) {
clientId = crypto.createHash('sha256')
.update(clientId)
.digest('hex');
return new Booking({ id: Booking.makeKey([clientId, flightId]), clientId: clientId, flightId: flightId, seatNumber: seatNumber});
}

Expand Down
9 changes: 8 additions & 1 deletion lib/basic-flight-booking-manager/client-balance-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

'use strict';

const crypto = require('crypto');
const StateList = require('../../ledger-api/statelist');
const ClientBalance = require('./client-balance');

Expand Down Expand Up @@ -43,14 +44,20 @@ class ClientBalanceList extends StateList {
* @returns {Promise<ClientBalance>}
*/
async getClientBalance(client) {
client = crypto.createHash('sha256')
.update(client)
.digest('hex');

console.log("client=" + client);

if (this.cache.has(client)) {
return this.cache.get(client);
}

let key = ClientBalance.makeKey([client]);
let result = await this.getState(key);

return result ? ClientBalance.fromBuffer(result) : null;
return result ? ClientBalance.fromBuffer(result) : ClientBalance.createInstance(client, "2000");
}

/**
Expand Down
8 changes: 7 additions & 1 deletion lib/basic-flight-booking-manager/client-balance.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

'use strict';



const crypto = require('crypto');
const State = require('../../ledger-api/state');

class ClientBalance extends State {
Expand Down Expand Up @@ -67,7 +70,10 @@ class ClientBalance extends State {
* @param {String} balance
*/
static createInstance(client, balance) {
return new ClientBalance({ client: client, balance: balance });
const hash = crypto.createHash('sha256')
.update(client)
.digest('hex');
return new ClientBalance({ client: hash, balance: balance });
}

}
Expand Down
4 changes: 4 additions & 0 deletions lib/basic-flight-booking-manager/flight-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class FlightList extends StateList {
* @returns {Promise<void>}
*/
async addFlight(element) {
console.debug("adding flight with id: " + element.getId());
await this.addState(element);
this.cache.set(element.getId(), element);
}
Expand All @@ -43,12 +44,15 @@ class FlightList extends StateList {
* @returns {Promise<Flight>}
*/
async getFlight(id) {
console.debug("getFlight(" + id + ")");

if (this.cache.has(id)) {
return this.cache.get(id);
}

let key = Flight.makeKey([id]);
let result = await this.getState(key);
console.debug("get Flight result = " + result ? Flight.fromBuffer(result) : "null");

return result ? Flight.fromBuffer(result) : null;
}
Expand Down

0 comments on commit d400ecf

Please sign in to comment.