35 lines
1.3 KiB
TypeScript
35 lines
1.3 KiB
TypeScript
import { publicProcedure, router } from "~/server/trpc";
|
|
import { db } from "~/server/db";
|
|
import { cvCategory } from "~/server/dbschema/schema";
|
|
import { eq } from "drizzle-orm";
|
|
import { z } from 'zod';
|
|
import { createInsertSchema, createUpdateSchema } from "drizzle-zod";
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
export const cvCategoryRouter = router({
|
|
listByLayoutPosition: publicProcedure.input(z.enum(cvCategory.layoutPosition.enumValues)).query(async ({input}) => {
|
|
console.log(input);
|
|
const res = await db.select().from(cvCategory).where(eq(cvCategory.layoutPosition,input));
|
|
console.log(res);
|
|
return res;
|
|
}),
|
|
getById: publicProcedure.input(z.string()).query(async ({input}) => {
|
|
const res = await db.query.cvCategory.findFirst({
|
|
with: {
|
|
cvEntry: true
|
|
},
|
|
where(fields, operators) {
|
|
return operators.eq(fields.id, input)
|
|
},
|
|
})
|
|
return res;
|
|
}),
|
|
insert: publicProcedure.input(createInsertSchema(cvCategory)).mutation(async ({input}) => {
|
|
let res = await db.insert(cvCategory).values(input).returning().execute();
|
|
return res.at(0)
|
|
}),
|
|
updateById: publicProcedure.input(z.object({id: z.string(),data: createUpdateSchema(cvCategory)})).mutation(async ({input}) => {
|
|
let res = await db.update(cvCategory).set(input.data).where(eq(cvCategory.id,input.id)).execute();
|
|
})
|
|
});
|