Spaces:
Sleeping
Sleeping
File size: 5,712 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 131 132 133 134 135 136 137 138 139 | import { Request, Response } from 'express';
import prisma from '../models/prisma';
import slugify from 'slugify';
import { z } from 'zod';
const portfolioSchema = z.object({
title: z.string().min(1, 'Title is required'),
slug: z.string().min(1, 'Slug is required').regex(/^[a-z0-9-]+$/, 'Slug must be lowercase letters, numbers, and hyphens only'),
description: z.string().min(1, 'Description is required'),
technologies: z.string().nullable().optional(),
imageUrl: z.string().nullable().optional(),
clientName: z.string().nullable().optional(),
industry: z.string().nullable().optional(),
testimonials: z.string().nullable().optional(),
});
// GET /api/admin/portfolio — List all portfolio items
export const getAllPortfolioItems = async (_req: Request, res: Response) => {
try {
const items = await prisma.portfolioItem.findMany({
orderBy: { createdAt: 'desc' },
include: { author: { select: { name: true, email: true } } },
});
return res.json(items);
} catch (error) {
console.error('Get portfolio items error:', error);
return res.status(500).json({ error: 'Internal server error' });
}
};
// GET /api/admin/portfolio/:id — Get single portfolio item
export const getPortfolioItem = async (req: Request, res: Response) => {
try {
const item = await prisma.portfolioItem.findUnique({
where: { id: req.params.id },
include: { author: { select: { name: true, email: true } } },
});
if (!item) return res.status(404).json({ error: 'Not found' });
return res.json(item);
} catch (error) {
console.error('Get portfolio item error:', error);
return res.status(500).json({ error: 'Internal server error' });
}
};
// POST /api/admin/portfolio — Create portfolio item
export const createPortfolioItem = async (req: Request, res: Response) => {
try {
const validatedData = portfolioSchema.parse(req.body);
const { title, slug, description, technologies, imageUrl, clientName, industry, testimonials } = validatedData;
// Check slug uniqueness
const existing = await prisma.portfolioItem.findUnique({ where: { slug } });
if (existing) {
return res.status(409).json({ error: 'Slug already exists' });
}
const item = await prisma.portfolioItem.create({
data: {
title,
slug,
description,
technologies: technologies || null,
imageUrl: imageUrl || null,
clientName: clientName || null,
industry: industry || null,
testimonials: testimonials || null,
},
});
return res.status(201).json(item);
} catch (error: any) {
if (error instanceof z.ZodError) {
return res.status(400).json({ error: error.errors[0].message });
}
console.error('Create portfolio item error:', error);
return res.status(500).json({ error: 'Internal server error' });
}
};
// PATCH /api/admin/portfolio/:id — Update portfolio item
export const updatePortfolioItem = async (req: Request, res: Response) => {
try {
const { id } = req.params;
const validatedData = portfolioSchema.partial().parse(req.body);
const { title, slug, description, technologies, imageUrl, clientName, industry, testimonials } = validatedData;
// Check item exists
const existing = await prisma.portfolioItem.findUnique({ where: { id } });
if (!existing) return res.status(404).json({ error: 'Not found' });
// Check slug uniqueness if changed
if (slug && slug !== existing.slug) {
const slugExists = await prisma.portfolioItem.findUnique({ where: { slug } });
if (slugExists) return res.status(409).json({ error: 'Slug already exists' });
}
const item = await prisma.portfolioItem.update({
where: { id },
data: {
...(title !== undefined && { title }),
...(slug !== undefined && { slug }),
...(description !== undefined && { description }),
...(technologies !== undefined && { technologies }),
...(imageUrl !== undefined && { imageUrl }),
...(clientName !== undefined && { clientName }),
...(industry !== undefined && { industry }),
...(testimonials !== undefined && { testimonials }),
},
});
return res.json(item);
} catch (error: any) {
if (error instanceof z.ZodError) {
return res.status(400).json({ error: error.errors[0].message });
}
console.error('Update portfolio item error:', error);
return res.status(500).json({ error: 'Internal server error' });
}
};
// DELETE /api/admin/portfolio/:id — Delete portfolio item
export const deletePortfolioItem = async (req: Request, res: Response) => {
try {
const { id } = req.params;
const existing = await prisma.portfolioItem.findUnique({ where: { id } });
if (!existing) return res.status(404).json({ error: 'Not found' });
await prisma.portfolioItem.delete({ where: { id } });
return res.json({ message: 'Deleted successfully' });
} catch (error) {
console.error('Delete portfolio item error:', error);
return res.status(500).json({ error: 'Internal server error' });
}
};
|