Skip to content

Commit

Permalink
feat: place bid service
Browse files Browse the repository at this point in the history
  • Loading branch information
dorucioclea committed Jul 17, 2023
1 parent 102dbe0 commit b832c35
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Action } from "./action.model";
import { Highestbidder } from "./highest-bidder.model";

export interface CancelBid {
export interface CancelAuction {
action: Action;
'auction-id': Action;
'end-block': Action;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Inject, Injectable } from "@nestjs/common";
import { StartAuction } from "src/models";
import { CancelAuction, StartAuction } from "src/models";
import { AuctionEntityRepository } from "src/modules/auctions/repositories/auction.repository";
import { AuctionEntityRepositoryToken } from "src/modules/auctions/providers/auction.repository.provider";
import { Transactional } from "src/common/transaction/transaction";
import { runOnTransactionComplete, runOnTransactionRollback } from "src/common/transaction/hook";
import { AuctionEntity } from "src/modules/auctions/entities/auction.entity";
import { AuctionStatus } from "src/modules/auctions/entities/auction.status";

@Injectable()
export class CancelAuctionService {
Expand All @@ -14,17 +15,20 @@ export class CancelAuctionService {
) {}

@Transactional()
public async startAuction(startAuctionModel: StartAuction): Promise<void> {
public async cancelAuction(cancelAuctionModel: CancelAuction): Promise<void> {

runOnTransactionRollback((cb) =>
console.log('Rollback error ' + cb.message),
);

runOnTransactionComplete((_) => console.log('Transaction Complete'));

const auction = await this.auctionRepository.findOneOrFail({auctionId: Number(startAuctionModel["auction-id"].value)})

auction.status = startAuctionModel["auction-status"].value;
const auction = await this.auctionRepository.findOneOrFail({
where: { auctionId: Number(cancelAuctionModel["auction-id"].value)},
relations: ['bids'],
});

auction.status = AuctionStatus.CANCELLED;

await this.auctionRepository.save(auction);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Inject, Injectable } from "@nestjs/common";
import { PlaceBid, StartAuction } from "src/models";
import { AuctionEntityRepository } from "src/modules/auctions/repositories/auction.repository";
import { AuctionEntityRepositoryToken } from "src/modules/auctions/providers/auction.repository.provider";
import { Transactional } from "src/common/transaction/transaction";
import { runOnTransactionComplete, runOnTransactionRollback } from "src/common/transaction/hook";
import { AuctionBidEntity } from "src/modules/auctions/entities/auction.bid.entity";
import { AuctionBidEntityRepositoryToken } from "src/modules/auctions/providers/auction.bid.repository.provider";
import { AuctionBidEntityRepository } from "src/modules/auctions/repositories/auction.bid.entity.repository";

@Injectable()
export class PlaceBidService {
constructor(
@Inject(AuctionEntityRepositoryToken)
private auctionRepository: AuctionEntityRepository,
@Inject(AuctionBidEntityRepositoryToken)
private auctionBidRepository: AuctionBidEntityRepository
) {}

@Transactional()
public async placeBid(placeBidModel: PlaceBid): Promise<void> {

runOnTransactionRollback((cb) =>
console.log('Rollback error ' + cb.message),
);

runOnTransactionComplete((_) => console.log('Transaction Complete'));

const auction = await this.auctionRepository.findOneOrFail({
where: { auctionId: Number(placeBidModel["auction-id"].value)},
relations: ['bids'],
});

const bid = new AuctionBidEntity();
bid.amount = Number(placeBidModel.bid.value);
bid.bidder = placeBidModel.bidder.value;

await this.auctionBidRepository.save(bid);

(auction.bids || []).push(bid);

await this.auctionRepository.save(auction);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AuctionEntityRepositoryToken } from "src/modules/auctions/providers/auc
import { Transactional } from "src/common/transaction/transaction";
import { runOnTransactionComplete, runOnTransactionRollback } from "src/common/transaction/hook";
import { AuctionEntity } from "src/modules/auctions/entities/auction.entity";
import { AuctionStatus } from "src/modules/auctions/entities/auction.status";

@Injectable()
export class StartAuctionService {
Expand All @@ -25,7 +26,7 @@ export class StartAuctionService {
const auction = new AuctionEntity();

auction.auctionId = Number(startAuctionModel["auction-id"].value);
auction.status = startAuctionModel["auction-status"].value;
auction.status = AuctionStatus.OPEN;
auction.endBlock = startAuctionModel["end-block"].value;
auction.highestBid = startAuctionModel["highest-bid"].value;
auction.highestBidder = startAuctionModel["highest-bidder"].value.value;
Expand Down

0 comments on commit b832c35

Please sign in to comment.