Spaces:
Sleeping
Sleeping
File size: 4,833 Bytes
d53e5b4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | import { Request, Response } from 'express';
import prisma from '../models/prisma';
// GET /api/admin/testimonials — List all testimonials
export const getAllTestimonials = async (_req: Request, res: Response) => {
try {
const items = await prisma.testimonial.findMany({
orderBy: { createdAt: 'desc' },
});
return res.json(items);
} catch (error) {
console.error('Get testimonials error:', error);
return res.status(500).json({ error: 'Internal server error' });
}
};
// GET /api/admin/testimonials/:id — Get single testimonial
export const getTestimonial = async (req: Request, res: Response) => {
try {
const item = await prisma.testimonial.findUnique({
where: { id: req.params.id },
});
if (!item) return res.status(404).json({ error: 'Not found' });
return res.json(item);
} catch (error) {
console.error('Get testimonial error:', error);
return res.status(500).json({ error: 'Internal server error' });
}
};
// POST /api/admin/testimonials — Create testimonial
export const createTestimonial = async (req: Request, res: Response) => {
try {
const { clientName, company, content, rating, imageUrl, approved } = req.body;
if (!clientName || !content) {
return res.status(400).json({ error: 'Client name and content are required' });
}
if (rating !== undefined && (rating < 1 || rating > 5)) {
return res.status(400).json({ error: 'Rating must be between 1 and 5' });
}
const item = await prisma.testimonial.create({
data: {
clientName,
company: company || null,
content,
rating: rating || 5,
imageUrl: imageUrl || null,
approved: approved ?? false,
},
});
return res.status(201).json(item);
} catch (error) {
console.error('Create testimonial error:', error);
return res.status(500).json({ error: 'Internal server error' });
}
};
// PATCH /api/admin/testimonials/:id — Update testimonial
export const updateTestimonial = async (req: Request, res: Response) => {
try {
const { id } = req.params;
const { clientName, company, content, rating, imageUrl, approved } = req.body;
const existing = await prisma.testimonial.findUnique({ where: { id } });
if (!existing) return res.status(404).json({ error: 'Not found' });
if (rating !== undefined && (rating < 1 || rating > 5)) {
return res.status(400).json({ error: 'Rating must be between 1 and 5' });
}
const item = await prisma.testimonial.update({
where: { id },
data: {
...(clientName !== undefined && { clientName }),
...(company !== undefined && { company }),
...(content !== undefined && { content }),
...(rating !== undefined && { rating }),
...(imageUrl !== undefined && { imageUrl }),
...(approved !== undefined && { approved }),
},
});
return res.json(item);
} catch (error) {
console.error('Update testimonial error:', error);
return res.status(500).json({ error: 'Internal server error' });
}
};
// PATCH /api/admin/testimonials/:id/toggle — Toggle approval status
export const toggleApproval = async (req: Request, res: Response) => {
try {
const { id } = req.params;
const existing = await prisma.testimonial.findUnique({ where: { id } });
if (!existing) return res.status(404).json({ error: 'Not found' });
const item = await prisma.testimonial.update({
where: { id },
data: { approved: !existing.approved },
});
return res.json(item);
} catch (error) {
console.error('Toggle approval error:', error);
return res.status(500).json({ error: 'Internal server error' });
}
};
// DELETE /api/admin/testimonials/:id — Delete testimonial
export const deleteTestimonial = async (req: Request, res: Response) => {
try {
const { id } = req.params;
const existing = await prisma.testimonial.findUnique({ where: { id } });
if (!existing) return res.status(404).json({ error: 'Not found' });
await prisma.testimonial.delete({ where: { id } });
return res.json({ message: 'Deleted successfully' });
} catch (error) {
console.error('Delete testimonial error:', error);
return res.status(500).json({ error: 'Internal server error' });
}
};
|