mahaplatform/backframe

View on GitHub
src/resources/edit_route.js

Summary

Maintainability
A
1 hr
Test Coverage
import Route from '../route'
import _ from 'lodash'

class EditRoute extends Route {

  constructor(config = {}) {
    super(config)
    this.setAction('edit')
    this.setMethod('get')
    this.setPath('/:id/edit')
    this.setProcessor(this._processor)
    this.setSerializer(this._processor)
    if(config.defaultQuery) this.setDefaultQuery(config.defaultQuery)
    if(config.model) this.setModel(config.model)
  }

  setDefaultQuery(defaultQuery) {
    this._setOption('defaultQuery', _.castArray(defaultQuery))
  }

  setModel(model) {
    this._setOption('model', model)
  }

  async _processor(req, trx, options) {

    return req.resource

  }

  _serializer(req, trx, record, options) {

    if(!options.allowedParams) return record.toJSON()

    return _.pick(record.toJSON(), options.allowedParams)

  }

}

export default EditRoute