Create a standard output object for queries · SDC-Raspberry/atelier-api@fab06b8 · GitHub
Skip to content

Commit fab06b8

Browse files
committed
Create a standard output object for queries
1 parent 7c5805a commit fab06b8

3 files changed

Lines changed: 120 additions & 52 deletions

File tree

‎__tests__/queries.test.js‎

Lines changed: 45 additions & 28 deletions

‎server/db/queries.js‎

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,28 @@ const STATUS = {
1717
ERROR: 500,
1818
};
1919

20+
const MESSAGE = {
21+
OK: 'OK',
22+
CREATED: 'CREATED',
23+
NO_CONTENT: 'NO CONTENT',
24+
INVALID: 'INVALID',
25+
ERROR: 'ERROR',
26+
}
27+
2028
const getNextValue = async (collectionName) => {
21-
// increment value in collection
22-
const response = await Counter.findByIdAndUpdate(
23-
collectionName,
24-
{ $inc: { value: 1 } },
25-
{ new: true }
26-
).exec();
27-
// return that incremented value
28-
return response.value
29+
try {
30+
// increment value in collection
31+
const response = await Counter.findByIdAndUpdate(
32+
collectionName,
33+
{ $inc: { value: 1 } },
34+
{ new: true }
35+
).exec();
36+
// return that incremented value
37+
return response.value;
38+
} catch (error) {
39+
console.error(error);
40+
return STATUS.ERROR;
41+
}
2942
};
3043

3144
const getCurrentUnixTimestamp = () => Math.floor(new Date().getTime() / 1000);
@@ -111,10 +124,17 @@ const getReviews = async (page, count, sort, product_id) => {
111124

112125
output.results = reviewResults;
113126

114-
return output;
127+
return {
128+
status: STATUS.OK,
129+
message: MESSAGE.OK,
130+
data: output,
131+
};
115132
} catch (error) {
116133
console.error(error);
117-
return STATUS.ERROR;
134+
return {
135+
status: STATUS.ERROR,
136+
message: MESSAGE.ERROR,
137+
}
118138
}
119139
};
120140

@@ -211,10 +231,17 @@ const getReviewsMeta = async (product_id) => {
211231
};
212232
}
213233

214-
return output;
234+
return {
235+
status: STATUS.OK,
236+
message: MESSAGE.OK,
237+
data: output,
238+
};
215239
} catch (error) {
216240
console.error(error);
217-
return STATUS.ERROR;
241+
return {
242+
status: STATUS.ERROR,
243+
message: MESSAGE.ERROR,
244+
}
218245
}
219246
};
220247

@@ -272,10 +299,16 @@ const postReview = async (reqBody) => {
272299
await Review.create(newReview);
273300
await ReviewPhoto.insertMany(newReviewPhotos);
274301
await CharacteristicReview.insertMany(newCharacteristicReviews);
275-
return STATUS.OK;
302+
return {
303+
status: STATUS.CREATED,
304+
message: MESSAGE.CREATED,
305+
};
276306
} catch (error) {
277307
console.error(error);
278-
return STATUS.ERROR;
308+
return {
309+
status: STATUS.ERROR,
310+
message: MESSAGE.ERROR,
311+
};
279312
}
280313
};
281314

@@ -291,10 +324,16 @@ const putReviewHelpful = async (review_id) => {
291324
{ $inc: { helpfulness: 1 } },
292325
{ new: true }
293326
).exec();
294-
return STATUS.NO_CONTENT;
327+
return {
328+
status: STATUS.NO_CONTENT,
329+
message: MESSAGE.NO_CONTENT,
330+
};
295331
} catch (error) {
296332
console.error(error);
297-
return STATUS.ERROR;
333+
return {
334+
status: STATUS.ERROR,
335+
message: MESSAGE.ERROR,
336+
}
298337
}
299338
};
300339

@@ -310,10 +349,16 @@ const putReviewReport = async (review_id) => {
310349
{ reported: true },
311350
{ new: true }
312351
).exec();
313-
return STATUS.NO_CONTENT;
352+
return {
353+
status: STATUS.NO_CONTENT,
354+
message: MESSAGE.NO_CONTENT,
355+
};
314356
} catch (error) {
315357
console.error(error);
316-
return STATUS.ERROR;
358+
return {
359+
status: STATUS.ERROR,
360+
message: MESSAGE.ERROR,
361+
}
317362
}
318363
};
319364

‎server/server.js‎

Lines changed: 12 additions & 6 deletions

0 commit comments

Comments
 (0)