import { PrismaClient , FindManyPostArgs } from "@prisma/client" ; import { PaginationArgs , offsetToPrismaPagination } from "./utils" ; export class PostService { constructor ( protected prisma ) { } async findOne ( params ) { const user = await this . prisma . post . findOne ( { where : { id : params . id } } ) ; if ( ! user ) { throw new Error ( "could not find post with id: " + params . id ) ; } return user ; } async findMany ( params ) { return this . prisma . post . findMany ( { ... offsetToPrismaPagination ( params ) , orderBy : params . orderBy ?? undefined , } ) ; } async count ( ) { return this . prisma . post . count ( ) ; } async create ( params ) { return this . prisma . post . create ( { data : params , } ) ; } async votePost ( params ) { const currentPost = await this . findOne ( { id : params . id } ) ; const updatePost = await this . prisma . post . update ( { where : { id : params . id } , data : { votes : currentPost . votes + 1 } , } ) ; return updatePost ; } }