mirror of
https://github.com/Permissionless-Software-Foundation/bch-dex.git
synced 2026-09-21 16:52:00 -07:00
68 lines
1.7 KiB
JavaScript
68 lines
1.7 KiB
JavaScript
/*
|
|
REST API Controller library for the /entry route
|
|
*/
|
|
|
|
// const { wlogger } = require('../../../adapters/wlogger')
|
|
|
|
let _this
|
|
|
|
class EntryRESTControllerLib {
|
|
constructor (localConfig = {}) {
|
|
// Dependency Injection.
|
|
this.adapters = localConfig.adapters
|
|
if (!this.adapters) {
|
|
throw new Error(
|
|
'Instance of Adapters library required when instantiating /entry REST Controller.'
|
|
)
|
|
}
|
|
this.useCases = localConfig.useCases
|
|
if (!this.useCases) {
|
|
throw new Error(
|
|
'Instance of Use Cases library required when instantiating /entry REST Controller.'
|
|
)
|
|
}
|
|
|
|
// Encapsulate dependencies
|
|
this.EntryModel = this.adapters.localdb.Entry
|
|
// this.userUseCases = this.useCases.user
|
|
|
|
_this = this
|
|
}
|
|
|
|
// No api-doc documentation because this wont be a public endpoint
|
|
async createEntry (ctx) {
|
|
try {
|
|
console.log('body: ', ctx.request.body)
|
|
|
|
const entryObj = ctx.request.body.entry
|
|
|
|
const entry = await _this.useCases.entry.createEntry(entryObj)
|
|
|
|
ctx.body = { entry }
|
|
} catch (err) {
|
|
// console.log(`err.message: ${err.message}`)
|
|
// console.log('err: ', err)
|
|
// ctx.throw(422, err.message)
|
|
_this.handleError(ctx, err)
|
|
}
|
|
}
|
|
|
|
// DRY error handler
|
|
handleError (ctx, err) {
|
|
console.log('err', err.message)
|
|
// If an HTTP status is specified by the buisiness logic, use that.
|
|
if (err.status) {
|
|
if (err.message) {
|
|
ctx.throw(err.status, err.message)
|
|
} else {
|
|
ctx.throw(err.status)
|
|
}
|
|
} else {
|
|
// By default use a 422 error if the HTTP status is not specified.
|
|
ctx.throw(422, err.message)
|
|
}
|
|
}
|
|
}
|
|
|
|
export default EntryRESTControllerLib
|