/var/www/verywell.gr/server/delivery/api/controllers
Edit: /var/www/verywell.gr/server/delivery/api/controllers/category.controller.js (5521B)
const logger = require('../../logger')({ service: 'api' })
const checkPermission = require('../utils/checkPermission.util')
const { NotFound, BadRequest } = require('../responseModels/errors')
const { CategoryListModel, CategoryModel } = require('../responseModels')
const {performance} = require('perf_hooks');
module.exports = function ({ db }) {
this.List = async (req, res, next) => {
logger.child({ path: req.path, request: req.query }).info()
try {
let t1 = performance.now()
//checkPermission(req, 'category_list')
const { limit, page } = req.query
let offset = page ? (page - 1) * (limit || 10) : 0
const { count, rows } = await db.category.findAndCountAll({
include: [
{ model: db.categoryImage }
// { model: db.category, as: 'subcategories' },
],
// where: { categoryId: null },
offset,
limit: parseInt(limit) || 10
})
if (!rows || rows.length == 0) {
throw new NotFound({ lang: req.user?.languageCode })
}
const time = performance.now() - t1
return res.status(200).json(
new CategoryListModel({
categories: rows,
count,
page,
time
})
)
} catch (err) {
next(err)
}
}
this.Details = async (req, res, next) => {
logger.child({ path: req.path, request: req.params }).info()
try {
//checkPermission(req, 'category_details')
const { uid } = req.params
if (!uid) {
throw new BadRequest({ lang: req.user?.languageCode })
}
const category = await db.category.findOne({
include: [
{ model: db.categoryImage },
{
model: db.category,
as: 'subcategories',
include: [{ model: db.categoryImage }]
}
],
where: { uid: uid }
})
if (!category) {
throw new NotFound({ lang: req.user?.languageCode })
}
return res.status(200).json(new CategoryModel(category))
} catch (err) {
next(err)
}
}
this.Create = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
checkPermission(req, 'category_edit')
const { name, description, categoryId, position } = req.body
const category = await db.category.create({
name,
description,
categoryId,
position
})
return res.status(200).json({
status: 'success',
uid: category.uid
})
} catch (err) {
next(err)
}
}
this.Edit = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
checkPermission(req, 'category_edit')
const { uid, name, description, categoryId, position } = req.body
const category = await db.category.findOne({ where: { uid: uid } })
if (!category) {
throw new NotFound({ lang: req.user?.languageCode })
}
category.name = name ? name : category.name
category.description = description ? description : category.description
category.categoryId = categoryId ? categoryId : null
category.position = position ? position : 0
await category.save()
return res.status(200).json(category)
} catch (err) {
next(err)
}
}
this.Delete = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
checkPermission(req, 'category_edit')
const { uid } = req.body
if (!uid) {
throw new BadRequest({ lang: req.user?.languageCode })
}
const category = await db.category.findOne({ where: { uid: uid } })
if (!category) {
throw new NotFound({ lang: req.user?.languageCode })
}
await db.category.destroy({ where: { uid: uid } })
return res.status(200).json({ status: 'success', uid: category.uid })
} catch (err) {
next(err)
}
}
this.AddImage = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
checkPermission(req, 'category_edit')
const { categoryUid, categoryImage } = req.body
if (!categoryUid) {
throw new BadRequest({ lang: req.user?.languageCode })
}
var image = JSON.parse(categoryImage)
if (!categoryImage) {
throw new BadRequest({ lang: req.user?.languageCode })
}
const category = await db.category.findOne({
where: { uid: categoryUid }
})
if (!category) {
throw new NotFound({ lang: req.user?.languageCode })
}
image.categoryId = category.id
const result = await db.categoryImage.create(image)
return res.status(200).json({ status: 'success', uid: result.uid })
} catch (err) {
next(err)
}
}
this.RemoveImage = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
checkPermission(req, 'category_edit')
const { imageUid } = req.body
if (!imageUid) {
throw new BadRequest({ lang: req.user?.languageCode })
}
await db.categoryImage.destroy({ where: { uid: imageUid } })
return res.status(200).json({ status: 'success', uid: imageUid })
} catch (err) {
next(err)
}
}
this.SetDefaultImage = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
checkPermission(req, 'category_edit')
const { imageUid } = req.body
if (!imageUid) {
throw new BadRequest({ lang: req.user?.languageCode })
}
let category_image = await db.categoryImage.findOne({
where: { uid: imageUid }
})
await db.categoryImage.update(
{ isDefault: false },
{ where: { categoryId: category_image.categoryId } }
)
category_image.isDefault = true
category_image.save()
return res.status(200).json({ status: 'success', uid: imageUid })
} catch (err) {
next(err)
}
}
}