Skip to content

Commit

Permalink
crate add kindergartenSearch, valedate the querys and use getKinderga…
Browse files Browse the repository at this point in the history
…rtenSearch to get the data

Relates #21
  • Loading branch information
Osama-you committed Feb 9, 2021
1 parent 6e4e47f commit 65aae8f
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion server/controllers/kindergarten/kindergartenSearch.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,48 @@
const kindergartenSearch = () => {};
const { getKindergartenSearch } = require('../../database/queries');
const { kindergartenSearchSchema } = require('../../utils/validation');
const { boomify } = require('../../utils');

const kindergartenSearch = async (req, res, next) => {
try {
const {
q,
minPrice,
maxPrice,
locationId,
} = await kindergartenSearchSchema.validate(req.query, {
abortEarly: false,
});

if (!q && (!minPrice || !maxPrice) && !locationId) {
next(
boomify(
400,
'Validation Error',
'At least one of these (q, minPrice & maxPrice,locationId) values is required'
)
);
}

const { rows: data } = await getKindergartenSearch({
q,
minPrice,
maxPrice,
locationId,
});

const StatusCode = 200;

res.status(StatusCode).json({
StatusCode,
data,
});
} catch (error) {
next(
error.name === 'ValidationError'
? boomify(400, 'Validation Error', error.errors)
: error
);
}
};

module.exports = kindergartenSearch;

0 comments on commit 65aae8f

Please sign in to comment.