mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2024-09-20 15:26:15 +08:00
Merge branch 'master' of https://github.com/Miodec/monkeytype
This commit is contained in:
commit
ec8ec49b71
|
@ -3,7 +3,7 @@ import { v4 as uuidv4 } from "uuid";
|
|||
import { getUser, updateQuoteRatings } from "../../dao/user";
|
||||
import * as ReportDAL from "../../dao/report";
|
||||
import * as NewQuotesDAL from "../../dao/new-quotes";
|
||||
import QuoteRatingsDAO from "../../dao/quote-ratings";
|
||||
import * as QuoteRatingsDAL from "../../dao/quote-ratings";
|
||||
import MonkeyError from "../../utils/error";
|
||||
import { verify } from "../../utils/captcha";
|
||||
import Logger from "../../utils/logger";
|
||||
|
@ -79,7 +79,7 @@ export async function getRating(
|
|||
): Promise<MonkeyResponse> {
|
||||
const { quoteId, language } = req.query;
|
||||
|
||||
const data = await QuoteRatingsDAO.get(
|
||||
const data = await QuoteRatingsDAL.get(
|
||||
parseInt(quoteId as string, 10),
|
||||
language as string
|
||||
);
|
||||
|
@ -104,7 +104,7 @@ export async function submitRating(
|
|||
const newRating = normalizedRating - currentRating;
|
||||
const shouldUpdateRating = currentRating !== 0;
|
||||
|
||||
await QuoteRatingsDAO.submit(
|
||||
await QuoteRatingsDAL.submit(
|
||||
quoteId,
|
||||
language,
|
||||
newRating,
|
||||
|
|
|
@ -1,50 +1,46 @@
|
|||
import db from "../init/db";
|
||||
|
||||
class QuoteRatingsDAO {
|
||||
static async submit(
|
||||
quoteId: number,
|
||||
language: string,
|
||||
rating: number,
|
||||
update: boolean
|
||||
): Promise<void> {
|
||||
if (update) {
|
||||
await db
|
||||
.collection<MonkeyTypes.QuoteRating>("quote-rating")
|
||||
.updateOne(
|
||||
{ quoteId, language },
|
||||
{ $inc: { totalRating: rating } },
|
||||
{ upsert: true }
|
||||
);
|
||||
} else {
|
||||
await db
|
||||
.collection<MonkeyTypes.QuoteRating>("quote-rating")
|
||||
.updateOne(
|
||||
{ quoteId, language },
|
||||
{ $inc: { ratings: 1, totalRating: rating } },
|
||||
{ upsert: true }
|
||||
);
|
||||
}
|
||||
|
||||
const quoteRating = await this.get(quoteId, language);
|
||||
const average = parseFloat(
|
||||
(
|
||||
Math.round((quoteRating!.totalRating / quoteRating!.ratings) * 10) / 10
|
||||
).toFixed(1)
|
||||
);
|
||||
|
||||
export async function submit(
|
||||
quoteId: number,
|
||||
language: string,
|
||||
rating: number,
|
||||
update: boolean
|
||||
): Promise<void> {
|
||||
if (update) {
|
||||
await db
|
||||
.collection<MonkeyTypes.QuoteRating>("quote-rating")
|
||||
.updateOne({ quoteId, language }, { $set: { average } });
|
||||
.updateOne(
|
||||
{ quoteId, language },
|
||||
{ $inc: { totalRating: rating } },
|
||||
{ upsert: true }
|
||||
);
|
||||
} else {
|
||||
await db
|
||||
.collection<MonkeyTypes.QuoteRating>("quote-rating")
|
||||
.updateOne(
|
||||
{ quoteId, language },
|
||||
{ $inc: { ratings: 1, totalRating: rating } },
|
||||
{ upsert: true }
|
||||
);
|
||||
}
|
||||
|
||||
static async get(
|
||||
quoteId: number,
|
||||
language: string
|
||||
): Promise<MonkeyTypes.QuoteRating | null> {
|
||||
return await db
|
||||
.collection<MonkeyTypes.QuoteRating>("quote-rating")
|
||||
.findOne({ quoteId, language });
|
||||
}
|
||||
const quoteRating = await get(quoteId, language);
|
||||
const average = parseFloat(
|
||||
(
|
||||
Math.round((quoteRating!.totalRating / quoteRating!.ratings) * 10) / 10
|
||||
).toFixed(1)
|
||||
);
|
||||
|
||||
await db
|
||||
.collection<MonkeyTypes.QuoteRating>("quote-rating")
|
||||
.updateOne({ quoteId, language }, { $set: { average } });
|
||||
}
|
||||
|
||||
export default QuoteRatingsDAO;
|
||||
export async function get(
|
||||
quoteId: number,
|
||||
language: string
|
||||
): Promise<MonkeyTypes.QuoteRating | null> {
|
||||
return await db
|
||||
.collection<MonkeyTypes.QuoteRating>("quote-rating")
|
||||
.findOne({ quoteId, language });
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue