92 lines
2.2 KiB
TypeScript
92 lines
2.2 KiB
TypeScript
import { PrismaClient } from '@prisma/client';
|
|
import { logger } from '../config/logger.js';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
export const galleryService = {
|
|
createImage: async (data: {
|
|
url: string;
|
|
caption: string;
|
|
alt: string;
|
|
width: number;
|
|
height: number;
|
|
size: number;
|
|
format: string;
|
|
articleId: string;
|
|
order?: number;
|
|
}) => {
|
|
try {
|
|
const image = await prisma.galleryImage.create({
|
|
data
|
|
});
|
|
logger.info(`Created gallery image: ${image.id}`);
|
|
return image;
|
|
} catch (error) {
|
|
logger.error('Error creating gallery image:', error);
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
updateImage: async (
|
|
id: string,
|
|
data: {
|
|
caption?: string;
|
|
alt?: string;
|
|
order?: number;
|
|
}
|
|
) => {
|
|
try {
|
|
const image = await prisma.galleryImage.update({
|
|
where: { id },
|
|
data
|
|
});
|
|
logger.info(`Updated gallery image: ${id}`);
|
|
return image;
|
|
} catch (error) {
|
|
logger.error(`Error updating gallery image ${id}:`, error);
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
deleteImage: async (id: string) => {
|
|
try {
|
|
await prisma.galleryImage.delete({
|
|
where: { id }
|
|
});
|
|
logger.info(`Deleted gallery image: ${id}`);
|
|
} catch (error) {
|
|
logger.error(`Error deleting gallery image ${id}:`, error);
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
reorderImages: async (articleId: string, imageIds: string[]) => {
|
|
try {
|
|
await prisma.$transaction(
|
|
imageIds.map((id, index) =>
|
|
prisma.galleryImage.update({
|
|
where: { id },
|
|
data: { order: index }
|
|
})
|
|
)
|
|
);
|
|
logger.info(`Reordered gallery images for article: ${articleId}`);
|
|
} catch (error) {
|
|
logger.error(`Error reordering gallery images for article ${articleId}:`, error);
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
getArticleGallery: async (articleId: string) => {
|
|
try {
|
|
const images = await prisma.galleryImage.findMany({
|
|
where: { articleId },
|
|
orderBy: { order: 'asc' }
|
|
});
|
|
return images;
|
|
} catch (error) {
|
|
logger.error(`Error fetching gallery for article ${articleId}:`, error);
|
|
throw error;
|
|
}
|
|
}
|
|
}; |