|
| 1 | +import { |
| 2 | + Body, |
| 3 | + Controller, |
| 4 | + Delete, |
| 5 | + Get, |
| 6 | + NotFoundException, |
| 7 | + Param, |
| 8 | + Post, |
| 9 | + Query, |
| 10 | + BadRequestException, |
| 11 | + ForbiddenException, |
| 12 | + ConflictException, |
| 13 | +} from '@nestjs/common'; |
| 14 | +import { |
| 15 | + ApiBody, |
| 16 | + ApiOperation, |
| 17 | + ApiParam, |
| 18 | + ApiQuery, |
| 19 | + ApiResponse, |
| 20 | + ApiTags, |
| 21 | +} from '@nestjs/swagger'; |
| 22 | + |
| 23 | +import { MarketplaceService } from './marketplace.service'; |
| 24 | +import { ListUsernameDto, PlaceBidDto, AcceptBidDto, CancelListingDto } from './dto'; |
| 25 | +import { MarketplaceError, MarketplaceErrorCode } from './errors'; |
| 26 | + |
| 27 | +@ApiTags('marketplace') |
| 28 | +@Controller('marketplace') |
| 29 | +export class MarketplaceController { |
| 30 | + constructor(private readonly marketplaceService: MarketplaceService) {} |
| 31 | + |
| 32 | + @Post('list') |
| 33 | + @ApiOperation({ summary: 'List a username for sale' }) |
| 34 | + @ApiBody({ type: ListUsernameDto }) |
| 35 | + @ApiResponse({ status: 201, description: 'Listing created' }) |
| 36 | + @ApiResponse({ status: 400, description: 'Invalid input' }) |
| 37 | + @ApiResponse({ status: 403, description: 'Username not owned by this wallet' }) |
| 38 | + @ApiResponse({ status: 409, description: 'Username already listed' }) |
| 39 | + async listUsername(@Body() body: ListUsernameDto) { |
| 40 | + try { |
| 41 | + const listing = await this.marketplaceService.listUsername( |
| 42 | + body.username, |
| 43 | + body.sellerPublicKey, |
| 44 | + body.askingPrice, |
| 45 | + ); |
| 46 | + return { listing }; |
| 47 | + } catch (err) { |
| 48 | + if (err instanceof MarketplaceError) { |
| 49 | + this.throwHttp(err); |
| 50 | + } |
| 51 | + throw err; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @Get() |
| 56 | + @ApiOperation({ summary: 'Get all active listings' }) |
| 57 | + @ApiQuery({ name: 'limit', required: false, example: 20 }) |
| 58 | + @ApiQuery({ name: 'offset', required: false, example: 0 }) |
| 59 | + @ApiResponse({ status: 200, description: 'List of active listings' }) |
| 60 | + async getActiveListings( |
| 61 | + @Query('limit') limit = 20, |
| 62 | + @Query('offset') offset = 0, |
| 63 | + ) { |
| 64 | + const { listings, total } = await this.marketplaceService.getActiveListings( |
| 65 | + Number(limit), |
| 66 | + Number(offset), |
| 67 | + ); |
| 68 | + return { listings, total }; |
| 69 | + } |
| 70 | + |
| 71 | + @Get(':listingId') |
| 72 | + @ApiOperation({ summary: 'Get a specific listing' }) |
| 73 | + @ApiParam({ name: 'listingId', description: 'Listing UUID' }) |
| 74 | + @ApiResponse({ status: 200, description: 'Listing details' }) |
| 75 | + @ApiResponse({ status: 404, description: 'Listing not found' }) |
| 76 | + async getListing(@Param('listingId') listingId: string) { |
| 77 | + try { |
| 78 | + const listing = await this.marketplaceService.getListing(listingId); |
| 79 | + return { listing }; |
| 80 | + } catch (err) { |
| 81 | + if (err instanceof MarketplaceError) { |
| 82 | + this.throwHttp(err); |
| 83 | + } |
| 84 | + throw err; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Delete(':listingId') |
| 89 | + @ApiOperation({ summary: 'Cancel a listing' }) |
| 90 | + @ApiParam({ name: 'listingId', description: 'Listing UUID' }) |
| 91 | + @ApiBody({ type: CancelListingDto }) |
| 92 | + @ApiResponse({ status: 200, description: 'Listing cancelled' }) |
| 93 | + @ApiResponse({ status: 403, description: 'Not the seller' }) |
| 94 | + @ApiResponse({ status: 404, description: 'Listing not found' }) |
| 95 | + async cancelListing( |
| 96 | + @Param('listingId') listingId: string, |
| 97 | + @Body() body: CancelListingDto, |
| 98 | + ) { |
| 99 | + try { |
| 100 | + await this.marketplaceService.cancelListing(listingId, body.sellerPublicKey); |
| 101 | + return { ok: true }; |
| 102 | + } catch (err) { |
| 103 | + if (err instanceof MarketplaceError) { |
| 104 | + this.throwHttp(err); |
| 105 | + } |
| 106 | + throw err; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Post(':listingId/bid') |
| 111 | + @ApiOperation({ summary: 'Place a bid on a listing' }) |
| 112 | + @ApiParam({ name: 'listingId', description: 'Listing UUID' }) |
| 113 | + @ApiBody({ type: PlaceBidDto }) |
| 114 | + @ApiResponse({ status: 201, description: 'Bid placed' }) |
| 115 | + @ApiResponse({ status: 400, description: 'Seller cannot bid on own listing' }) |
| 116 | + @ApiResponse({ status: 404, description: 'Listing not found' }) |
| 117 | + async placeBid( |
| 118 | + @Param('listingId') listingId: string, |
| 119 | + @Body() body: PlaceBidDto, |
| 120 | + ) { |
| 121 | + try { |
| 122 | + const bid = await this.marketplaceService.placeBid( |
| 123 | + listingId, |
| 124 | + body.bidderPublicKey, |
| 125 | + body.bidAmount, |
| 126 | + ); |
| 127 | + return { bid }; |
| 128 | + } catch (err) { |
| 129 | + if (err instanceof MarketplaceError) { |
| 130 | + this.throwHttp(err); |
| 131 | + } |
| 132 | + throw err; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + @Get(':listingId/bids') |
| 137 | + @ApiOperation({ summary: 'Get all bids for a listing' }) |
| 138 | + @ApiParam({ name: 'listingId', description: 'Listing UUID' }) |
| 139 | + @ApiResponse({ status: 200, description: 'List of bids' }) |
| 140 | + @ApiResponse({ status: 404, description: 'Listing not found' }) |
| 141 | + async getBids(@Param('listingId') listingId: string) { |
| 142 | + try { |
| 143 | + const bids = await this.marketplaceService.getBids(listingId); |
| 144 | + return { bids }; |
| 145 | + } catch (err) { |
| 146 | + if (err instanceof MarketplaceError) { |
| 147 | + this.throwHttp(err); |
| 148 | + } |
| 149 | + throw err; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @Post(':listingId/accept-bid/:bidId') |
| 154 | + @ApiOperation({ summary: 'Accept a bid — atomically transfers username ownership' }) |
| 155 | + @ApiParam({ name: 'listingId', description: 'Listing UUID' }) |
| 156 | + @ApiParam({ name: 'bidId', description: 'Bid UUID' }) |
| 157 | + @ApiBody({ type: AcceptBidDto }) |
| 158 | + @ApiResponse({ status: 200, description: 'Bid accepted and ownership transferred' }) |
| 159 | + @ApiResponse({ status: 400, description: 'Bid not pending or listing not active' }) |
| 160 | + @ApiResponse({ status: 403, description: 'Not the seller' }) |
| 161 | + @ApiResponse({ status: 404, description: 'Listing or bid not found' }) |
| 162 | + async acceptBid( |
| 163 | + @Param('listingId') listingId: string, |
| 164 | + @Param('bidId') bidId: string, |
| 165 | + @Body() body: AcceptBidDto, |
| 166 | + ) { |
| 167 | + try { |
| 168 | + await this.marketplaceService.acceptBid(listingId, bidId, body.sellerPublicKey); |
| 169 | + return { ok: true }; |
| 170 | + } catch (err) { |
| 171 | + if (err instanceof MarketplaceError) { |
| 172 | + this.throwHttp(err); |
| 173 | + } |
| 174 | + throw err; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + private throwHttp(err: MarketplaceError): never { |
| 179 | + switch (err.code) { |
| 180 | + case MarketplaceErrorCode.LISTING_NOT_FOUND: |
| 181 | + case MarketplaceErrorCode.BID_NOT_FOUND: |
| 182 | + throw new NotFoundException({ code: err.code, message: err.message }); |
| 183 | + case MarketplaceErrorCode.UNAUTHORIZED: |
| 184 | + case MarketplaceErrorCode.USERNAME_NOT_OWNED: |
| 185 | + throw new ForbiddenException({ code: err.code, message: err.message }); |
| 186 | + case MarketplaceErrorCode.ALREADY_LISTED: |
| 187 | + throw new ConflictException({ code: err.code, message: err.message }); |
| 188 | + default: |
| 189 | + throw new BadRequestException({ code: err.code, message: err.message }); |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments