import { Controller , Get , Param , Post , Body , Put , Delete , Query , } from '@nestjs/common' ; import { PrismaService } from './prisma.service' ; import { User as UserModel , Post as PostModel , Prisma } from '@prisma/client' ; @ Controller ( ) export class AppController { constructor ( private readonly prismaService ) { } @ Get ( 'post/:id' ) async getPostById ( @ Param ( 'id' ) id ) { return this . prismaService . post . findUnique ( { where : { id : Number ( id ) } } ) ; } @ Get ( 'feed' ) async getFilteredPosts ( @ Query ( 'take' ) take ? , @ Query ( 'skip' ) skip ? , @ Query ( 'searchString' ) searchString ? , @ Query ( 'orderBy' ) orderBy ? ) { const or = searchString ? { OR : [ { title : { contains : searchString } } , { content : { contains : searchString } } , ] , } : { } ; return this . prismaService . post . findMany ( { where : { published : true , ... or , } , include : { author : true } , take : Number ( take ) || undefined , skip : Number ( skip ) || undefined , orderBy : { updatedAt : orderBy , } , } ) ; } @ Get ( 'users' ) async getAllUsers ( ) { return this . prismaService . user . findMany ( ) ; } @ Get ( 'user/:id/drafts' ) async getDraftsByUser ( @ Param ( 'id' ) id ) { return this . prismaService . user . findUnique ( { where : { id : Number ( id ) } , } ) . posts ( { where : { published : false , } , } ) ; } @ Post ( 'post' ) async createDraft ( @ Body ( ) postData ) { const { title , content , authorEmail } = postData ; return this . prismaService . post . create ( { data : { title , content , author : { connect : { email : authorEmail } , } , } , } ) ; } @ Post ( 'signup' ) async signupUser ( @ Body ( ) userData ) { const postData = userData . posts ?. map ( ( post ) => { return { title : post ?. title , content : post ?. content } ; } ) ; return this . prismaService . user . create ( { data : { name : userData ?. name , email : userData . email , posts : { create : postData , } , } , } ) ; } @ Put ( 'publish/:id' ) async togglePublishPost ( @ Param ( 'id' ) id ) { const postData = await this . prismaService . post . findUnique ( { where : { id : Number ( id ) } , select : { published : true , } , } ) ; return this . prismaService . post . update ( { where : { id : Number ( id ) || undefined } , data : { published : ! postData ?. published } , } ) ; } @ Delete ( 'post/:id' ) async deletePost ( @ Param ( 'id' ) id ) { return this . prismaService . post . delete ( { where : { id : Number ( id ) } } ) ; } @ Put ( '/post/:id/views' ) async incrementPostViewCount ( @ Param ( 'id' ) id ) { return this . prismaService . post . update ( { where : { id : Number ( id ) } , data : { viewCount : { increment : 1 , } , } , } ) ; } }