Skip to content

Commit

Permalink
feat: get pets by id
Browse files Browse the repository at this point in the history
  • Loading branch information
fcbento committed Dec 16, 2024
1 parent cef5d82 commit e8166d6
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ $ npm run test:e2e
### Application Rules

- [x] It must be possible to register a pet
- [ ] It must be possible to list all pets available for adoption in a city
- [x] It must be possible to list all pets available for adoption in a city
- [ ] It must be possible to filter pets by their characteristics
- [ ] It must be possible to view details of a pet available for adoption
- [x] It must be possible to register as an ORG
- [x] It must be possible to log in as an ORG

### Business Rules

- [ ] To list the pets, it is mandatory to provide the city
- [x] To list the pets, it is mandatory to provide the city
- [x] An ORG must have an address and a WhatsApp number
- [x] A pet must be linked to an ORG
- [ ] The user who wants to adopt will contact the ORG via WhatsApp
Expand Down
63 changes: 63 additions & 0 deletions src/controllers/pets/e2e/get-pet-controllet.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { app } from "@/app";
import { prisma } from "@/lib/prisma";
import { AddressResponse, getAddressByCep } from "@/services/cep/cep-api-service";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import request from 'supertest'

describe('Get pet by id', () => {

beforeAll(async () => {
await app.ready()
})

afterAll(async () => {
await app.close()
})

it('should get pet by id', async () => {
const address = await getAddressByCep('08560200') as AddressResponse

const org = await prisma.org.create({
data: {
cep: '08560200',
email: '[email protected]',
name: 'OEG TEST',
password_hash: '123456',
whatsapp: '119827464',
bairro: address.bairro,
estado: address.estado,
localidade: address.localidade,
logradouro: address.logradouro,
uf: address.uf
}
})

const petAddress = await getAddressByCep('13423790') as AddressResponse

const pet = await prisma.pet.create({
data: {
age: 'Small',
cep: '13423790',
description: 'A really nice cat',
energy: 'Active',
environment: 'All',
name: 'Harry',
org_id: org.id,
size: 'Big',
localidade: petAddress.localidade,
bairro: petAddress.bairro,
estado: petAddress.estado,
logradouro: petAddress.logradouro,
uf: petAddress.uf,
}
})

const response = await request(app.server).get(`/pets/single/${pet.id}`)

expect(response.body.pet).toEqual(expect.objectContaining({
localidade: expect.any(String)
}))
expect(response.body.pet.localidade).toEqual(petAddress.localidade)
expect(response.statusCode).toEqual(200)
})
})
16 changes: 16 additions & 0 deletions src/controllers/pets/get-pet-controllet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { GetPetFacotry } from "@/services/pets/factories/get-pet-factory";
import { FastifyReply, FastifyRequest } from "fastify";
import { z } from "zod";

export async function getPetById(request: FastifyRequest, reply: FastifyReply){

const getPetBodySchema = z.object({
id: z.string(),
})

const { id } = getPetBodySchema.parse(request.params)
const getPetService = GetPetFacotry()

const pet = await getPetService.execute({ id })
reply.send(pet).status(200)
}
2 changes: 2 additions & 0 deletions src/controllers/pets/routes.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { FastifyInstance } from "fastify";
import { registerPet } from "./register-pet-controller";
import { listPet } from "./list-pet-controller";
import { getPetById } from "./get-pet-controllet";

export async function petRoutes(app: FastifyInstance) {
app.post('/pets', registerPet)
app.get('/pets/:city', listPet)
app.get('/pets/single/:id', getPetById)
}
9 changes: 9 additions & 0 deletions src/services/pets/factories/get-pet-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { PrismaPetsRepository } from "@/repositories/pets/pets-repository"
import { GetPetService } from "../get-pet-service"

export function GetPetFacotry(){
const petsRepository = new PrismaPetsRepository()
const getPetService = new GetPetService(petsRepository)

return getPetService
}
20 changes: 20 additions & 0 deletions src/services/pets/get-pet-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Pet } from "@prisma/client";
import { PetsRepository } from '../../repositories/pets/prisma-pets-repository';

interface GetPetRequest {
id: string;
}

interface GetPetResponse {
pet: Pet | null
}

export class GetPetService {

constructor(private petsRepository: PetsRepository) { }

async execute({ id }: GetPetRequest): Promise<GetPetResponse> {
const pet = await this.petsRepository.findById(id)
return { pet }
}
}
62 changes: 62 additions & 0 deletions src/services/pets/tests/get-pet-service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { beforeEach, describe, expect, it } from "vitest";
import { GetPetService } from "../get-pet-service";
import { PetsRepository } from "@/repositories/pets/prisma-pets-repository";
import { InMemoryPetsRepository } from "@/repositories/pets/in-memory-pets-repository";
import { AddressResponse, getAddressByCep } from "@/services/cep/cep-api-service";

let sut: GetPetService
let petsRepository: PetsRepository

describe('Get pet by id', () => {

beforeEach(() => {
petsRepository = new InMemoryPetsRepository()
sut = new GetPetService(petsRepository)
})

it('should get pet by id', async () => {
const address = await getAddressByCep('08560200') as AddressResponse

const createdPet = await petsRepository.create({
age: 'Small',
cep: '08560250',
description: 'A really nice cat',
energy: 'Active',
environment: 'All',
name: 'Harry',
org_id: '1',
size: 'Big',
bairro: address.bairro,
estado: address.estado,
localidade: address.localidade,
logradouro: address.logradouro,
uf: address.uf,
})

const { pet } = await sut.execute({ id: createdPet.id })
expect(pet?.age).toEqual(createdPet.age)
})

it('should not get pet by id', async () => {
const address = await getAddressByCep('08560200') as AddressResponse

await petsRepository.create({
age: 'Small',
cep: '08560250',
description: 'A really nice cat',
energy: 'Active',
environment: 'All',
name: 'Harry',
org_id: '1',
size: 'Big',
bairro: address.bairro,
estado: address.estado,
localidade: address.localidade,
logradouro: address.logradouro,
uf: address.uf,
})

const { pet } = await sut.execute({ id: 'NON_EXISTING ID' })
expect(pet?.age).toBeUndefined()
})
})

0 comments on commit e8166d6

Please sign in to comment.