diff --git a/src/controllers/rest-api/offer/controller.js b/src/controllers/rest-api/offer/controller.js index 198c3d8..507d0d7 100644 --- a/src/controllers/rest-api/offer/controller.js +++ b/src/controllers/rest-api/offer/controller.js @@ -1,5 +1,8 @@ /* REST API Controller library for the /offer route + + TODO: + - Add api-doc documentation for all endpoints in this file. */ import wlogger from '../../../adapters/wlogger.js' @@ -30,6 +33,7 @@ class OfferRESTControllerLib { this.listNftOffers = this.listNftOffers.bind(this) this.listFungibleOffers = this.listFungibleOffers.bind(this) this.takeOffer = this.takeOffer.bind(this) + this.listOffersByAddress = this.listOffersByAddress.bind(this) this.handleError = this.handleError.bind(this) } @@ -115,6 +119,21 @@ class OfferRESTControllerLib { } } + // List all offers being made by a given address. + // curl -X GET http://localhost:5700/offer/list/addr/bitcoincash:qrpxtnrlhfz9wsuuse7z5k2mxmvw0r3pu5qepyhmq2 + async listOffersByAddress (ctx) { + try { + const addr = ctx.params.addr + + const offers = await this.useCases.offer.listOffersByAddress(addr) + + ctx.body = offers + } catch (err) { + console.log('Error in listOffersByAddress REST API handler: ', err) + this.handleError(ctx, err) + } + } + // DRY error handler handleError (ctx, err) { console.log('err', err.message) diff --git a/src/controllers/rest-api/offer/index.js b/src/controllers/rest-api/offer/index.js index 77fe1bb..d62231a 100644 --- a/src/controllers/rest-api/offer/index.js +++ b/src/controllers/rest-api/offer/index.js @@ -8,8 +8,6 @@ import Router from 'koa-router' // Local libraries. import OfferRESTControllerLib from './controller.js' -let _this - class OfferRouter { constructor (localConfig = {}) { // Dependency Injection. @@ -38,7 +36,8 @@ class OfferRouter { const baseUrl = '/offer' this.router = new Router({ prefix: baseUrl }) - _this = this + // Bind 'this' object to all subfunctions. + this.attach = this.attach.bind(this) } attach (app) { @@ -50,19 +49,20 @@ class OfferRouter { // 12/3/24 CT: // Note: The createOffer() path was used by a P2WDB webhook to generate an - // Offer from and Order. This has been deprecated and Offers are now created + // Offer from an Order. This has been deprecated and Offers are now created // by a Timer Controller monitoring a Nostr topic. // Define the routes and attach the controller. // this.router.post('/', _this.offerRESTController.createOffer) // Deprecated. - this.router.post('/take', _this.offerRESTController.takeOffer) - this.router.get('/list/all/:page', _this.offerRESTController.listOffers) - this.router.get('/list/nft/:page', _this.offerRESTController.listNftOffers) - this.router.get('/list/fungible/:page', _this.offerRESTController.listFungibleOffers) + this.router.post('/take', this.offerRESTController.takeOffer) + this.router.get('/list/all/:page', this.offerRESTController.listOffers) + this.router.get('/list/nft/:page', this.offerRESTController.listNftOffers) + this.router.get('/list/fungible/:page', this.offerRESTController.listFungibleOffers) + this.router.get('/list/addr/:addr', this.offerRESTController.listOffersByAddress) // Attach the Controller routes to the Koa app. - app.use(_this.router.routes()) - app.use(_this.router.allowedMethods()) + app.use(this.router.routes()) + app.use(this.router.allowedMethods()) } } diff --git a/src/use-cases/offer/index.js b/src/use-cases/offer/index.js index 2367de0..c8f4ce2 100644 --- a/src/use-cases/offer/index.js +++ b/src/use-cases/offer/index.js @@ -64,6 +64,7 @@ class OfferUseCases { this.removeStaleOffers = this.removeStaleOffers.bind(this) this.flagOffer = this.flagOffer.bind(this) this.loadOffers = this.loadOffers.bind(this) + this.listOffersByAddress = this.listOffersByAddress.bind(this) // State this.seenOffers = [] @@ -808,6 +809,17 @@ class OfferUseCases { throw error } } + + // List all offers being made by a given address. + async listOffersByAddress (addr) { + try { + const offers = await this.OfferModel.find({ makerAddr: addr }) + return offers + } catch (err) { + console.error('Error in listOffersByAddress(): ', err) + throw err + } + } } export default OfferUseCases