social-media/system/controllers/Controller.js

73 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

2023-11-08 20:11:47 +00:00
const autoBind = require('auto-bind');
class Controller {
/**
* Base Controller Layer
* @author Sunil Kumar Samanta
* @param service
*/
constructor(service) {
this.service = service;
autoBind(this);
}
async index(req, res, next) {
try {
const response = await this.service.index(req.query);
return res.status(response.statusCode).json(response);
} catch (e) {
next(e);
}
}
async show(req, res, next) {
const { id } = req.params;
try {
const response = await this.service.show(id);
return res.status(response.statusCode).json(response);
} catch (e) {
next(e);
}
}
async store(req, res, next) {
try {
const response = await this.service.store(req.body);
return res.status(response.statusCode).json(response);
} catch (e) {
next(e);
}
}
async update(req, res, next) {
const { id } = req.params;
try {
const response = await this.service.update(id, req.body);
return res.status(response.statusCode).json(response);
} catch (e) {
next(e);
}
}
async delete(req, res, next) {
const { id } = req.params;
try {
const response = await this.service.delete(id);
return res.status(response.statusCode).json(response);
} catch (e) {
next(e);
}
}
}
module.exports = { Controller };