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' }); } };