Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Company router #23

Merged
merged 8 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
},
});
}),
});
Loading