Skip to content

Commit

Permalink
Add Role Router (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
RishikeshNK authored Feb 4, 2024
1 parent 6eaebfb commit 59822cd
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/server/api/root.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createTRPCRouter } from "~/server/api/trpc";
import { roleRouter } from "~/server/api/routers/role";
import { profileRouter } from "~/server/api/routers/profile";
import { reviewRouter } from "~/server/api/routers/review";
import { companyRouter } from "~/server/api/routers/company";
Expand All @@ -9,6 +10,7 @@ import { companyRouter } from "~/server/api/routers/company";
* All routers added in /api/routers should be manually added here.
*/
export const appRouter = createTRPCRouter({
role: roleRouter,
profile: profileRouter,
review: reviewRouter,
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,
},
});
}),
});

0 comments on commit 59822cd

Please sign in to comment.