Skip to content

Commit

Permalink
Company router (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
banushi-a authored Feb 3, 2024
1 parent cedfc23 commit 397f22a
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 3 deletions.
4 changes: 2 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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?
}

Expand All @@ -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])
Expand Down
5 changes: 4 additions & 1 deletion src/server/api/root.ts
Original file line number Diff line number Diff line change
@@ -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;
Empty file removed src/server/api/routers/.gitkeep
Empty file.
80 changes: 80 additions & 0 deletions src/server/api/routers/company.ts
Original file line number Diff line number Diff line change
@@ -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,
},
});
}),
});

0 comments on commit 397f22a

Please sign in to comment.