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

Add Role Router #25

Merged
merged 11 commits into from
Feb 4, 2024
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,4 @@ enum WorkTerm {
FALL
SPRING
SUMMER
}
}
4 changes: 3 additions & 1 deletion src/server/api/root.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { createTRPCRouter } from "~/server/api/trpc";
import { companyRouter } from "./routers/company";
import { roleRouter } from "~/server/api/routers/role";
import { companyRouter } from "~/server/api/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({
role: roleRouter,
company: companyRouter,
});

Expand Down
148 changes: 148 additions & 0 deletions src/server/api/routers/role.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import {
createTRPCRouter,
protectedProcedure,
publicProcedure,
} from "~/server/api/trpc";

export const roleRouter = createTRPCRouter({
list: publicProcedure.query(async ({ ctx }) => {
return await ctx.db.role.findMany();
}),
getById: publicProcedure
.input(
z.object({
id: z.string(),
}),
)
.query(async ({ ctx, input }) => {
const role = await ctx.db.role.findUnique({
where: {
id: input.id,
},
});

if (!role) {
throw new TRPCError({
code: "NOT_FOUND",
message: `Role with ID ${input.id} not found.`,
});
}

return role;
}),
getByTitle: publicProcedure
.input(
z.object({
title: z.string(),
}),
)
.query(async ({ ctx, input }) => {
return await ctx.db.role.findMany({
where: {
title: input.title,
},
});
}),
getByCompany: publicProcedure
.input(
z.object({
companyId: z.string(),
}),
)
.query(async ({ ctx, input }) => {
const company = await ctx.db.company.findUnique({
where: {
id: input.companyId,
},
include: {
roles: true,
},
});

if (!company) {
throw new TRPCError({
code: "BAD_REQUEST",
message: `Company with ID ${input.companyId} not found.`,
});
}

return company.roles;
}),
create: protectedProcedure
.input(
z.object({
title: z.string(),
description: z.string(),
companyId: z.string(),
}),
)
.mutation(async ({ ctx, input }) => {
return await ctx.db.role.create({
data: {
...input,
},
});
}),
update: protectedProcedure
.input(
z.object({
id: z.string(),
data: z.object({
title: z.string().optional(),
description: z.string().optional(),
companyId: z.string().optional(),
}),
}),
)
.mutation(async ({ ctx, input }) => {
const role = await ctx.db.role.findUnique({
where: {
id: input.id,
},
});

if (!role) {
throw new TRPCError({
code: "NOT_FOUND",
message: `Role with ID ${input.id} not found.`,
});
}

return await ctx.db.role.update({
where: {
id: input.id,
},
data: {
...input.data,
},
});
}),
delete: protectedProcedure
.input(
z.object({
id: z.string(),
}),
)
.mutation(async ({ ctx, input }) => {
const role = await ctx.db.role.findUnique({
where: {
id: input.id,
},
});

if (!role) {
throw new TRPCError({
code: "NOT_FOUND",
message: `Role with ID ${input.id} not found.`,
});
}

return await ctx.db.role.delete({
where: {
id: input.id,
},
});
}),
});
Loading