feat: Implement CMS module with API endpoints, data model, and initial seeding for landing page content.
This commit is contained in:
44
src/cms/cms.controller.ts
Normal file
44
src/cms/cms.controller.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { Controller, Get, Put, Patch, Delete, Param, Body } from '@nestjs/common';
|
||||
import { UserRole } from '@prisma/client';
|
||||
import { CmsService } from './cms.service';
|
||||
import { CreateCmsContentDto, UpdateCmsContentDto } from './dto';
|
||||
import { Public } from '../auth/decorators/public.decorator';
|
||||
import { Roles } from '../auth/decorators/roles.decorator';
|
||||
|
||||
@Controller('cms')
|
||||
export class CmsController {
|
||||
constructor(private readonly cmsService: CmsService) {}
|
||||
|
||||
@Public()
|
||||
@Get('page/:pageSlug')
|
||||
findByPage(@Param('pageSlug') pageSlug: string) {
|
||||
return this.cmsService.findByPage(pageSlug);
|
||||
}
|
||||
|
||||
@Public()
|
||||
@Get('page/:pageSlug/:sectionKey')
|
||||
findByPageAndSection(
|
||||
@Param('pageSlug') pageSlug: string,
|
||||
@Param('sectionKey') sectionKey: string,
|
||||
) {
|
||||
return this.cmsService.findByPageAndSection(pageSlug, sectionKey);
|
||||
}
|
||||
|
||||
@Roles(UserRole.ADMIN)
|
||||
@Put('upsert')
|
||||
upsert(@Body() dto: CreateCmsContentDto) {
|
||||
return this.cmsService.upsert(dto);
|
||||
}
|
||||
|
||||
@Roles(UserRole.ADMIN)
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() dto: UpdateCmsContentDto) {
|
||||
return this.cmsService.update(id, dto);
|
||||
}
|
||||
|
||||
@Roles(UserRole.ADMIN)
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.cmsService.remove(id);
|
||||
}
|
||||
}
|
||||
12
src/cms/cms.module.ts
Normal file
12
src/cms/cms.module.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { CmsController } from './cms.controller';
|
||||
import { CmsService } from './cms.service';
|
||||
import { PrismaModule } from '../prisma';
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [CmsController],
|
||||
providers: [CmsService],
|
||||
exports: [CmsService],
|
||||
})
|
||||
export class CmsModule {}
|
||||
65
src/cms/cms.service.ts
Normal file
65
src/cms/cms.service.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
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 } });
|
||||
}
|
||||
}
|
||||
20
src/cms/dto/create-cms-content.dto.ts
Normal file
20
src/cms/dto/create-cms-content.dto.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { IsString, IsNotEmpty, IsOptional, IsBoolean, IsObject } from 'class-validator';
|
||||
import { Prisma } from '@prisma/client';
|
||||
|
||||
export class CreateCmsContentDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
pageSlug: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
sectionKey: string;
|
||||
|
||||
@IsObject()
|
||||
@IsNotEmpty()
|
||||
content: Prisma.InputJsonValue;
|
||||
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
isPublished?: boolean;
|
||||
}
|
||||
2
src/cms/dto/index.ts
Normal file
2
src/cms/dto/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export { CreateCmsContentDto } from './create-cms-content.dto';
|
||||
export { UpdateCmsContentDto } from './update-cms-content.dto';
|
||||
12
src/cms/dto/update-cms-content.dto.ts
Normal file
12
src/cms/dto/update-cms-content.dto.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { IsOptional, IsBoolean, IsObject } from 'class-validator';
|
||||
import { Prisma } from '@prisma/client';
|
||||
|
||||
export class UpdateCmsContentDto {
|
||||
@IsObject()
|
||||
@IsOptional()
|
||||
content?: Prisma.InputJsonValue;
|
||||
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
isPublished?: boolean;
|
||||
}
|
||||
4
src/cms/index.ts
Normal file
4
src/cms/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export { CmsModule } from './cms.module';
|
||||
export { CmsService } from './cms.service';
|
||||
export { CmsController } from './cms.controller';
|
||||
export * from './dto';
|
||||
Reference in New Issue
Block a user