Skip to content

Commit

Permalink
feat(trpc): create profile router
Browse files Browse the repository at this point in the history
  • Loading branch information
RishikeshNK committed Feb 3, 2024
1 parent cedfc23 commit a8b5b4c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
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 { profileRouter } from "~/server/api/routers/profileRouter";

/**
* 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({
profile: profileRouter,
});

// export type definition of API
export type AppRouter = typeof appRouter;
Empty file removed src/server/api/routers/.gitkeep
Empty file.
78 changes: 78 additions & 0 deletions src/server/api/routers/profileRouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { z } from "zod";
import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc";

export const profileRouter = createTRPCRouter({
list: protectedProcedure.query(async ({ ctx }) => {
return await ctx.db.profile.findMany();
}),
getCurrentUser: protectedProcedure.query(async ({ ctx }) => {
return await ctx.db.profile.findMany({
where: {
userId: ctx.session.user.id,
},
});
}),
create: protectedProcedure
.input(
z.object({
firstName: z.string(),
lastName: z.string(),
major: z.string(),
minor: z.string().optional(),
// TODO: Update this to something more robust
graduationYear: z.number().min(2024).max(2030),
graduationMonth: z.number().min(1).max(12),
}),
)
.mutation(async ({ ctx, input }) => {
return await ctx.db.profile.create({
data: {
firstName: input.firstName,
lastName: input.lastName,
major: input.major,
minor: input.minor,
graduationYear: input.graduationYear,
graduationMonth: input.graduationMonth,
userId: ctx.session.user.id,
},
});
}),
update: protectedProcedure
.input(
z.object({
id: z.string(),
data: z.object({
firstName: z.string().optional(),
lastName: z.string().optional(),
major: z.string().optional(),
minor: z.string().optional(),
// TODO: Update this to something more robust
graduationYear: z.number().min(2024).max(2030).optional(),
graduationMonth: z.number().min(1).max(12).optional(),
}),
}),
)
.mutation(async ({ ctx, input }) => {
return await ctx.db.profile.update({
where: {
id: input.id,
},
data: {
...input.data,
},
});
}),
delete: protectedProcedure
.input(
z.object({
id: z.string(),
}),
)
.mutation(async ({ ctx, input }) => {
return await ctx.db.profile.delete({
where: {
id: input.id,
},
});
}),
});

0 comments on commit a8b5b4c

Please sign in to comment.