diff --git a/src/controllers/rest-api/offer/controller.js b/src/controllers/rest-api/offer/controller.js index fb9da5b..e6a9611 100644 --- a/src/controllers/rest-api/offer/controller.js +++ b/src/controllers/rest-api/offer/controller.js @@ -49,14 +49,47 @@ class OfferRESTControllerLib { } } - // curl -X GET http://localhost:5700/offer/list + // curl -X GET http://localhost:5700/offer/list/all/0 async listOffers (ctx) { try { - const offers = await _this.useCases.offer.listOffers() + let page = ctx.params.page + if (!page) page = 0 + + const offers = await _this.useCases.offer.listOffers(page) ctx.body = offers } catch (err) { - console.log('Error in listOffers REST API handler.') + console.log('Error in listOffers REST API handler: ', err) + _this.handleError(ctx, err) + } + } + + // curl -X GET http://localhost:5700/offer/list/nft/0 + async listNftOffers (ctx) { + try { + let page = ctx.params.page + if (!page) page = 0 + + const offers = await _this.useCases.offer.listNftOffers(page) + + ctx.body = offers + } catch (err) { + console.log('Error in listNftOffers REST API handler: ', err) + _this.handleError(ctx, err) + } + } + + // curl -X GET http://localhost:5700/offer/list/fungible/0 + async listFungibleOffers (ctx) { + try { + let page = ctx.params.page + if (!page) page = 0 + + const offers = await _this.useCases.offer.listFungibleOffers(page) + + ctx.body = offers + } catch (err) { + console.log('Error in Fungible REST API handler: ', err) _this.handleError(ctx, err) } } diff --git a/src/controllers/rest-api/offer/index.js b/src/controllers/rest-api/offer/index.js index f057558..da3123c 100644 --- a/src/controllers/rest-api/offer/index.js +++ b/src/controllers/rest-api/offer/index.js @@ -50,8 +50,10 @@ class OfferRouter { // Define the routes and attach the controller. this.router.post('/', _this.offerRESTController.createOffer) - this.router.get('/list', _this.offerRESTController.listOffers) 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) // Attach the Controller routes to the Koa app. app.use(_this.router.routes()) diff --git a/src/use-cases/offer/index.js b/src/use-cases/offer/index.js index 6425edf..6875f0c 100644 --- a/src/use-cases/offer/index.js +++ b/src/use-cases/offer/index.js @@ -17,6 +17,10 @@ const OfferEntity = require('../../entities/offer') const config = require('../../../config') +const DEFAULT_ENTRIES_PER_PAGE = 20 +const NFT_ENTRIES_PER_PAGE = 6 +const FUNGIBLE_ENTRIES_PER_PAGE = 20 + class OfferUseCases { constructor (localConfig = {}) { // console.log('User localConfig: ', localConfig) @@ -139,15 +143,61 @@ class OfferUseCases { } } - async listOffers () { + async listOffers (page = 0) { try { - return this.OfferModel.find({}).sort('-timestamp') + const data = await this.OfferModel.find({}) + // Sort entries so newest entries show first. + .sort('-timestamp') + // Skip to the start of the selected page. + .skip(page * DEFAULT_ENTRIES_PER_PAGE) + // Only return 20 results. + .limit(DEFAULT_ENTRIES_PER_PAGE) + + return data } catch (error) { console.error('Error in use-cases/offer/listOffers()') throw error } } + async listNftOffers (page = 0) { + try { + const data = await this.OfferModel.find({ displayCategory: { $ne: 'fungible' } }) + // Sort entries so newest entries show first. + .sort('-timestamp') + // Skip to the start of the selected page. + .skip(page * NFT_ENTRIES_PER_PAGE) + // Only return 20 results. + .limit(NFT_ENTRIES_PER_PAGE) + + // console.log('listNftOffers() returning this data: ', data) + + return data + } catch (error) { + console.error('Error in use-cases/offer/listNftOffers()') + throw error + } + } + + async listFungibleOffers (page = 0) { + try { + const data = await this.OfferModel.find({ displayCategory: 'fungible' }) + // Sort entries so newest entries show first. + .sort('-timestamp') + // Skip to the start of the selected page. + .skip(page * FUNGIBLE_ENTRIES_PER_PAGE) + // Only return 20 results. + .limit(FUNGIBLE_ENTRIES_PER_PAGE) + + // console.log('listFungibleOffers() returning this data: ', data) + + return data + } catch (error) { + console.error('Error in use-cases/offer/listFungibleOffers()') + throw error + } + } + // Generate phase 2 of 3 - take the other side of an Offer. // Based on this example: // https://github.com/Permission=less-Software-Foundation/bch-js-examples/blob/master/bch/applications/collaborate/sell-slp/e2e-exchange/step2-purchase-tx.js