move trpc to api

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-04-23 08:21:15 +02:00
parent 8207f15a83
commit ec8bf02fb9
37 changed files with 497 additions and 156 deletions

View File

@@ -0,0 +1,66 @@
import { z } from 'zod';
import { db, getId, getProjectsByOrganizationSlug } from '@openpanel/db';
import { createTRPCRouter, protectedProcedure } from '../trpc';
export const projectRouter = createTRPCRouter({
list: protectedProcedure
.input(
z.object({
organizationSlug: z.string().nullable(),
})
)
.query(async ({ input: { organizationSlug } }) => {
if (organizationSlug === null) return [];
return getProjectsByOrganizationSlug(organizationSlug);
}),
update: protectedProcedure
.input(
z.object({
id: z.string(),
name: z.string(),
})
)
.mutation(({ input }) => {
return db.project.update({
where: {
id: input.id,
},
data: {
name: input.name,
},
});
}),
create: protectedProcedure
.input(
z.object({
name: z.string().min(1),
organizationSlug: z.string(),
})
)
.mutation(async ({ input: { name, organizationSlug } }) => {
return db.project.create({
data: {
id: await getId('project', name),
organizationSlug: organizationSlug,
name: name,
},
});
}),
remove: protectedProcedure
.input(
z.object({
id: z.string(),
})
)
.mutation(async ({ input }) => {
await db.project.delete({
where: {
id: input.id,
},
});
return true;
}),
});