project router
This commit is contained in:
@@ -2,9 +2,11 @@ import type { inferRouterOutputs } from "@trpc/server";
|
||||
import { router } from "../trpc";
|
||||
import { CvRouter } from "./cv";
|
||||
import type { inferReactQueryProcedureOptions } from "@trpc/react-query";
|
||||
import { ProjectRouter } from "./project";
|
||||
|
||||
export const trpcRouter = router({
|
||||
cv: CvRouter
|
||||
cv: CvRouter,
|
||||
project: ProjectRouter
|
||||
})
|
||||
|
||||
export type TrpcRouter = typeof trpcRouter
|
||||
|
||||
@@ -51,8 +51,7 @@ export const EntryRouter = router({
|
||||
)
|
||||
}
|
||||
const { input } = opts;
|
||||
const entry = await db.insert(cvEntry).values(input).returning().execute()
|
||||
return entry
|
||||
return await db.insert(cvEntry).values(input).returning().execute()
|
||||
}),
|
||||
update: publicProcedure
|
||||
.input(Schemas.updateRouteSchema)
|
||||
|
||||
72
src/server/routers/project/index.ts
Normal file
72
src/server/routers/project/index.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import { publicProcedure, router } from "~/server/trpc"
|
||||
import { db } from "~/server/db";
|
||||
import * as Schemas from '~/lib/schema/project/project'
|
||||
import { eq } from "drizzle-orm";
|
||||
import { project } from "~/server/db/schema";
|
||||
import { isAdmin } from "~/app/actions";
|
||||
import z from "zod";
|
||||
import { StackRouter } from "./techStack";
|
||||
import { TRPCError, type inferRouterOutputs} from "@trpc/server";
|
||||
export const ProjectRouter = router({
|
||||
list: publicProcedure.query(async () => {
|
||||
return await db.query.project.findMany({
|
||||
orderBy: (model, {desc}) => desc(model.title),
|
||||
with: {
|
||||
techStack: true
|
||||
}
|
||||
});
|
||||
}),
|
||||
get: publicProcedure.input(Schemas.getSchema).query(async (opts) => {
|
||||
const {input} = opts;
|
||||
const projects = await db.query.project.findMany({
|
||||
with: {
|
||||
techStack: true
|
||||
},
|
||||
where: eq(project.id,input.id),
|
||||
limit: 1
|
||||
});
|
||||
if (projects[0] !== undefined) {
|
||||
return projects[0]
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}),
|
||||
delete: publicProcedure.input(z.string()).mutation(async (opts) => {
|
||||
let admin = await isAdmin()
|
||||
if (!admin) {
|
||||
throw new TRPCError(
|
||||
{ message: "Access denied", code: "FORBIDDEN", }
|
||||
)
|
||||
}
|
||||
const { input } = opts;
|
||||
db.delete(project).where(eq(project.id,input)).execute()
|
||||
}),
|
||||
create: publicProcedure.input(Schemas.insertSchema).mutation(async (opts) => {
|
||||
let admin = await isAdmin()
|
||||
if (!admin) {
|
||||
throw new TRPCError(
|
||||
{ message: "Access denied", code: "FORBIDDEN", }
|
||||
)
|
||||
}
|
||||
const { input } = opts;
|
||||
return await db.insert(project).values(input).returning().execute();
|
||||
}),
|
||||
update: publicProcedure
|
||||
.input(Schemas.updateRouteSchema)
|
||||
.mutation(async (opts) => {
|
||||
let admin = await isAdmin()
|
||||
if (!admin) {
|
||||
throw new TRPCError(
|
||||
{ message: "Access denied", code: "FORBIDDEN", }
|
||||
)
|
||||
}
|
||||
const { input } = opts;
|
||||
return await db.update(project)
|
||||
.set(input.update)
|
||||
.returning()
|
||||
.where(eq(project.id,input.by.id))
|
||||
}),
|
||||
stack: StackRouter,
|
||||
})
|
||||
|
||||
export type ProjectRouterOutputs = inferRouterOutputs<typeof ProjectRouter>
|
||||
62
src/server/routers/project/techStack.ts
Normal file
62
src/server/routers/project/techStack.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { publicProcedure, router } from "~/server/trpc"
|
||||
import { db } from "~/server/db";
|
||||
import * as Schemas from '~/lib/schema/project/techStack'
|
||||
import { eq } from "drizzle-orm";
|
||||
import { techStack } from "~/server/db/schema";
|
||||
import { isAdmin } from "~/app/actions";
|
||||
import { TRPCError, type inferRouterOutputs} from "@trpc/server";
|
||||
import z from "zod";
|
||||
export const StackRouter = router({
|
||||
list: publicProcedure.query(async () => {
|
||||
return await db.query.techStack.findMany();
|
||||
}),
|
||||
get: publicProcedure.input(Schemas.getSchema).query(async (opts) => {
|
||||
const {input} = opts;
|
||||
const techStacks = await db.query.techStack.findMany({
|
||||
where: eq(techStack.id,input.id),
|
||||
limit: 1
|
||||
});
|
||||
if (techStacks[0] !== undefined) {
|
||||
return techStacks[0]
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}),
|
||||
delete: publicProcedure.input(z.string()).mutation(async (opts) => {
|
||||
let admin = await isAdmin()
|
||||
if (!admin) {
|
||||
throw new TRPCError(
|
||||
{ message: "Access denied", code: "FORBIDDEN", }
|
||||
)
|
||||
}
|
||||
const { input } = opts;
|
||||
db.delete(techStack).where(eq(techStack.id,input)).execute()
|
||||
}),
|
||||
create: publicProcedure.input(Schemas.insertSchema).mutation(async (opts) => {
|
||||
let admin = await isAdmin()
|
||||
if (!admin) {
|
||||
throw new TRPCError(
|
||||
{ message: "Access denied", code: "FORBIDDEN", }
|
||||
)
|
||||
}
|
||||
const { input } = opts;
|
||||
return await db.insert(techStack).values(input).returning().execute();
|
||||
}),
|
||||
update: publicProcedure
|
||||
.input(Schemas.updateRouteSchema)
|
||||
.mutation(async (opts) => {
|
||||
let admin = await isAdmin()
|
||||
if (!admin) {
|
||||
throw new TRPCError(
|
||||
{ message: "Access denied", code: "FORBIDDEN", }
|
||||
)
|
||||
}
|
||||
const { input } = opts;
|
||||
return await db.update(techStack)
|
||||
.set(input.update)
|
||||
.returning()
|
||||
.where(eq(techStack.id,input.by.id))
|
||||
})
|
||||
})
|
||||
|
||||
export type StackRouterOutputs = inferRouterOutputs<typeof StackRouter>
|
||||
Reference in New Issue
Block a user