import get from "lodash/get"
import BaseController from "../../BaseController"
import ProgramModel from "./model"
/**
* Controller for the @namespace /programs
*
* @class ProgramsController
* @extends {BaseController}
*/
class ProgramsController extends BaseController {
constructor() {
super()
this.index = this.index.bind(this)
this.show = this.show.bind(this)
}
/**
* GET / Fetch all programs
*
* @memberof ProgramsController
*/
async index(_, res) {
const { success, unprocessableEntity } = this.codes
const program = new ProgramModel()
const result = await program.all()
.catch((error) => {
res.status(unprocessableEntity).send({
success: "false",
message: "programs_error",
error,
})
throw error
})
res.status(success).send({
success: "true",
message: "program_retrieved_successfully",
programs: get(result, "rows", []),
})
}
/**
* GET /:id Fetch specific program by identifier
*
* @param {int} id
* @memberof ProgramsController
*/
async show(req, res) {
const { success, notFound, unprocessableEntity } = this.codes
const { id } = req.params
const program = new ProgramModel()
const result = await program.find({ id })
.catch((error) => {
res.status(unprocessableEntity).send({
success: "false",
message: "program_error",
error,
})
throw error
})
program.set(get(result, "rows[0]"))
if (program.error) {
res.status(notFound).send({
success: "false",
message: "program_not_found",
error: program.error,
})
return
}
res.status(success).send({
success: "true",
message: "program_retrieved_successfully",
program: program.props,
})
}
}
export default ProgramsController