2022-02-23 03:55:48 +08:00
|
|
|
import db from "../init/db";
|
2021-08-28 03:10:00 +08:00
|
|
|
|
|
|
|
class QuoteRatingsDAO {
|
2022-03-05 05:50:15 +08:00
|
|
|
static async submit(
|
|
|
|
quoteId: number,
|
|
|
|
language: string,
|
|
|
|
rating: number,
|
|
|
|
update: boolean
|
|
|
|
): Promise<void> {
|
2021-08-28 03:10:00 +08:00
|
|
|
if (update) {
|
2022-02-23 03:55:48 +08:00
|
|
|
await db
|
2022-03-05 05:50:15 +08:00
|
|
|
.collection<MonkeyTypes.QuoteRating>("quote-rating")
|
2021-08-28 03:10:00 +08:00
|
|
|
.updateOne(
|
|
|
|
{ quoteId, language },
|
|
|
|
{ $inc: { totalRating: rating } },
|
|
|
|
{ upsert: true }
|
|
|
|
);
|
|
|
|
} else {
|
2022-02-23 03:55:48 +08:00
|
|
|
await db
|
2022-03-05 05:50:15 +08:00
|
|
|
.collection<MonkeyTypes.QuoteRating>("quote-rating")
|
2021-08-28 03:10:00 +08:00
|
|
|
.updateOne(
|
|
|
|
{ quoteId, language },
|
|
|
|
{ $inc: { ratings: 1, totalRating: rating } },
|
|
|
|
{ upsert: true }
|
|
|
|
);
|
|
|
|
}
|
2021-09-01 02:12:04 +08:00
|
|
|
|
2022-03-05 05:50:15 +08:00
|
|
|
const quoteRating = await this.get(quoteId, language);
|
|
|
|
const average = parseFloat(
|
2021-09-01 02:12:04 +08:00
|
|
|
(
|
|
|
|
Math.round((quoteRating.totalRating / quoteRating.ratings) * 10) / 10
|
|
|
|
).toFixed(1)
|
|
|
|
);
|
|
|
|
|
2022-03-05 05:50:15 +08:00
|
|
|
await db
|
|
|
|
.collection<MonkeyTypes.QuoteRating>("quote-rating")
|
2021-09-01 02:12:04 +08:00
|
|
|
.updateOne({ quoteId, language }, { $set: { average } });
|
2021-08-28 03:10:00 +08:00
|
|
|
}
|
|
|
|
|
2022-03-05 05:50:15 +08:00
|
|
|
static async get(
|
|
|
|
quoteId: number,
|
|
|
|
language: string
|
|
|
|
): Promise<MonkeyTypes.QuoteRating> {
|
|
|
|
return await db
|
|
|
|
.collection<MonkeyTypes.QuoteRating>("quote-rating")
|
|
|
|
.findOne({ quoteId, language });
|
2021-08-28 03:10:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-23 03:55:48 +08:00
|
|
|
export default QuoteRatingsDAO;
|