Software: Apache. PHP/8.1.30 uname -a: Linux server1.tuhinhossain.com 5.15.0-163-generic #173-Ubuntu SMP Tue Oct 14 17:51:00 UTC uid=1002(picotech) gid=1003(picotech) groups=1003(picotech),0(root) Safe-mode: OFF (not secure) /home/picotech/domains/rentals.picotech.app/public_html/server/routes/ drwxr-xr-x | |
| Viewing file: Select action/file-type: import express from 'express';
import Category from '../models/Category.js';
import { Activity } from '../models/index.js';
import { auth, authorize } from '../middleware/auth.js';
import slugify from 'slugify';
import { categoryValidators } from '../middleware/validators.js';
const router = express.Router();
// Get all categories
router.get('/', auth, async (req, res) => {
try {
const categories = await Category.findAll({
order: [['created_at', 'DESC']],
});
res.json(categories);
} catch (err) {
console.error('Get categories error:', err);
res.status(500).json({ message: 'Failed to fetch categories' });
}
});
// Get single category
router.get('/:id', auth, async (req, res) => {
try {
const category = await Category.findByPk(req.params.id);
if (!category) return res.status(404).json({ message: 'Category not found' });
res.json(category);
} catch (err) {
res.status(500).json({ message: 'Error fetching category' });
}
});
// Create category
router.post('/', auth, authorize(['manage']), categoryValidators.create, async (req, res) => {
try {
const slug = slugify(req.body.name, { lower: true, strict: true });
const category = await Category.create({
...req.body,
slug,
});
await Activity.create({
model_id: category.id,
model_name: 'Category',
description: `Category "${category.name}" created`,
});
res.status(201).json(category);
} catch (err) {
console.error('Create category error:', err);
res.status(500).json({ message: 'Failed to create category' });
}
});
// Update category
router.put('/:id', auth, authorize(['manage']), categoryValidators.update, async (req, res) => {
try {
const category = await Category.findByPk(req.params.id);
if (!category) return res.status(404).json({ message: 'Category not found' });
const slug = slugify(req.body.name, { lower: true, strict: true });
await category.update({
...req.body,
slug,
});
await Activity.create({
model_id: category.id,
model_name: 'Category',
description: `Category "${category.name}" updated`,
});
res.json(category);
} catch (err) {
console.error('Update category error:', err);
res.status(500).json({ message: 'Failed to update category' });
}
});
// Delete category
router.delete('/:id', auth, authorize(['manage']), async (req, res) => {
try {
const category = await Category.findByPk(req.params.id);
if (!category) return res.status(404).json({ message: 'Category not found' });
await Activity.create({
model_id: category.id,
model_name: 'Category',
description: `Category "${category.name}" deleted`,
});
await category.destroy();
res.status(204).send();
} catch (err) {
console.error('Delete category error:', err);
res.status(500).json({ message: 'Failed to delete category' });
}
});
export default router;
|
:: Command execute :: | |
--[ c99shell v. 2.5 [PHP 8 Update] [24.05.2025] | Generation time: 0.0033 ]-- |