some fixes
This commit is contained in:
142
.history/routes/services_20251019204521.js
Normal file
142
.history/routes/services_20251019204521.js
Normal file
@@ -0,0 +1,142 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { Service, Portfolio } = require('../models');
|
||||
const { Op } = require('sequelize');
|
||||
|
||||
// Get all services
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const category = req.query.category;
|
||||
const featured = req.query.featured;
|
||||
|
||||
let whereClause = { isActive: true };
|
||||
|
||||
if (category && category !== 'all') {
|
||||
whereClause.category = category;
|
||||
}
|
||||
|
||||
if (featured === 'true') {
|
||||
whereClause.featured = true;
|
||||
}
|
||||
|
||||
const services = await Service.findAll({
|
||||
where: whereClause,
|
||||
order: [['featured', 'DESC'], ['order', 'ASC']]
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
services
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Services API error:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Error fetching services'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Get single service
|
||||
router.get('/:id', async (req, res) => {
|
||||
try {
|
||||
const service = await Service.findByPk(req.params.id);
|
||||
|
||||
if (!service || !service.isActive) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Service not found'
|
||||
});
|
||||
}
|
||||
|
||||
// Get related services
|
||||
const relatedServices = await Service.findAll({
|
||||
where: {
|
||||
id: { [Op.ne]: service.id },
|
||||
category: service.category,
|
||||
isActive: true
|
||||
},
|
||||
limit: 3
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
service,
|
||||
relatedServices
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Service detail API error:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Error fetching service'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Get service categories
|
||||
router.get('/meta/categories', async (req, res) => {
|
||||
try {
|
||||
const categories = await Service.distinct('category', { isActive: true });
|
||||
|
||||
// Get count for each category
|
||||
const categoriesWithCount = await Promise.all(
|
||||
categories.map(async (category) => {
|
||||
const count = await Service.countDocuments({
|
||||
category,
|
||||
isActive: true
|
||||
});
|
||||
return { category, count };
|
||||
})
|
||||
);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
categories: categoriesWithCount
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Service categories API error:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Error fetching categories'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Search services
|
||||
router.get('/search/:term', async (req, res) => {
|
||||
try {
|
||||
const searchTerm = req.params.term;
|
||||
const limit = parseInt(req.query.limit) || 10;
|
||||
|
||||
const services = await Service.find({
|
||||
$and: [
|
||||
{ isActive: true },
|
||||
{
|
||||
$or: [
|
||||
{ name: { $regex: searchTerm, $options: 'i' } },
|
||||
{ description: { $regex: searchTerm, $options: 'i' } },
|
||||
{ tags: { $in: [new RegExp(searchTerm, 'i')] } }
|
||||
]
|
||||
}
|
||||
]
|
||||
})
|
||||
.select('name shortDescription icon pricing category')
|
||||
.sort({ featured: -1, order: 1 })
|
||||
.limit(limit);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
services,
|
||||
searchTerm,
|
||||
count: services.length
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Service search API error:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Error searching services'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user