{"size": 1532, "ext": "js", "max_stars_repo_path": "app/components/header/headerController.js", "max_stars_repo_name": "muhammadardie/express-cms", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "app/components/header/headerController.js", "max_issues_repo_name": "muhammadardie/express-cms", "max_issues_repo_issues_event_min_datetime": "2020-07-01T04:58:03.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-11T05:09:35.000Z", "max_forks_repo_path": "app/components/header/headerController.js", "max_forks_repo_name": "muhammadardie/express-cms", "max_forks_repo_forks_event_min_datetime": "2021-08-02T08:54:51.000Z", "max_forks_repo_forks_event_max_datetime": "2021-08-02T08:54:51.000Z"}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": 1, "avg_line_length": 30.0392156863, "max_line_length": 125, "alphanum_fraction": 0.6507832898}
import header from './headerModel.js';
import { all, find, findBy } from '../repository/queryRepository';
import { storeImage, updateImage, deleteImage } from '../repository/fileRepository';

// if in field contain file upload use fileRepository

const capitalizeFirstLetter = (string) => {
	return string.charAt(0).toUpperCase() + string.slice(1);
}

exports.findHeader = (req, res) => {
    find(header, req.params.headerId, res);
};

exports.getHeaders = (req, res) => {
    all(header, res);
};

exports.findHeaderByPage = (req, res) => {
	const param = { page: capitalizeFirstLetter(req.params.page) };
	findBy(header, param, res);
};

exports.storeHeader = (req, res) => {
	storeImage(header, req, res, 'image', 'header'); // (model, request, respond, fieldName, pathName)
};

exports.updateHeader = (req, res) => {
	updateImage(header, req.params.headerId, req, res, 'image', 'header'); // (model, id, request, respond, fieldName, pathName)
};

exports.deleteHeader = (req, res) => {
    deleteImage(header, req.params.headerId, req, res, 'image','header');
};

exports.existHeader = (req, res) => {
	Promise.all([
	  req.body.page && header.countDocuments({'page': req.body.page, '_id': { $ne: req.params.headerId } }),
	]).then( ([ foundPage ]) => {
		
	  if(foundPage > 0) {
	  	res.json({exist: true, msg: 'Header for '+ req.body.page +' page already exist'})
	  } else {
	  	return res.json({exist: false})	
	  }
	  
	}).catch(err => {
		console.log(err)
		res.json({exist: true, msg: 'Failed to check existed'})
	});
};