{"size": 2607, "ext": "js", "max_stars_repo_path": "apps/content/server/controllers/categories.js", "max_stars_repo_name": "sliron1989/greenpress", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "apps/content/server/controllers/categories.js", "max_issues_repo_name": "sliron1989/greenpress", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "apps/content/server/controllers/categories.js", "max_forks_repo_name": "sliron1989/greenpress", "max_forks_repo_forks_event_min_datetime": "2021-12-26T10:14:34.000Z", "max_forks_repo_forks_event_max_datetime": "2021-12-26T10:14:34.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 21.5454545455, "max_line_length": 86, "alphanum_fraction": 0.6440352896}
const Category = require('../models/category')

function getCategoryByPath (req, res, next) {
	Category.findOne(
		{ path: req.params.categoryPath || req.query.category, tenant: req.headers.tenant },
		req.categorySelect, { lean: req.leanCategory })
		.then(category => {
			if (!category) {
				return Promise.reject(null)
			}
			req.category = category
			return next()
		})
		.catch(() => {
			res.status(404).json({ message: 'category not exists' }).end()
		})
}

function getCategoryMetadataByPath (req, res, next) {
	req.categorySelect = 'name path _id'
	req.leanCategory = true
	return getCategoryByPath(req, res, next)
}

function getCategoriesList (req, res) {
	const query = { tenant: req.headers.tenant }
	if (req.query.target === 'front' || !(req.user && req.user.isEditor)) {
		query.isPublic = true
	}

	Category.find(query)
		.select('-tenant')
		.lean()
		.then(list => {
			res.status(200).json(list || []).end()
		})
		.catch(() => {
			res.status(400).json({ message: 'failed to load categories' }).end()
		})
}

function getCategory (req, res) {
	res.status(200).json(req.category).end()
}

function createCategory (req, res) {
	const body = req.body || {}
	const category = new Category({
		tenant: req.headers.tenant,
		name: body.name,
		content: body.content,
		path: body.path,
		isPublic: body.isPublic
	})

	if (category.path === '-') {
		category.name = ''
		category.isPublic = true
	}

	category.save()
		.then(category => {
			if (!category) {
				return Promise.reject(null)
			}
			res.status(200).json(category).end()
		})
		.catch(() => res.status(400).json({ message: 'category creation failed' }).end())
}

function updateCategory (req, res) {
	const body = req.body || {}
	const category = req.category

	if (category.path !== '-') {
		if (body.name) {
			category.name = body.name
		}
		if (body.path) {
			category.path = body.path
		}
		if (typeof body.isPublic !== 'undefined') {
			category.isPublic = body.isPublic
		}
	}

	if (body.content) {
		category.content = body.content
	}

	category.save()
		.then(category => {
			res.status(200).json(category).end()
		})
		.catch(() => {
			res.status(400).json({ message: 'category update failed' }).end()
		})
}

function removeCategory (req, res) {
	const category = req.category

	category.remove()
		.then(category => {
			res.status(200).json(category).end()
		})
		.catch(() => {
			res.status(400).json({ message: 'category remove failed' }).end()
		})
}

module.exports = {
	getCategoryByPath,
	getCategoryMetadataByPath,
	getCategoriesList,
	getCategory,
	createCategory,
	updateCategory,
	removeCategory
}
