Skip to content

Commit

Permalink
Fix basic 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 f17576a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
17 changes: 14 additions & 3 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,15 @@ class BasicFlightBookingManager extends Contract {
return new BasicFlightBookingManagerContext();
}

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

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



/**
*
* @param {BasicFlightBookingManagerContext} ctx
Expand All @@ -127,6 +134,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,7 +225,7 @@ 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 hasReservation = await this.hasReservation(ctx, flightId);
let balance = await this.queryClientBalance(ctx);
let price = await this.querySeatPrice(ctx, flightId);

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
7 changes: 7 additions & 0 deletions 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,6 +44,12 @@ 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);
}
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

0 comments on commit f17576a

Please sign in to comment.