/**
 *
 * This is an example router, you can delete this file and then update `../pages/api/trpc/[trpc].tsx`
 */ import { createRouter } from 'server/createRouter' ; import { z } from 'zod' ; import { TRPCError } from '@trpc/server' ; export const postRouter = createRouter ( ) . mutation ( 'add' , { input : z . object ( { id : z . string ( ) . uuid ( ) . optional ( ) , title : z . string ( ) . min ( 1 ) . max ( 32 ) , text : z . string ( ) . min ( 1 ) , } ) , async resolve ( { ctx , input } ) { const post = await ctx . prisma . post . create ( { data : input , } ) ; return post ; } , } ) . query ( 'all' , { async resolve ( { ctx } ) { link return ctx . prisma . post . findMany ( { select : { id : true , title : true , } , } ) ; } , } ) . query ( 'byId' , { input : z . object ( { id : z . string ( ) , } ) , async resolve ( { ctx , input } ) { const { id } = input ; const post = await ctx . prisma . post . findUnique ( { where : { id } , select : { id : true , title : true , text : true , createdAt : true , updatedAt : true , } , } ) ; if ( ! post ) { throw new TRPCError ( { code : 'NOT_FOUND' , message : `No post with id '${ id }'` , } ) ; } return post ; } , } ) . mutation ( 'edit' , { input : z . object ( { id : z . string ( ) . uuid ( ) , data : z . object ( { title : z . string ( ) . min ( 1 ) . max ( 32 ) . optional ( ) , text : z . string ( ) . min ( 1 ) . optional ( ) , } ) , } ) , async resolve ( { ctx , input } ) { const { id , data } = input ; const post = await ctx . prisma . post . update ( { where : { id } , data , } ) ; return post ; } , } ) . mutation ( 'delete' , { input : z . string ( ) . uuid ( ) , async resolve ( { input : id , ctx } ) { await ctx . prisma . post . delete ( { where : { id } } ) ; return id ; } , } ) ;