mirror of
https://github.com/Permissionless-Software-Foundation/x402-base-facilitator.git
synced 2026-09-21 16:52:01 -07:00
ported user route. Still need middleware
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
// const WebhookRESTController = require('./rest/webhook')
|
||||
// const PostWebhook = require('./rest/post-webhook')
|
||||
const AuthRESTController = require('./auth')
|
||||
const UserRESTController = require('./users')
|
||||
|
||||
// Load the Clean Architecture Adapters library
|
||||
// const adapters = require('../adapters')
|
||||
@@ -42,6 +43,10 @@ function attachRESTControllers (app) {
|
||||
// Attach the REST API Controllers associated with the /auth route
|
||||
const authRESTController = new AuthRESTController()
|
||||
authRESTController.attach(app)
|
||||
|
||||
// Attach the REST API Controllers associated with the /user route
|
||||
const userRESTController = new UserRESTController()
|
||||
userRESTController.attach(app)
|
||||
}
|
||||
|
||||
// Add the JSON RPC router to the ipfs-coord adapter.
|
||||
|
||||
@@ -1,16 +1,21 @@
|
||||
/*
|
||||
REST API Controller library for the /user route
|
||||
*/
|
||||
|
||||
// User database model.
|
||||
const User = require('../../models/users')
|
||||
const UserModel = require('../../../models/users')
|
||||
|
||||
// User library for business logic.
|
||||
const UserLib = require('../../lib/users')
|
||||
const UserLib = require('../../../lib/users')
|
||||
|
||||
const wlogger = require('../../lib/wlogger')
|
||||
const wlogger = require('../../../lib/wlogger')
|
||||
|
||||
let _this
|
||||
class UserController {
|
||||
|
||||
class UserRESTControllerLib {
|
||||
constructor () {
|
||||
// Encapsulate dependencies
|
||||
this.User = User
|
||||
this.UserModel = UserModel
|
||||
this.userLib = new UserLib()
|
||||
|
||||
_this = this
|
||||
@@ -277,4 +282,4 @@ class UserController {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = UserController
|
||||
module.exports = UserRESTControllerLib
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
REST API library for /user route.
|
||||
*/
|
||||
|
||||
const UserRESTRouter = require('./router')
|
||||
|
||||
class UserRESTController {
|
||||
constructor (localConfig = {}) {
|
||||
this.userRESTRouter = new UserRESTRouter()
|
||||
}
|
||||
|
||||
attach (app) {
|
||||
this.userRESTRouter.attachControllers(app)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = UserRESTController
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
REST Router for the /user route.
|
||||
*/
|
||||
|
||||
// Public npm libraries.
|
||||
const Router = require('koa-router')
|
||||
|
||||
// Local libraries.
|
||||
const UserRESTControllerLib = require('./controller')
|
||||
|
||||
class UserRESTRouter {
|
||||
constructor (localConfig = {}) {
|
||||
// Encapsulate dependencies.
|
||||
this.userRESTController = new UserRESTControllerLib()
|
||||
|
||||
// Instantiate the router and set the base route.
|
||||
const baseUrl = '/users'
|
||||
this.router = new Router({ prefix: baseUrl })
|
||||
}
|
||||
|
||||
attachControllers (app) {
|
||||
if (!app) {
|
||||
throw new Error(
|
||||
'Must pass app object when attaching REST API controllers.'
|
||||
)
|
||||
}
|
||||
|
||||
// Define the routes and attach the controller.
|
||||
this.router.post('/', this.userRESTController.createUser)
|
||||
this.router.get('/', this.userRESTController.getUsers)
|
||||
this.router.get('/:id', this.userRESTController.getUsers)
|
||||
this.router.put('/', this.userRESTController.updateUser)
|
||||
this.router.delete('/', this.userRESTController.deleteUser)
|
||||
|
||||
// Attach the Controller routes to the Koa app.
|
||||
app.use(this.router.routes())
|
||||
app.use(this.router.allowedMethods())
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = UserRESTRouter
|
||||
@@ -1,50 +0,0 @@
|
||||
const VALIDATOR = require('../../middleware/validators')
|
||||
const validator = new VALIDATOR()
|
||||
|
||||
const CONTROLLER = require('./controller')
|
||||
const controller = new CONTROLLER()
|
||||
|
||||
// export const baseUrl = '/users'
|
||||
module.exports.baseUrl = '/users'
|
||||
|
||||
module.exports.routes = [
|
||||
{
|
||||
method: 'POST',
|
||||
route: '/',
|
||||
handlers: [controller.createUser]
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
route: '/',
|
||||
handlers: [
|
||||
validator.ensureUser,
|
||||
controller.getUsers
|
||||
]
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
route: '/:id',
|
||||
handlers: [
|
||||
validator.ensureUser,
|
||||
controller.getUser
|
||||
]
|
||||
},
|
||||
{
|
||||
method: 'PUT',
|
||||
route: '/:id',
|
||||
handlers: [
|
||||
validator.ensureTargetUserOrAdmin,
|
||||
controller.getUser,
|
||||
controller.updateUser
|
||||
]
|
||||
},
|
||||
{
|
||||
method: 'DELETE',
|
||||
route: '/:id',
|
||||
handlers: [
|
||||
validator.ensureTargetUserOrAdmin,
|
||||
controller.getUser,
|
||||
controller.deleteUser
|
||||
]
|
||||
}
|
||||
]
|
||||
+1
-1
@@ -11,7 +11,7 @@ const LOCALHOST = `http://localhost:${config.port}`
|
||||
|
||||
const context = {}
|
||||
|
||||
const UserController = require('../../../src/modules/users/controller')
|
||||
const UserController = require('../../../src/controllers/rest-api/users/controller')
|
||||
let uut
|
||||
let sandbox
|
||||
|
||||
@@ -12,7 +12,7 @@ const config = require('../../../config')
|
||||
const testUtils = require('../../utils/test-utils')
|
||||
const User = require('../../../src/models/users')
|
||||
|
||||
const UserController = require('../../../src/modules/users/controller')
|
||||
const UserController = require('../../../src/controllers/rest-api/users/controller')
|
||||
let uut
|
||||
let sandbox
|
||||
let ctx
|
||||
|
||||
Reference in New Issue
Block a user