| import { Request, Response } from 'express';
|
| import prisma from '../models/prisma';
|
|
|
|
|
| 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' });
|
| }
|
| };
|
|
|
|
|
| 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' });
|
| }
|
| };
|
|
|
|
|
| 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' });
|
| }
|
| };
|
|
|
|
|
| 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' });
|
| }
|
| };
|
|
|
|
|
| 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' });
|
| }
|
| };
|
|
|
|
|
| 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' });
|
| }
|
| };
|
|
|