diff --git a/prisma/schema.prisma b/prisma/schema.prisma index a1bec55..5f82df5 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -96,7 +96,7 @@ model Role { reviews Review[] - Company Company? @relation(fields: [companyId], references: [id]) + Company Company? @relation(fields: [companyId], references: [id], onDelete: Cascade) companyId String? } @@ -120,7 +120,7 @@ model Review { overtimeNormal Boolean createdAt DateTime @default(now()) - Role Role? @relation(fields: [roleId], references: [id]) + Role Role? @relation(fields: [roleId], references: [id], onDelete: Cascade) roleId String? Profile Profile? @relation(fields: [profileId], references: [id]) diff --git a/src/server/api/root.ts b/src/server/api/root.ts index 32643bb..32886d2 100644 --- a/src/server/api/root.ts +++ b/src/server/api/root.ts @@ -1,11 +1,14 @@ import { createTRPCRouter } from "~/server/api/trpc"; +import { companyRouter } from "./routers/company"; /** * This is the primary router for your server. * * All routers added in /api/routers should be manually added here. */ -export const appRouter = createTRPCRouter({}); +export const appRouter = createTRPCRouter({ + company: companyRouter, +}); // export type definition of API export type AppRouter = typeof appRouter; diff --git a/src/server/api/routers/.gitkeep b/src/server/api/routers/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/server/api/routers/company.ts b/src/server/api/routers/company.ts new file mode 100644 index 0000000..779215a --- /dev/null +++ b/src/server/api/routers/company.ts @@ -0,0 +1,80 @@ +import { z } from "zod"; +import { + createTRPCRouter, + protectedProcedure, + publicProcedure, +} from "~/server/api/trpc"; +import { Industry } from "@prisma/client"; + +export const companyRouter = createTRPCRouter({ + list: publicProcedure.query(({ ctx }) => { + return ctx.db.company.findMany(); + }), + getByName: publicProcedure + .input( + z.object({ + name: z.string(), + }), + ) + .query(({ ctx, input }) => { + return ctx.db.company.findFirst({ + where: { + name: input.name, + }, + }); + }), + delete: protectedProcedure + .input( + z.object({ + id: z.string(), + }), + ) + .mutation(({ ctx, input }) => { + return ctx.db.company.delete({ + where: { + id: input.id, + }, + }); + }), + create: protectedProcedure + .input( + z.object({ + name: z.string(), + description: z.string().optional(), + industry: z.nativeEnum(Industry), + location: z.string(), + }), + ) + .mutation(({ ctx, input }) => { + return ctx.db.company.create({ + data: { + name: input.name, + description: input.description, + industry: input.industry, + location: input.location, + }, + }); + }), + update: protectedProcedure + .input( + z.object({ + id: z.string(), + data: z.object({ + name: z.string().optional(), + description: z.string().optional(), + industry: z.nativeEnum(Industry).optional(), + location: z.string().optional(), + }), + }), + ) + .mutation(({ ctx, input }) => { + return ctx.db.company.update({ + where: { + id: input.id, + }, + data: { + ...input.data, + }, + }); + }), +});