diff --git a/src/controllers/rest-api/smAccount/controller.js b/src/controllers/rest-api/smAccount/controller.js index 7ba1e43..0ac2fb6 100644 --- a/src/controllers/rest-api/smAccount/controller.js +++ b/src/controllers/rest-api/smAccount/controller.js @@ -29,6 +29,16 @@ class SmAccountRESTControllerLib { _this = this } + /** + * @api {get} /sm/list/all/{page} List all Social Media Accounts + * @apiPermission public + * @apiName ListAccounts + * @apiGroup REST Social Media Accounts + * + * @apiExample Example usage: + * curl -H "Content-Type: application/json" -X GET localhost:5700/sm/list/all/0 + * + */ // curl -X GET http://localhost:5700/sm/list/all/0 async listAccounts (ctx) { try { @@ -44,6 +54,68 @@ class SmAccountRESTControllerLib { } } + /** + * @api {get} /sm/npub/{npub} Get Social Media Account by Npub + * @apiPermission public + * @apiName GetAccountByNpub + * @apiGroup REST Social Media Accounts + * + * @apiExample Example usage: + * curl -H "Content-Type: application/json" -X GET localhost:5700/sm/npub/npub1y3xq402pu9aqms3khetnt5gzm54t63pzcla56wwfmwshde0ws77qkff5gc + * + */ + // curl -X GET http://localhost:5700/sm/npub/npub1y3xq402pu9aqms3khetnt5gzm54t63pzcla56wwfmwshde0ws77qkff5gc + async getAccountByNpub (ctx) { + try { + const npub = ctx.params.npub + if (!npub) { + ctx.throw(400, 'Npub is required') + } + + const account = await _this.useCases.smAccount.getAccountByNpub(npub) + + if (!account) { + ctx.throw(404, 'Account not found') + } + + ctx.body = account + } catch (err) { + // console.log('Error in getAccountByNpub REST API handler: ', err) + _this.handleError(ctx, err) + } + } + + /** + * @api {get} /sm/bchAddr/{bchAddr} Get Social Media Account by BCH address + * @apiPermission public + * @apiName GetAccountByBchAddr + * @apiGroup REST Social Media Accounts + * + * @apiExample Example usage: + * curl -H "Content-Type: application/json" -X GET localhost:5700/sm/bchAddr/bitcoincash:qz6lf6gpmn3secx73g7ucfcgy8mrh3sz2y2ylk643x + * + */ + // curl -X GET http://localhost:5700/sm/bchAddr/bitcoincash:qz6lf6gpmn3secx73g7ucfcgy8mrh3sz2y2ylk643x + async getAccountByBchAddr (ctx) { + try { + const bchAddr = ctx.params.bchAddr + if (!bchAddr) { + ctx.throw(400, 'BCH address is required') + } + + const account = await _this.useCases.smAccount.getAccountByBchAddr(bchAddr) + + if (!account) { + ctx.throw(404, 'Account not found') + } + + ctx.body = account + } catch (err) { + // console.log('Error in getAccountByBchAddr REST API handler: ', err) + _this.handleError(ctx, err) + } + } + // DRY error handler handleError (ctx, err) { console.log('err', err) diff --git a/src/controllers/rest-api/smAccount/index.js b/src/controllers/rest-api/smAccount/index.js index 349877e..8420eca 100644 --- a/src/controllers/rest-api/smAccount/index.js +++ b/src/controllers/rest-api/smAccount/index.js @@ -53,6 +53,8 @@ class SmAccountRouter { // Define the routes and attach the controller. this.router.get('/list/all/:page', _this.smAccountRESTController.listAccounts) + this.router.get('/npub/:npub', _this.smAccountRESTController.getAccountByNpub) + this.router.get('/bchAddr/:bchAddr', _this.smAccountRESTController.getAccountByBchAddr) // Attach the Controller routes to the Koa app. app.use(_this.router.routes()) diff --git a/src/use-cases/smAccount-use-cases.js b/src/use-cases/smAccount-use-cases.js index d340d36..e70b484 100644 --- a/src/use-cases/smAccount-use-cases.js +++ b/src/use-cases/smAccount-use-cases.js @@ -17,6 +17,8 @@ class smAccountUseCases { // Bind 'this' object to all methods this.checkForNewSmAccounts = this.checkForNewSmAccounts.bind(this) this.listAccounts = this.listAccounts.bind(this) + this.getAccountByNpub = this.getAccountByNpub.bind(this) + this.getAccountByBchAddr = this.getAccountByBchAddr.bind(this) } // Check for new Social Media Accounts. @@ -73,6 +75,32 @@ class smAccountUseCases { throw err } } + + // Get a Social Media Account by Npub. + async getAccountByNpub (npub) { + try { + const SmAccount = await this.adapters.localdb.SmAccount + const account = await SmAccount.findOne({ npub }) + return account + } catch (err) { + console.error('Error in smAccount-use-cases.js/getAccountByNpub(): ', err) + throw err + } + } + + // Get a Social Media Account by BCH address. + async getAccountByBchAddr (bchAddr) { + try { + const SmAccount = await this.adapters.localdb.SmAccount + const account = await SmAccount.findOne({ bchAddr }) + console.log('Account found: ', account) + + return account + } catch (err) { + console.error('Error in smAccount-use-cases.js/getAccountByBchAddr(): ', err) + throw err + } + } } export default smAccountUseCases