mirror of
https://github.com/khairul169/code-share.git
synced 2026-10-24 11:53:58 +07:00
feat: add project schema
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import { router } from "../api/trpc";
|
||||
import project from "./project";
|
||||
import file from "./file";
|
||||
|
||||
export const appRouter = router({
|
||||
project,
|
||||
file,
|
||||
});
|
||||
|
||||
|
||||
@@ -8,17 +8,19 @@ import { TRPCError } from "@trpc/server";
|
||||
const fileRouter = router({
|
||||
getAll: procedure
|
||||
.input(
|
||||
z
|
||||
.object({ id: z.number().array().min(1), isPinned: z.boolean() })
|
||||
.partial()
|
||||
.optional()
|
||||
z.object({
|
||||
projectId: z.number(),
|
||||
id: z.number().array().min(1).optional(),
|
||||
isPinned: z.boolean().optional(),
|
||||
})
|
||||
)
|
||||
.query(async ({ input: opt }) => {
|
||||
const files = await db.query.file.findMany({
|
||||
where: and(
|
||||
eq(file.projectId, opt.projectId),
|
||||
isNull(file.deletedAt),
|
||||
opt?.id && opt.id.length > 0 ? inArray(file.id, opt.id) : undefined,
|
||||
opt?.isPinned ? eq(file.isPinned, true) : undefined
|
||||
opt.id && opt.id.length > 0 ? inArray(file.id, opt.id) : undefined,
|
||||
opt.isPinned ? eq(file.isPinned, true) : undefined
|
||||
),
|
||||
orderBy: [desc(file.isDirectory), asc(file.filename)],
|
||||
columns: !file.isPinned ? { content: true } : undefined,
|
||||
@@ -55,7 +57,7 @@ const fileRouter = router({
|
||||
}
|
||||
|
||||
const data: z.infer<typeof insertFileSchema> = {
|
||||
userId: 1,
|
||||
projectId: 1,
|
||||
parentId: input.parentId,
|
||||
path: basePath + input.filename,
|
||||
filename: input.filename,
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
import { and, desc, eq, isNull, sql } from "drizzle-orm";
|
||||
import db from "../db";
|
||||
import { procedure, router } from "../api/trpc";
|
||||
import {
|
||||
project,
|
||||
insertProjectSchema,
|
||||
selectProjectSchema,
|
||||
} from "../db/schema/project";
|
||||
import { z } from "zod";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { uid } from "../lib/utils";
|
||||
|
||||
const projectRouter = router({
|
||||
getAll: procedure.query(async () => {
|
||||
const where = and(isNull(project.deletedAt));
|
||||
|
||||
const projects = await db.query.project.findMany({
|
||||
where,
|
||||
with: {
|
||||
user: { columns: { password: false } },
|
||||
},
|
||||
orderBy: [desc(project.id)],
|
||||
});
|
||||
|
||||
return projects;
|
||||
}),
|
||||
|
||||
getById: procedure
|
||||
.input(z.number().or(z.string()))
|
||||
.query(async ({ input }) => {
|
||||
const where = and(
|
||||
typeof input === "string"
|
||||
? eq(project.slug, input)
|
||||
: eq(project.id, input),
|
||||
isNull(project.deletedAt)
|
||||
);
|
||||
|
||||
return db.query.project.findFirst({
|
||||
where,
|
||||
with: {
|
||||
user: { columns: { password: false } },
|
||||
},
|
||||
});
|
||||
}),
|
||||
|
||||
create: procedure
|
||||
.input(
|
||||
insertProjectSchema.pick({
|
||||
title: true,
|
||||
})
|
||||
)
|
||||
.mutation(async ({ input }) => {
|
||||
const data: z.infer<typeof insertProjectSchema> = {
|
||||
userId: 1,
|
||||
title: input.title,
|
||||
slug: uid(),
|
||||
};
|
||||
|
||||
const [result] = await db.insert(project).values(data).returning();
|
||||
return result;
|
||||
}),
|
||||
|
||||
update: procedure
|
||||
.input(selectProjectSchema.partial().required({ id: true }))
|
||||
.mutation(async ({ input }) => {
|
||||
const projectData = await db.query.project.findFirst({
|
||||
where: and(eq(project.id, input.id), isNull(project.deletedAt)),
|
||||
});
|
||||
if (!projectData) {
|
||||
throw new TRPCError({ code: "NOT_FOUND" });
|
||||
}
|
||||
|
||||
const [result] = await db
|
||||
.update(project)
|
||||
.set(input)
|
||||
.where(and(eq(project.id, input.id), isNull(project.deletedAt)))
|
||||
.returning();
|
||||
|
||||
return result;
|
||||
}),
|
||||
|
||||
delete: procedure.input(z.number()).mutation(async ({ input }) => {
|
||||
const projectData = await db.query.project.findFirst({
|
||||
where: and(eq(project.id, input), isNull(project.deletedAt)),
|
||||
});
|
||||
if (!projectData) {
|
||||
throw new TRPCError({ code: "NOT_FOUND" });
|
||||
}
|
||||
|
||||
const [result] = await db
|
||||
.update(project)
|
||||
.set({ deletedAt: sql`CURRENT_TIMESTAMP` })
|
||||
.where(eq(project.id, projectData.id))
|
||||
.returning();
|
||||
|
||||
return result;
|
||||
}),
|
||||
});
|
||||
|
||||
export default projectRouter;
|
||||
Reference in New Issue
Block a user