Files
backend/src/cms/cms.service.ts

66 lines
1.9 KiB
TypeScript
Raw Normal View History

import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { CreateCmsContentDto, UpdateCmsContentDto } from './dto';
@Injectable()
export class CmsService {
constructor(private prisma: PrismaService) {}
async findByPage(pageSlug: string) {
return this.prisma.cmsContent.findMany({
where: { pageSlug, isPublished: true },
orderBy: { sectionKey: 'asc' },
});
}
async findByPageAndSection(pageSlug: string, sectionKey: string) {
const content = await this.prisma.cmsContent.findUnique({
where: { pageSlug_sectionKey: { pageSlug, sectionKey } },
});
if (!content) {
throw new NotFoundException(`CMS content not found for ${pageSlug}/${sectionKey}`);
}
return content;
}
async upsert(dto: CreateCmsContentDto) {
return this.prisma.cmsContent.upsert({
where: {
pageSlug_sectionKey: {
pageSlug: dto.pageSlug,
sectionKey: dto.sectionKey,
},
},
update: {
content: dto.content,
isPublished: dto.isPublished ?? true,
},
create: {
pageSlug: dto.pageSlug,
sectionKey: dto.sectionKey,
content: dto.content,
isPublished: dto.isPublished ?? true,
},
});
}
async update(id: string, dto: UpdateCmsContentDto) {
const existing = await this.prisma.cmsContent.findUnique({ where: { id } });
if (!existing) {
throw new NotFoundException(`CMS content with ID ${id} not found`);
}
return this.prisma.cmsContent.update({
where: { id },
data: dto,
});
}
async remove(id: string) {
const existing = await this.prisma.cmsContent.findUnique({ where: { id } });
if (!existing) {
throw new NotFoundException(`CMS content with ID ${id} not found`);
}
return this.prisma.cmsContent.delete({ where: { id } });
}
}