/var/www/qr4y.com/server/delivery/api/controllers
Edit: /var/www/qr4y.com/server/delivery/api/controllers/gallery.controller.js (7929B)
const logger = require('../../logger')({ service: 'RoomController' })
const checkPermission = require('../utils/checkPermission.util')
const { isSet, isEmpty, minLength, isPhone, isEmail } = require('../utils/validator.util')
const { NotFound, BadRequest } = require('../responseModels/errors')
module.exports = function ({ db }) {
this.NextList = async (req, res, next) => {
logger.child({ path: req.path, request: req.query }).info()
let t1 = performance.now()
try {
const { countryCodeId, limit, page } = req.query
let where = {
'status': 'active'
}
if (countryCodeId) {
where['countryCodeId'] = countryCodeId
}
let offset = page > 0 ? (page - 1) * (limit || 10) : 0
const galleries = await db.gallery.findAll({
where,
offset,
order: [['updatedAt', 'DESC']],
limit: parseInt(limit) || 10
})
if (galleries.length == 0 || !galleries) {
throw new NotFound({ lang: req.user?.languageCode })
}
const time = performance.now() - t1
return res.status(200).json({ list: galleries, count: galleries.length, time, page })
} catch (err) {
next(err)
}
}
this.List = async (req, res, next) => {
logger.child({ path: req.path, request: req.query }).info()
let t1 = performance.now()
try {
const { countryCodeId, branchId, limit, page } = req.query
let where = {}
if (branchId) {
where['branchId'] = branchId
}
if (countryCodeId) {
where['countryCodeId'] = countryCodeId
}
let offset = page > 0 ? (page - 1) * (limit || 10) : 0
const galleries = await db.gallery.findAll({
where,
offset,
limit: parseInt(limit) || 100
})
if (galleries.length == 0 || !galleries) {
throw new NotFound({ lang: req.user?.languageCode })
}
const time = performance.now() - t1
return res.status(200).json({ list: galleries, count: galleries.length, time, page })
} catch (err) {
next(err)
}
}
this.Details = async (req, res, next) => {
logger.child({ path: req.path, request: req.params }).info()
try {
const { id } = req.params
if (!isSet(id) || isEmpty(id)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
const gallery = await db.gallery.findOne({
where: { id }
})
if (!gallery) {
throw new NotFound({ lang: req.user?.languageCode })
}
return res.status(200).json(gallery)
} catch (err) {
next(err)
}
}
this.Create = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
const { name, image, countryCodeId, branchId } = req.body
if (!isSet(name) || isEmpty(name)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
if (!isSet(branchId)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
const gallery = await db.gallery.create({
name,
image,
status: 'new',
showCount: 0,
showCounter: 0,
branchId,
countryCodeId
})
return res.status(200).json({
status: 'success',
id: gallery.id
})
} catch (err) {
next(err)
}
}
this.Edit = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
const { id, name, image, showCount } = req.body
if (!isSet(id) || isEmpty(id)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
if (!isSet(name) || isEmpty(name)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
const gallery = await db.gallery.findOne({
where: { id }
})
if (!gallery) {
throw new NotFound({ lang: req.user?.languageCode })
}
gallery.name = name ? name : gallery.name
gallery.image = image ? image : gallery.image
gallery.showCount = showCount ? showCount : gallery.showCount
gallery.status = 'new'
await gallery.save()
return res.status(200).json(gallery)
} catch (err) {
next(err)
}
}
this.Delete = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
const { id } = req.body
const gallery = await db.gallery.findOne({
where: { id }
})
if (!gallery) {
throw new NotFound({ lang: req.user?.languageCode })
}
await db.gallery.destroy({ where: { id } })
return res.status(200).json({ status: 'success', id })
} catch (err) {
next(err)
}
}
this.Start = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
const { id, showCount } = req.body
if (!isSet(id) || isEmpty(id)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
if (!isSet(showCount) || isEmpty(showCount)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
const gallery = await db.gallery.findOne({
where: { id }
})
if (!gallery) {
throw new NotFound({ lang: req.user?.languageCode })
}
gallery.showCount = showCount ? showCount : gallery.showCount
gallery.status = 'active'
await gallery.save()
return res.status(200).json(gallery)
} catch (err) {
next(err)
}
}
this.Stop = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
const { id } = req.body
if (!isSet(id) || isEmpty(id)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
const gallery = await db.gallery.findOne({
where: { id }
})
if (!gallery) {
throw new NotFound({ lang: req.user?.languageCode })
}
gallery.status = 'end'
await gallery.save()
return res.status(200).json(gallery)
} catch (err) {
next(err)
}
}
this.Check = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
const { id } = req.body
if (!isSet(id) || isEmpty(id)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
const gallery = await db.gallery.findOne({
where: { id }
})
if (!gallery) {
throw new NotFound({ lang: req.user?.languageCode })
}
gallery.status = 'start'
await gallery.save()
return res.status(200).json(gallery)
} catch (err) {
next(err)
}
}
this.Reject = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
const { id } = req.body
if (!isSet(id) || isEmpty(id)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
const gallery = await db.gallery.findOne({
where: { id }
})
if (!gallery) {
throw new NotFound({ lang: req.user?.languageCode })
}
gallery.status = 'reject'
await gallery.save()
return res.status(200).json(gallery)
} catch (err) {
next(err)
}
}
this.Show = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
const { id } = req.body
if (!isSet(id) || isEmpty(id)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
const gallery = await db.gallery.findOne({
where: { id }
})
if (!gallery) {
throw new NotFound({ lang: req.user?.languageCode })
}
if (gallery.status == 'active' && gallery.showCount > 0) {
gallery.showCount = gallery.showCount - 1
gallery.showCounter = gallery.showCounter + 1
} else {
gallery.status = 'end'
}
await gallery.save()
return res.status(200).json(gallery)
} catch (err) {
next(err)
}
}
this.Click = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
const { id } = req.body
if (!isSet(id) || isEmpty(id)) {
throw new BadRequest({ lang: req.user?.languageCode })
}
const gallery = await db.gallery.findOne({
where: { id }
})
if (!gallery) {
throw new NotFound({ lang: req.user?.languageCode })
}
if (gallery.status == 'active') {
gallery.clickCounter = gallery.clickCounter + 1
gallery.ctr = gallery.showCounter / 100 * gallery.clickCounter;
}
await gallery.save()
return res.status(200).json(gallery)
} catch (err) {
next(err)
}
}
}