Skip to content

Commit

Permalink
item Read, Create and Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Romero committed Nov 17, 2023
1 parent 9340e6f commit 65bd8f9
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 41 deletions.
2 changes: 1 addition & 1 deletion packages/api/src/router/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { router } from "../trpc";
import { itemRouter } from "./post";
import { itemRouter } from "./item";
import { authRouter } from "./auth";

export const appRouter = router({
Expand Down
84 changes: 84 additions & 0 deletions packages/api/src/router/item.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { router, publicProcedure, protectedProcedure } from "../trpc";
import { z } from "zod";

export const itemRouter = router({
all: publicProcedure.query(({ ctx }) => {
return ctx.prisma.items.findMany({
include: {
category: {
select: {
id: true,
},
},
},
});
}),

byId: publicProcedure
.input(z.object({ id: z.number().positive() }))
.query(({ ctx, input }) => {
return ctx.prisma.items.findFirstOrThrow({
where: {
id: input.id,
},
include: {
category: {
select: {
id: true,
},
},
},
});
}),

create: protectedProcedure
.input(
z.object({
name: z.string(),
price: z.number().positive(),
stock: z.number(),
categoryId: z.number().positive(),
description: z.string().optional(),
}),
)
.mutation(({ ctx, input }) => {
return ctx.prisma.items.create({
data: input,
include: {
category: {
select: {
id: true,
},
},
},
});
}),

update: protectedProcedure
.input(
z.object({
id: z.number().positive(),
name: z.string().optional(),
price: z.number().positive().optional(),
stock: z.number().optional(),
categoryId: z.number().positive().optional(),
description: z.string().optional(),
}),
)
.mutation(({ ctx, input }) => {
const { id } = input;
return ctx.prisma.items.update({
where: {
id: id,
},
data: input,
include: {
category: {
select: {
id: true,
},
},
},
});
}),
});
8 changes: 0 additions & 8 deletions packages/api/src/router/post.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
"db-generate": "pnpm with-env prisma generate"
},
"dependencies": {
"@prisma/client": "^4.6.1"
"@prisma/client": "^5.6.0"
},
"devDependencies": {
"dotenv-cli": "^6.0.0",
"prisma": "^4.6.1",
"prisma": "^5.6.0",
"typescript": "^4.9.3"
}
}
61 changes: 31 additions & 30 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 65bd8f9

Please sign in to comment.