Skip to content

Commit 19e3984

Browse files
Merge pull request #3 from MuriloMorandi/feat/pets
Criado as funcionalidade relacionadas ao pets
2 parents 03a55c9 + b01cb49 commit 19e3984

25 files changed

Lines changed: 669 additions & 3 deletions

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"test:coverage": "vitest run --coverage",
1616
"test:ui": "vitest --ui",
1717
"db:generate": "prisma generate",
18-
"db:migrate:dev": "prisma migrate dev",
18+
"db:migrate": "prisma migrate dev",
1919
"db:studio": "prisma studio"
2020
},
2121
"keywords": [],
@@ -30,6 +30,7 @@
3030
"zod": "^3.25.67"
3131
},
3232
"devDependencies": {
33+
"@faker-js/faker": "^9.9.0",
3334
"@types/node": "^24.0.4",
3435
"@vitest/coverage-v8": "3.2.4",
3536
"@vitest/ui": "3.2.4",

pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- CreateTable
2+
CREATE TABLE "pets" (
3+
"id" TEXT NOT NULL,
4+
"name" TEXT NOT NULL,
5+
"about" TEXT NOT NULL,
6+
"age" TEXT NOT NULL,
7+
"size" TEXT NOT NULL,
8+
"energy_level" TEXT NOT NULL,
9+
"environment" TEXT NOT NULL,
10+
"org_id" TEXT NOT NULL,
11+
12+
CONSTRAINT "pets_pkey" PRIMARY KEY ("id")
13+
);
14+
15+
-- AddForeignKey
16+
ALTER TABLE "pets" ADD CONSTRAINT "pets_org_id_fkey" FOREIGN KEY ("org_id") REFERENCES "orgs"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

prisma/schema.prisma

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,22 @@ model Orgs {
2525
latitude Decimal
2626
longitude Decimal
2727
28+
pets Pets[]
29+
2830
@@map("orgs")
2931
}
32+
33+
model Pets {
34+
id String @id @default(uuid())
35+
name String
36+
about String
37+
age String
38+
size String
39+
energy_level String
40+
environment String
41+
org_id String
42+
43+
org Orgs @relation(fields: [org_id], references: [id], onDelete: Restrict)
44+
45+
@@map("pets")
46+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { OrgNotFoundError } from "@/use-cases/errors/orgNotFoundError";
2+
import { makeCreatePetsUseCase } from "@/use-cases/factories/makeCreatePetsUseCase";
3+
import { FastifyReply, FastifyRequest } from "fastify";
4+
import { z } from "zod/v4";
5+
6+
export const createPetsController = async (
7+
request: FastifyRequest,
8+
reply: FastifyReply,
9+
) => {
10+
const createOrgsBodySchema = z.object({
11+
idOrg: z.string(),
12+
name: z.string(),
13+
about: z.string(),
14+
age: z.string(),
15+
size: z.string(),
16+
energy_level: z.string(),
17+
environment: z.string(),
18+
});
19+
20+
const bodyData = createOrgsBodySchema.parse(request.body);
21+
22+
try
23+
{
24+
const createOrgsUseCase = makeCreatePetsUseCase();
25+
await createOrgsUseCase.execute(bodyData);
26+
27+
} catch (error)
28+
{
29+
if (error instanceof OrgNotFoundError)
30+
{
31+
return reply.status(404).send(error.message);
32+
}
33+
34+
throw error;
35+
}
36+
37+
reply.status(201).send();
38+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { ResourceNotFoundError } from '@/use-cases/errors/resourceNotFound';
2+
import { makeGetPetsUseCase } from '@/use-cases/factories/makeGetPetsUseCase';
3+
import { FastifyReply, FastifyRequest } from "fastify";
4+
import { z } from "zod/v4";
5+
6+
export const getPetsController = async (
7+
request: FastifyRequest,
8+
reply: FastifyReply,
9+
) => {
10+
const getPetsQuerySchema = z.object({
11+
id: z.string()
12+
});
13+
14+
const paramsData = getPetsQuerySchema.parse(request.params);
15+
16+
try
17+
{
18+
const getPetsUseCase = makeGetPetsUseCase();
19+
const { pet } = await getPetsUseCase.execute(paramsData);
20+
21+
reply.status(200).send(pet);
22+
} catch (error)
23+
{
24+
if (error instanceof ResourceNotFoundError)
25+
{
26+
return reply.status(404).send(error.message);
27+
}
28+
29+
throw error;
30+
}
31+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { makeListPetsUseCase } from "@/use-cases/factories/makeListPetsUseCase";
2+
import { FastifyReply, FastifyRequest } from "fastify";
3+
import { z } from "zod/v4";
4+
5+
export const listPetsController = async (
6+
request: FastifyRequest,
7+
reply: FastifyReply,
8+
) => {
9+
const getPetsQuerySchema = z.object({
10+
city: z.string(),
11+
age: z.string().optional(),
12+
size: z.string().optional(),
13+
energy_level: z.string().optional(),
14+
environment: z.string().optional()
15+
});
16+
17+
const queryData = getPetsQuerySchema.parse(request.query);
18+
19+
try
20+
{
21+
const listPetsUseCase = makeListPetsUseCase();
22+
const { pets } = await listPetsUseCase.execute(queryData);
23+
24+
reply.status(200).send(pets);
25+
} catch (error)
26+
{
27+
throw error;
28+
}
29+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { FastifyInstance } from "fastify";
2+
import { createPetsController } from "./createPetsController";
3+
import { getPetsController } from "./getPetsController";
4+
import { listPetsController } from "./listPetsController";
5+
6+
7+
export const petsRoutes = async (app: FastifyInstance) => {
8+
app.post('/pets', createPetsController);
9+
app.get('/pets/:id', getPetsController);
10+
app.get('/pets', listPetsController);
11+
}

src/http/routes.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { FastifyInstance } from "fastify";
22
import { statusRoutes } from "./controllers/status/statusRouts";
33
import { orgsRoutes } from "./controllers/orgs/orgsRoutes";
4+
import { petsRoutes } from "./controllers/pets/petsRoutes";
45

56
export const appRoutes = async (app: FastifyInstance) => {
67
app.register(statusRoutes);
7-
app.register(orgsRoutes)
8+
app.register(orgsRoutes);
9+
app.register(petsRoutes);
810
}

src/http/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ app.setErrorHandler((error, _, reply) => {
1414
{
1515
return reply.status(400).send({
1616
message: 'Validation error',
17-
issues: error.format(),
17+
issues: error.issues,
1818
});
1919
}
2020

0 commit comments

Comments
 (0)