2021-08-28 03:10:00 +08:00
|
|
|
const MonkeyError = require("../handlers/error");
|
|
|
|
const { mongoDB } = require("../init/mongodb");
|
|
|
|
|
|
|
|
class QuoteRatingsDAO {
|
|
|
|
static async submit(quoteId, language, rating, update) {
|
|
|
|
if (update) {
|
2021-09-01 02:12:04 +08:00
|
|
|
await mongoDB()
|
2021-08-28 03:10:00 +08:00
|
|
|
.collection("quote-rating")
|
|
|
|
.updateOne(
|
|
|
|
{ quoteId, language },
|
|
|
|
{ $inc: { totalRating: rating } },
|
|
|
|
{ upsert: true }
|
|
|
|
);
|
|
|
|
} else {
|
2021-09-01 02:12:04 +08:00
|
|
|
await mongoDB()
|
2021-08-28 03:10:00 +08:00
|
|
|
.collection("quote-rating")
|
|
|
|
.updateOne(
|
|
|
|
{ quoteId, language },
|
|
|
|
{ $inc: { ratings: 1, totalRating: rating } },
|
|
|
|
{ upsert: true }
|
|
|
|
);
|
|
|
|
}
|
2021-09-01 02:12:04 +08:00
|
|
|
let quoteRating = await this.get(quoteId, language);
|
|
|
|
|
|
|
|
let average = parseFloat(
|
|
|
|
(
|
|
|
|
Math.round((quoteRating.totalRating / quoteRating.ratings) * 10) / 10
|
|
|
|
).toFixed(1)
|
|
|
|
);
|
|
|
|
|
|
|
|
return await mongoDB()
|
|
|
|
.collection("quote-rating")
|
|
|
|
.updateOne({ quoteId, language }, { $set: { average } });
|
2021-08-28 03:10:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static async get(quoteId, language) {
|
|
|
|
return await mongoDB()
|
|
|
|
.collection("quote-rating")
|
|
|
|
.findOne({ quoteId, language });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = QuoteRatingsDAO;
|