Skip to content

Commit

Permalink
feat: trpc modules router
Browse files Browse the repository at this point in the history
  • Loading branch information
ixahmedxi committed Jul 27, 2024
1 parent 9fb1937 commit cfc5f6d
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 88 deletions.
Binary file modified bun.lockb
Binary file not shown.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
}
},
"dependencies": {
"@clerk/nextjs": "^5.2.6",
"@clerk/themes": "^2.1.13",
"@clerk/nextjs": "^5.2.8",
"@clerk/themes": "^2.1.14",
"@hookform/resolvers": "^3.9.0",
"@neondatabase/serverless": "^0.9.4",
"@paralleldrive/cuid2": "^2.2.2",
Expand All @@ -68,7 +68,7 @@
"@radix-ui/react-slot": "^1.1.0",
"@react-email/components": "^0.0.22",
"@t3-oss/env-nextjs": "^0.11.0",
"@tanstack/react-query": "^5.51.11",
"@tanstack/react-query": "^5.51.15",
"@trpc/client": "next",
"@trpc/react-query": "next",
"@trpc/server": "next",
Expand Down Expand Up @@ -104,7 +104,7 @@
"@eslint-community/eslint-plugin-eslint-comments": "^4.3.0",
"@eslint/compat": "^1.1.1",
"@eslint/eslintrc": "^3.1.0",
"@eslint/js": "^9.7.0",
"@eslint/js": "^9.8.0",
"@happy-dom/global-registrator": "^14.12.3",
"@ianvs/prettier-plugin-sort-imports": "^4.3.1",
"@next/eslint-plugin-next": "^14.2.5",
Expand All @@ -121,7 +121,7 @@
"commitizen": "^4.3.0",
"cspell": "^8.12.1",
"drizzle-kit": "^0.23.0",
"eslint": "^9.7.0",
"eslint": "^9.8.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-jsdoc": "^48.8.3",
"eslint-plugin-jsx-a11y": "^6.9.0",
Expand All @@ -131,7 +131,7 @@
"eslint-plugin-security": "^3.0.1",
"eslint-plugin-tailwindcss": "^3.17.4",
"globals": "^15.8.0",
"husky": "^9.1.2",
"husky": "^9.1.3",
"lint-staged": "^15.2.7",
"markdownlint": "^0.34.0",
"markdownlint-cli": "^0.41.0",
Expand Down
79 changes: 0 additions & 79 deletions src/db/actions/modules.ts

This file was deleted.

17 changes: 17 additions & 0 deletions src/db/schema/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
timestamp,
uuid,
} from 'drizzle-orm/pg-core';
import { createInsertSchema, createSelectSchema } from 'drizzle-zod';
import { z } from 'zod';

export const modulesTable = pgTable('modules', {
id: uuid('id').primaryKey().unique().defaultRandom().notNull(),
Expand All @@ -21,3 +23,18 @@ export const modulesTable = pgTable('modules', {
modifiedAt: timestamp('modified_at').notNull().defaultNow(),
lastVisited: timestamp('last_visited').notNull().defaultNow(),
});

export const insertModuleSchema = createInsertSchema(modulesTable).extend({
id: z.string().min(1),
icon: z.string().default('default'),
color: z.string().default('default'),
archived: z.boolean().default(false),
credits: z.number().default(0),
createdAt: z.date().default(new Date()),
modifiedAt: z.date().default(new Date()),
lastVisited: z.date().default(new Date()),
});

export type InsertModuleInput = z.infer<typeof insertModuleSchema>;

export const selectModuleSchema = createSelectSchema(modulesTable);
2 changes: 2 additions & 0 deletions src/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { earlyAccessRouter } from './routers/early-access';
import { modulesRouter } from './routers/modules';
import { createCallerFactory, createRouter } from './trpc';

export const appRouter = createRouter({
earlyAccess: earlyAccessRouter,
modules: modulesRouter,
});

export type AppRouter = typeof appRouter;
Expand Down
125 changes: 125 additions & 0 deletions src/server/routers/modules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { createRouter, protectedProcedure } from '../trpc';
import {
insertModuleSchema,
modulesTable,
selectModuleSchema,
} from '@/db/schema';
import { and, eq } from 'drizzle-orm';

export const modulesRouter = createRouter({
getById: protectedProcedure
.input(selectModuleSchema.pick({ id: true }))
.query(async ({ ctx, input }) => {
const userId = ctx.user.id;

return ctx.db.query.modulesTable.findFirst({
where: (t, { and, eq }) =>
and(eq(t.id, input.id), eq(t.user_id, userId)),
});
}),

getUserModules: protectedProcedure.query(async ({ ctx }) => {
const userId = ctx.user.id;

return ctx.db.query.modulesTable.findMany({
where: (t, { eq }) => eq(t.user_id, userId),
orderBy: (t, { desc }) => desc(t.lastVisited),
});
}),

create: protectedProcedure
.input(
insertModuleSchema.pick({
name: true,
description: true,
code: true,
icon: true,
color: true,
credits: true,
}),
)
.mutation(async ({ ctx, input }) => {
const userId = ctx.user.id;

return ctx.db.insert(modulesTable).values({
...input,
user_id: userId,
});
}),

archive: protectedProcedure
.input(selectModuleSchema.pick({ id: true }))
.mutation(async ({ ctx, input }) => {
const userId = ctx.user.id;

return ctx.db
.update(modulesTable)
.set({ archived: true })
.where(
and(eq(modulesTable.id, input.id), eq(modulesTable.user_id, userId)),
);
}),

recover: protectedProcedure
.input(selectModuleSchema.pick({ id: true }))
.mutation(async ({ ctx, input }) => {
const userId = ctx.user.id;

return ctx.db
.update(modulesTable)
.set({ archived: false })
.where(
and(eq(modulesTable.id, input.id), eq(modulesTable.user_id, userId)),
);
}),

update: protectedProcedure
.input(
insertModuleSchema.pick({
color: true,
icon: true,
name: true,
description: true,
code: true,
credits: true,
id: true,
}),
)
.mutation(async ({ ctx, input }) => {
const userId = ctx.user.id;

const { id: moduleId, ...updateData } = input;

const existingModule = await ctx.db.query.modulesTable.findFirst({
where: (t, { and, eq }) =>
and(eq(t.id, moduleId), eq(t.user_id, userId)),
});

if (!existingModule) {
throw new Error('Module not found');
}

return ctx.db
.update(modulesTable)
.set({
...updateData,
modifiedAt: new Date(),
})
.where(
and(eq(modulesTable.id, moduleId), eq(modulesTable.user_id, userId)),
);
}),

updateLastVisited: protectedProcedure
.input(insertModuleSchema.pick({ id: true }))
.mutation(async ({ ctx, input }) => {
const userId = ctx.user.id;

return ctx.db
.update(modulesTable)
.set({ lastVisited: new Date() })
.where(
and(eq(modulesTable.id, input.id), eq(modulesTable.user_id, userId)),
);
}),
});
6 changes: 3 additions & 3 deletions src/server/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ export const createRouter = t.router;
export const publicProcedure = t.procedure;

export const protectedProcedure = t.procedure.use(async ({ next }) => {
const session = await currentUser();
const user = await currentUser();

if (!session) {
if (!user) {
throw new TRPCError({ code: 'UNAUTHORIZED' });
}

return next({
ctx: {
session: { ...session },
user: { ...user },
},
});
});

0 comments on commit cfc5f6d

Please sign in to comment.