feat: Implement CMS module with API endpoints, data model, and initial seeding for landing page content.

This commit is contained in:
pradeepkumar
2026-02-22 21:52:16 +05:30
parent dd6636dbb3
commit 49bd08c5fe
10 changed files with 363 additions and 0 deletions

View File

@@ -16,6 +16,7 @@ import { UploadModule } from './upload';
import { ProfileFieldsModule } from './profile-fields';
import { ConnectionRequestsModule } from './connection-requests';
import { MessagesModule } from './messages';
import { CmsModule } from './cms';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@@ -55,6 +56,7 @@ import { AppService } from './app.service';
ProfileFieldsModule,
ConnectionRequestsModule,
MessagesModule,
CmsModule,
],
controllers: [AppController],
providers: [

44
src/cms/cms.controller.ts Normal file
View 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
View 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
View 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 } });
}
}

View 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
View File

@@ -0,0 +1,2 @@
export { CreateCmsContentDto } from './create-cms-content.dto';
export { UpdateCmsContentDto } from './update-cms-content.dto';

View 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
View File

@@ -0,0 +1,4 @@
export { CmsModule } from './cms.module';
export { CmsService } from './cms.service';
export { CmsController } from './cms.controller';
export * from './dto';