diff --git a/src/server/api/root.ts b/src/server/api/root.ts index 32643bb..dfc3e92 100644 --- a/src/server/api/root.ts +++ b/src/server/api/root.ts @@ -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; diff --git a/src/server/api/routers/.gitkeep b/src/server/api/routers/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/server/api/routers/profileRouter.ts b/src/server/api/routers/profileRouter.ts new file mode 100644 index 0000000..99f7b6d --- /dev/null +++ b/src/server/api/routers/profileRouter.ts @@ -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, + }, + }); + }), +});