forked from b00tc4mp/isdi-bootcamp-202405
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add searchPetsitter api and cor without spec test, modified api and c…
…or logic of getAllPetsitters and tests b00tc4mp#99
- Loading branch information
Showing
13 changed files
with
87 additions
and
34 deletions.
There are no files selected for viewing
4 changes: 1 addition & 3 deletions
4
staff/tatiana-garcia/project/api/handlers/getAllPetsittersHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
staff/tatiana-garcia/project/api/handlers/searchPetsittersHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 15 additions & 22 deletions
37
staff/tatiana-garcia/project/cor/logic/getAllPetsitters.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
})) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
staff/tatiana-garcia/project/cor/logic/searchPetsitters.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
})) | ||
}) | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
staff/tatiana-garcia/project/cor/logic/searchPetsitters.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |