-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpets-dao-example.js
45 lines (37 loc) · 1.18 KB
/
pets-dao-example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import _ from 'lodash';
import { parseQuery } from 'utils/parse-query';
import { serializePets, serializePet } from 'serializers/pets-serializer';
import { getObject } from './aws-operations';
const objectKey = 'pets.json';
/**
* Return a list of pets
*
* @param {object} query Query parameters
* @returns {Promise} Promise object represents a list of pets
*/
const getPets = async (query) => {
const object = await getObject(objectKey);
let rawPets = JSON.parse(object.Body.toString()).pets;
const parsedQuery = parseQuery(query);
const { species } = parsedQuery;
rawPets = species ? _.filter(rawPets, { species }) : rawPets;
const serializedPets = serializePets(rawPets, query);
return serializedPets;
};
/**
* Return a specific pet by unique ID
*
* @param {string} id Unique pet ID
* @returns {Promise} Promise object represents a specific pet
*/
const getPetById = async (id) => {
const object = await getObject(objectKey);
const rawPets = JSON.parse(object.Body.toString()).pets;
const rawPet = _.find(rawPets, { id });
if (!rawPet) {
return undefined;
}
const serializedPet = serializePet(rawPet);
return serializedPet;
};
export { getPets, getPetById };