This commit is contained in:
2025-06-06 03:12:19 +02:00
parent be9a9af137
commit 0aab9f55d7
49 changed files with 3255 additions and 93 deletions

View File

@@ -1,8 +1,12 @@
import type { inferRouterOutputs } from "@trpc/server";
import { router } from "../trpc";
import { CvRouter } from "./cv";
import type { inferReactQueryProcedureOptions } from "@trpc/react-query";
export const trpcRouter = router({
cv: CvRouter
})
export type TrpcRouter = typeof trpcRouter
export type RouterOutputs = inferRouterOutputs<TrpcRouter>
export type ReactQueryOptions = inferReactQueryProcedureOptions<TrpcRouter>

View File

@@ -1,45 +1,74 @@
import { db } from "~/server/db";
import { publicProcedure, router } from "~/server/trpc";
import { cvCategory } from "~/server/db/schema";
import { createInsertSchema, createUpdateSchema, createSelectSchema} from 'drizzle-zod'
import { z } from 'zod'
import { cvCategory, cvEntry } from "~/server/db/schema";
import { eq } from "drizzle-orm";
import { isAdmin } from "~/app/actions";
const selectShema = createSelectSchema(cvCategory)
const insertShema = createInsertSchema(cvCategory)
const updateSchema = createUpdateSchema(cvCategory)
import * as Schemas from "~/lib/schema/cv/category"
import { TRPCError } from "@trpc/server";
import { z } from "zod";
export const CategoryRouter = router({
list: publicProcedure.query(async () => {
const categories = await db.query.cvCategory.findMany({
orderBy: (model, {desc} ) => desc(model.name)
orderBy: (model, { desc }) => desc(model.name),
with: {
cvEntry: true
}
});
return categories;
}),
get: publicProcedure.input(selectShema.pick({id: true})).query(async (opts) => {
get: publicProcedure.input(Schemas.getSchema).query(async (opts) => {
const { input } = opts
const category = await db.query.cvCategory.findFirst({
where: eq(cvCategory.id,input.id)
const categories = await db.query.cvCategory.findMany({
with: {
cvEntry: true
},
where: eq(cvCategory.id, input.id),
limit: 1
})
return category;
if (categories[0] !== undefined) {
return categories[0]
} else {
return null
}
}),
create: publicProcedure.input(insertShema).mutation(async (opts) => {
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(cvCategory).where(eq(cvCategory.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;
const category = await db.insert(cvCategory).values(input).returning().execute()
return category
}),
update: publicProcedure
.input(z.object({
by: selectShema.pick({id:true}),
update: updateSchema
}))
.input(Schemas.updateRouteSchema)
.mutation(async (opts) => {
const {input} = opts;
const category = await db.update(cvCategory)
.set(input.update)
.returning()
.where(eq(cvCategory.id,input.by.id))
return category
})
let admin = await isAdmin()
if (!admin) {
throw new TRPCError(
{ message: "Access denied", code: "FORBIDDEN", }
)
}
const { input } = opts;
const category = await db.update(cvCategory)
.set(input.update)
.returning()
.where(eq(cvCategory.id, input.by.id))
return category
})
})

View File

@@ -0,0 +1,74 @@
import { db } from "~/server/db";
import { publicProcedure, router } from "~/server/trpc";
import { cvEntry } from "~/server/db/schema";
import { eq } from "drizzle-orm";
import { isAdmin } from "~/app/actions";
import * as Schemas from "~/lib/schema/cv/entry"
import { TRPCError } from "@trpc/server";
import { z } from "zod";
export const EntryRouter = router({
list: publicProcedure.query(async () => {
const entries = await db.query.cvEntry.findMany({
orderBy: (model, { desc }) => desc(model.toTime),
with: {
category: true
}
});
return entries;
}),
get: publicProcedure.input(Schemas.getSchema).query(async (opts) => {
const { input } = opts
const entries = await db.query.cvEntry.findMany({
with: {
category: true
},
where: eq(cvEntry.id, input.id),
limit: 1
})
if (entries[0] !== undefined) {
return entries[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(cvEntry).where(eq(cvEntry.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;
const entry = await db.insert(cvEntry).values(input).returning().execute()
return entry
}),
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;
const entry = await db.update(cvEntry)
.set(input.update)
.returning()
.where(eq(cvEntry.id, input.by.id))
return entry
})
})

View File

@@ -1,6 +1,8 @@
import { router } from "~/server/trpc"
import { CategoryRouter } from "./category"
import { EntryRouter } from "./entry"
export const CvRouter = router({
category: CategoryRouter
category: CategoryRouter,
entry: EntryRouter
})