Skip to content

Instantly share code, notes, and snippets.

@nartc
Created February 23, 2018 14:04
Show Gist options
  • Save nartc/dcf7e067f909ec6dfe6774bff1f991c8 to your computer and use it in GitHub Desktop.
Save nartc/dcf7e067f909ec6dfe6774bff1f991c8 to your computer and use it in GitHub Desktop.
import {Document, Model} from 'mongoose';
import {Request, Response} from 'express';
import {MongoError} from '../../../../Desktop/codewithcause/node_modules/@types/mongodb';
export class BaseController<U extends Document, T extends Model<U>> {
model: T;
constructor(_model: T) {
this.model = _model;
}
// Define Error Response and API Responses
private static resolveErrorResponse(response: Response, message: string, statusCode: number): Response {
return response.status(statusCode).json({
status: statusCode,
message
});
}
private static resolveMongoErrorResponse(response: Response, error: MongoError = null): Response {
return response.status(500).json({
status: 500,
mongoError: error.code,
message: error.message,
error: error.name
});
}
async getAll(req: Request, res: Response): Promise<Response> {
const result = await this.model.find().exec();
if (result instanceof MongoError)
return BaseController.resolveMongoErrorResponse(res, result);
return res.status(res.statusCode).json({
status: res.statusCode,
result
});
};
async getById(req: Request, res: Response): Promise<Response> {
const id = req.params.id;
if (!id)
return BaseController.resolveErrorResponse(res, 'Paramater ID is missing', 400);
const result = await this.model.findById(id).exec();
if (result instanceof MongoError)
return BaseController.resolveMongoErrorResponse(res, result);
return res.status(res.statusCode).json({
status: res.statusCode,
result
});
}
async create(req: Request, res: Response): Promise<Response> {
const requestBody: U = {...req.body} as U;
if (!requestBody)
return BaseController.resolveErrorResponse(res, 'Parameters missing', 400);
const result = await this.model.create(requestBody);
if (result instanceof MongoError)
return BaseController.resolveMongoErrorResponse(res, result);
return res.status(res.statusCode).json({
status: res.statusCode,
result
})
}
async update(req: Request, res: Response): Promise<Response> {
const updatedBody: U = {...req.body} as U;
const id = req.body.id;
if (!id)
return BaseController.resolveErrorResponse(res, 'Parameter ID is missing', 400);
if (!updatedBody)
return BaseController.resolveErrorResponse(res, 'Parameters missing', 400);
const result = await this.model.findByIdAndUpdate(id, updatedBody).exec();
if (result instanceof MongoError)
return BaseController.resolveMongoErrorResponse(res, result);
return res.status(res.statusCode).json({
status: res.statusCode,
result
});
}
async delete(req: Request, res: Response): Promise<Response> {
const id = req.body.id;
if (!id)
return BaseController.resolveErrorResponse(res, 'Parameter ID is missing', 400);
const result = await this.model.findByIdAndRemove(id).exec();
if (result instanceof MongoError)
return BaseController.resolveMongoErrorResponse(res, result);
return res.status(res.statusCode).json({
status: res.statusCode,
result
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment