Skip to content

Commit

Permalink
Add searchPetsitter api and cor without spec test, modified api and c…
Browse files Browse the repository at this point in the history
…or logic of getAllPetsitters and tests b00tc4mp#99
  • Loading branch information
TatiGV committed Aug 23, 2024
1 parent f21da85 commit fcfd303
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { userId } = req.params

try {
logic.getAllPetsitters(userId)
logic.getAllPetsitters()
.then(petsitters => res.json(petsitters))
.catch(error => next(error))
} catch (error) {
Expand Down
4 changes: 3 additions & 1 deletion staff/tatiana-garcia/project/api/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import registerPetsitterUserHandler from './registerPetsitterUserHandler.js'
import getUserHandler from './getUserHandler.js'
import updateUserHandler from './updateUserHandler.js'
import getAllPetsittersHandler from './getAllPetsittersHandler.js'
import searchPetsittersHandler from './searchPetsittersHandler.js'

export {
registerUserHandler,
authenticateUserHandler,
registerPetsitterUserHandler,
getUserHandler,
updateUserHandler,
getAllPetsittersHandler
getAllPetsittersHandler,
searchPetsittersHandler
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { city, pet } = req.query;

try {
logic.searchPetsitters(city, pet)
.then(petsitters => res.json(petsitters))
.catch(error => next(error))
} catch (error) {
next(error)
}

}
7 changes: 5 additions & 2 deletions staff/tatiana-garcia/project/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
getUserHandler,
updateUserHandler,
registerPetsitterUserHandler,
getAllPetsittersHandler
getAllPetsittersHandler,
searchPetsittersHandler

} from './handlers/index.js'

Expand All @@ -31,7 +32,9 @@ mongoose.connect(process.env.MONGODB_URI)

api.get('/users/:targetUserId', jwtVerifier, getUserHandler)

api.get('/petsitters', jwtVerifier, getAllPetsittersHandler)
api.get('/petsitters', getAllPetsittersHandler)

api.get('/petsitters/search', searchPetsittersHandler)

api.patch('/users', jwtVerifier, updateUserHandler)

Expand Down
2 changes: 1 addition & 1 deletion staff/tatiana-garcia/project/api/test/authenticate-user.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
curl -X POST -v http://localhost:8080/users/auth -d '{"email": "abt@garcia.com", "password":"123123123"}' -H "Content-Type: application/json"
curl -X POST -v http://localhost:8080/users/auth -d '{"email": "vet@point.com", "password":"123123123"}' -H "Content-Type: application/json"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
curl -X GET -v http://localhost:8080/petsitters -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmM3YWIyZjIzYjk5ZDYwMTRhNGU2MTMiLCJyb2xlIjoicGV0c2l0dGVyIiwiaWF0IjoxNzI0MzYyNzc1fQ.4sFqqyP05aa5XBAkiYMUn_U2U0uBiIKK949ep0xUrJM"
curl -X GET -v http://localhost:8080/petsitters
1 change: 1 addition & 0 deletions staff/tatiana-garcia/project/api/test/search-petsitters.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -X GET -v "http://localhost:8080/petsitters/search?city=barcelona&pet=reptiles"
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export default function RegisterPetsitterUser({ onRegisterPetsitterUser, onLogin
name='pets-input'
onChange={handlePetChange}
/>
<label htmlFor='rats-input'>Rátas</label><br />
<label htmlFor='rats-input'>Ratas</label><br />

<input
type='checkbox'
Expand Down
37 changes: 15 additions & 22 deletions staff/tatiana-garcia/project/cor/logic/getAllPetsitters.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
import { User } from '../data/models.js'

import { validate, errors } from '../../com/index.js'
import { errors } from '../../com/index.js'

const { SystemError, NotFoundError } = errors
const { SystemError } = errors

export default userId => {
validate.string(userId, 'userId')
export default () => {

return User.findById(userId).lean()
return User.find({ role: 'petsitter' }, { __v: 0 }).sort({ name: 1 }).lean()
.catch(error => { throw new SystemError(error.message) })
.then(user => {
if (!user) throw new NotFoundError('user not found')

return User.find({ role: 'petsitter' }, { __v: 0 }).sort({ name: 1 }).lean()
.catch(error => { throw new SystemError(error.message) })
.then(petsitters => {
return petsitters.map(petsitter => ({
id: petsitter._id.toString(),
image: petsitter.image,
name: petsitter.name,
city: petsitter.city,
description: petsitter.description,
email: petsitter.email,
phoneNumber: petsitter.phoneNumber,
pets: petsitter.pets
}))
})
.then(petsitters => {
return petsitters.map(petsitter => ({
id: petsitter._id.toString(),
image: petsitter.image,
name: petsitter.name,
city: petsitter.city,
description: petsitter.description,
email: petsitter.email,
phoneNumber: petsitter.phoneNumber,
pets: petsitter.pets
}))
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import getAllPetsitters from './getAllPetsitters.js'
import mongoose from 'mongoose'

mongoose.connect(process.env.MONGODB_URI)
.then(() => getAllPetsitters('66c7ab2f23b99d6014a4e613'))
.then(user => console.log(user))
.then(() => getAllPetsitters())
.then(petsitters => console.log(petsitters))
.catch(error => console.error(error))
.finally(() => mongoose.disconnect())
4 changes: 3 additions & 1 deletion staff/tatiana-garcia/project/cor/logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import registerPetsitterUser from './registerPetsitterUser.js'
import getUser from './getUser.js'
import updateUser from './updateUser.js'
import getAllPetsitters from './getAllPetsitters.js'
import searchPetsitters from './searchPetsitters.js'

const logic = {
registerUser,
registerPetsitterUser,
authenticateUser,
getUser,
updateUser,
getAllPetsitters
getAllPetsitters,
searchPetsitters
}

export default logic
31 changes: 31 additions & 0 deletions staff/tatiana-garcia/project/cor/logic/searchPetsitters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { User } from '../data/models.js'

import { validate, errors } from '../../com/index.js'

const { SystemError } = errors

export default (city, pet = null) => {
validate.city(city, 'city')

const query = { role: 'petsitter', city: city }

if (pet) {
query.pets = { $in: [pet] }
}

return User.find(query, { __v: 0 }).sort({ name: 1 }).lean()
.catch(error => { throw new SystemError(error.message) })
.then(petsitters => {
return petsitters.map(petsitter => ({
id: petsitter._id.toString(),
image: petsitter.image,
name: petsitter.name,
city: petsitter.city,
description: petsitter.description,
email: petsitter.email,
phoneNumber: petsitter.phoneNumber,
pets: petsitter.pets
}))
})

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'dotenv/config'
import searchPetsitters from './searchPetsitters.js'
import mongoose from 'mongoose'

mongoose.connect(process.env.MONGODB_URI)
.then(() => searchPetsitters('madrid'))
.then(petsitter => console.log(petsitter))
.catch(error => console.error(error))
.finally(() => mongoose.disconnect())

0 comments on commit fcfd303

Please sign in to comment.