Skip to content

Commit

Permalink
Merge pull request #11 from lbratkovskaya/task/implement-backend
Browse files Browse the repository at this point in the history
fix: updte rates on the very first review
  • Loading branch information
Tonia-SE authored Mar 16, 2021
2 parents f2ef1ca + ec35494 commit dfdacbc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
1 change: 0 additions & 1 deletion src/handlers/rateCounter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ function rateCounter(sightId: string) {
}

export default rateCounter;

25 changes: 14 additions & 11 deletions src/routes/reviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ reviewsRouter.post(
user: user,
sightId: sightId,
rate: +rate,
}).then(() => {
rateCounter(sightId);
});
} else {
Reviews.findOneAndUpdate(
Expand All @@ -45,10 +47,10 @@ reviewsRouter.post(
rate: +rate,
},
{},
() => {
rateCounter(sightId);
},
);
() => {},
).then(() => {
rateCounter(sightId);
});
}
res.status(200);
res.json({ result: 'Rate was updated' });
Expand Down Expand Up @@ -76,25 +78,26 @@ reviewsRouter.post(
};
const reviews: Array<IReview> = await Reviews.find(filter);
if (!reviews.length) {
Reviews.insertMany({
await Reviews.insertMany({
user: user,
sightId: sightId,
rate: +rate,
review: review,
}).then(() => {
rateCounter(sightId);
});
} else {
console.log('updating');
Reviews.findOneAndUpdate(
await Reviews.findOneAndUpdate(
filter,
{
rate: +rate,
review: review,
},
{},
() => {
rateCounter(sightId);
},
);
() => {},
).then(() => {
rateCounter(sightId);
});
}
res.status(200);
res.json({ result: 'Review was updated' });
Expand Down
2 changes: 2 additions & 0 deletions src/routes/sights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ sightsRouter.get(
let filter = {};
if (req.query.countryId) {
filter = { countryId: req.query.countryId };
} else if (req.query.sightId) {
filter = { _id: req.query.sightId };
}
const sights = await Sights.find(filter);
res.json(sights);
Expand Down

0 comments on commit dfdacbc

Please sign in to comment.