Skip to content

Instantly share code, notes, and snippets.

@harryWonder
Created May 17, 2020 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harryWonder/ccb6b59f1412f115d036cd35943bad3e to your computer and use it in GitHub Desktop.
Save harryWonder/ccb6b59f1412f115d036cd35943bad3e to your computer and use it in GitHub Desktop.
This gist acts as my continent controller
const _ = require('lodash');
/****************************************************************/
/************ Custom Module Imports *********************/
/****************************************************************/
const Controller = require('./Controller');
const ContinentModel = require('../model/ContinentModel');
class ContinentController extends Controller {
constructor() {
super();
}
/****************************************************************/
/******* Creates A New Continent Resource ***********************/
/****************************************************************/
async createContinent(req, res) {
let {name, status} = req.body;
let Response = { status: 200, message: [], data: [] };
if (typeof name === undefined || name == "") {
Response.status = 400;
Response.message.push({ name: 'The continent name is required.' });
}
if (!name.match( /^[a-zA-Z-,]+(\s{0,1}[a-zA-Z-, ])*$/)) {
Response.status = 400;
Response.message.push({ name: 'Sorry, Only Alphabets are allowed.' });
}
if (typeof status === undefined || typeof status !== "boolean") {
status = true;
}
try {
/****************************************************************/
/******* Do some lodash magic *************************/
/****************************************************************/
name = _.escape(name);
name = _.trim(name);
name = _.upperFirst(name);
let slug = _.kebabCase(name);
/****************************************************************/
/******* Check if continent exist ***********************/
/****************************************************************/
const Continent = new ContinentModel();
const checkContinent = await Continent.fetchOne(name);
if (checkContinent) {
Response.status = 400;
Response.message.push({ name: 'Sorry, this continent already exists.' });
}
/****************************************************************/
/******* Send Back the error response ***********************/
/****************************************************************/
if (Response.status !== 200) {
res.status(400).json(Response);
return;
}
/****************************************************************/
/******* Creates the continent resource ***********************/
/****************************************************************/
const Payload = { name, slug, status };
let newContinent = await Continent.create(Payload);
newContinent = _.omit(newContinent[0], ['_id']);
Response.status = 201;
Response.data.push(newContinent);
res.status(201).json(Response);
return;
} catch (e) {
Response.status = 500;
Response.message.push({
server: {
customMessage: `Sorry, an exception occurred and your request could not be completed. `,
errorName: e.name,
errorMessage: e.message
}
})
res.status(500).json(Response);
return;
}
}
/****************************************************************/
/********* Fetches all Continent Resource ***********************/
/****************************************************************/
async fetchContinents(req, res) {
const Response = { status: 200, message: [], data: [] };
try {
const Continent = new ContinentModel();
const ContinentsCollections = await Continent.fetchAll();
if (ContinentsCollections.length > 0) {
Response.data = ContinentsCollections;
Response.data = super.removeDocumentID(Response.data, _.omit);
res.status(200).json(Response);
return;
}
Response.status = 204;
res.status(204).send(Response);
return;
} catch (e) {
Response.status = 500;
Response.message.push({
server: {
customMessage: `Sorry, an exception occurred and your request could not be completed. `,
errorName: e.name,
errorMessage: e.message
}
})
res.status(500).json(Response);
return;
}
}
}
const Continent = new ContinentController
module.exports = Continent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment