mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-10-27 17:27:32 +08:00
Convert ResultDAO to typescript and rename to DAL (#2838)
* Started conversion to typescript * Added types to db collection * Removed changed logic
This commit is contained in:
parent
e64405a2f3
commit
b4c191e931
4 changed files with 111 additions and 92 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import ResultDAO from "../../dao/result";
|
||||
import * as ResultDAL from "../../dao/result";
|
||||
import {
|
||||
getUser,
|
||||
checkIfPb,
|
||||
|
|
@ -44,7 +44,7 @@ export async function getResults(
|
|||
req: MonkeyTypes.Request
|
||||
): Promise<MonkeyResponse> {
|
||||
const { uid } = req.ctx.decodedToken;
|
||||
const results = await ResultDAO.getResults(uid);
|
||||
const results = await ResultDAL.getResults(uid);
|
||||
return new MonkeyResponse("Result retrieved", results);
|
||||
}
|
||||
|
||||
|
|
@ -53,7 +53,7 @@ export async function deleteAll(
|
|||
): Promise<MonkeyResponse> {
|
||||
const { uid } = req.ctx.decodedToken;
|
||||
|
||||
await ResultDAO.deleteAll(uid);
|
||||
await ResultDAL.deleteAll(uid);
|
||||
Logger.logToDb("user_results_deleted", "", uid);
|
||||
return new MonkeyResponse("All results deleted");
|
||||
}
|
||||
|
|
@ -64,7 +64,7 @@ export async function updateTags(
|
|||
const { uid } = req.ctx.decodedToken;
|
||||
const { tagIds, resultId } = req.body;
|
||||
|
||||
await ResultDAO.updateTags(uid, resultId, tagIds);
|
||||
await ResultDAL.updateTags(uid, resultId, tagIds);
|
||||
return new MonkeyResponse("Result tags updated");
|
||||
}
|
||||
|
||||
|
|
@ -150,7 +150,7 @@ export async function addResult(
|
|||
//get latest result ordered by timestamp
|
||||
let lastResultTimestamp;
|
||||
try {
|
||||
lastResultTimestamp = (await ResultDAO.getLastResult(uid)).timestamp;
|
||||
lastResultTimestamp = (await ResultDAL.getLastResult(uid)).timestamp;
|
||||
} catch (e) {
|
||||
lastResultTimestamp = null;
|
||||
}
|
||||
|
|
@ -293,7 +293,7 @@ export async function addResult(
|
|||
|
||||
if (result.mode !== "custom") delete result.customText;
|
||||
|
||||
const addedResult = await ResultDAO.addResult(uid, result);
|
||||
const addedResult = await ResultDAL.addResult(uid, result);
|
||||
|
||||
if (isPb) {
|
||||
Logger.logToDb(
|
||||
|
|
|
|||
|
|
@ -1,85 +0,0 @@
|
|||
import { ObjectId } from "mongodb";
|
||||
import MonkeyError from "../utils/error";
|
||||
import db from "../init/db";
|
||||
|
||||
import { getUser, getTags } from "./user";
|
||||
|
||||
class ResultDAO {
|
||||
static async addResult(uid, result) {
|
||||
let user;
|
||||
try {
|
||||
user = await getUser(uid);
|
||||
} catch (e) {
|
||||
user = null;
|
||||
}
|
||||
if (!user) throw new MonkeyError(404, "User not found", "add result");
|
||||
if (result.uid === undefined) result.uid = uid;
|
||||
// result.ir = true;
|
||||
let res = await db.collection("results").insertOne(result);
|
||||
return {
|
||||
insertedId: res.insertedId,
|
||||
};
|
||||
}
|
||||
|
||||
static async deleteAll(uid) {
|
||||
return await db.collection("results").deleteMany({ uid });
|
||||
}
|
||||
|
||||
static async updateTags(uid, resultid, tags) {
|
||||
const result = await db
|
||||
.collection("results")
|
||||
.findOne({ _id: new ObjectId(resultid), uid });
|
||||
if (!result) throw new MonkeyError(404, "Result not found");
|
||||
const userTags = await getTags(uid);
|
||||
const userTagIds = userTags.map((tag) => tag._id.toString());
|
||||
let validTags = true;
|
||||
tags.forEach((tagId) => {
|
||||
if (!userTagIds.includes(tagId)) validTags = false;
|
||||
});
|
||||
if (!validTags) {
|
||||
throw new MonkeyError(422, "One of the tag id's is not valid");
|
||||
}
|
||||
return await db
|
||||
.collection("results")
|
||||
.updateOne({ _id: new ObjectId(resultid), uid }, { $set: { tags } });
|
||||
}
|
||||
|
||||
static async getResult(uid, id) {
|
||||
const result = await db
|
||||
.collection("results")
|
||||
.findOne({ _id: new ObjectId(id), uid });
|
||||
if (!result) throw new MonkeyError(404, "Result not found");
|
||||
return result;
|
||||
}
|
||||
|
||||
static async getLastResult(uid) {
|
||||
const [lastResult] = await db
|
||||
.collection("results")
|
||||
.find({ uid })
|
||||
.sort({ timestamp: -1 })
|
||||
.limit(1)
|
||||
.toArray();
|
||||
if (!lastResult) throw new MonkeyError(404, "No results found");
|
||||
return lastResult;
|
||||
}
|
||||
|
||||
static async getResultByTimestamp(uid, timestamp) {
|
||||
return await db.collection("results").findOne({ uid, timestamp });
|
||||
}
|
||||
|
||||
static async getResults(uid, start, end) {
|
||||
start = start ?? 0;
|
||||
end = end ?? 1000;
|
||||
const result = await db
|
||||
.collection("results")
|
||||
.find({ uid })
|
||||
.sort({ timestamp: -1 })
|
||||
.skip(start)
|
||||
.limit(end)
|
||||
.toArray(); // this needs to be changed to later take patreon into consideration
|
||||
if (!result) throw new MonkeyError(404, "Result not found");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
export default ResultDAO;
|
||||
104
backend/dao/result.ts
Normal file
104
backend/dao/result.ts
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
import { DeleteResult, ObjectId, UpdateResult } from "mongodb";
|
||||
import MonkeyError from "../utils/error";
|
||||
import db from "../init/db";
|
||||
|
||||
import { getUser, getTags } from "./user";
|
||||
|
||||
type MonkeyTypesResult = MonkeyTypes.Result<MonkeyTypes.Mode>;
|
||||
|
||||
export async function addResult(
|
||||
uid: string,
|
||||
result: MonkeyTypesResult
|
||||
): Promise<{ insertedId: ObjectId }> {
|
||||
let user;
|
||||
try {
|
||||
user = await getUser(uid);
|
||||
} catch (e) {
|
||||
user = null;
|
||||
}
|
||||
if (!user) throw new MonkeyError(404, "User not found", "add result");
|
||||
if (result.uid === undefined) result.uid = uid;
|
||||
// result.ir = true;
|
||||
const res = await db
|
||||
.collection<MonkeyTypesResult>("results")
|
||||
.insertOne(result);
|
||||
return {
|
||||
insertedId: res.insertedId,
|
||||
};
|
||||
}
|
||||
|
||||
export async function deleteAll(uid: string): Promise<DeleteResult> {
|
||||
return await db.collection<MonkeyTypesResult>("results").deleteMany({ uid });
|
||||
}
|
||||
|
||||
export async function updateTags(
|
||||
uid: string,
|
||||
resultId: string,
|
||||
tags: string[]
|
||||
): Promise<UpdateResult> {
|
||||
const result = await db
|
||||
.collection<MonkeyTypesResult>("results")
|
||||
.findOne({ _id: new ObjectId(resultId), uid });
|
||||
if (!result) throw new MonkeyError(404, "Result not found");
|
||||
const userTags = await getTags(uid);
|
||||
const userTagIds = userTags.map((tag) => tag._id.toString());
|
||||
let validTags = true;
|
||||
tags.forEach((tagId) => {
|
||||
if (!userTagIds.includes(tagId)) validTags = false;
|
||||
});
|
||||
if (!validTags) {
|
||||
throw new MonkeyError(422, "One of the tag id's is not valid");
|
||||
}
|
||||
return await db
|
||||
.collection<MonkeyTypesResult>("results")
|
||||
.updateOne({ _id: new ObjectId(resultId), uid }, { $set: { tags } });
|
||||
}
|
||||
|
||||
export async function getResult(
|
||||
uid: string,
|
||||
id: string
|
||||
): Promise<MonkeyTypesResult> {
|
||||
const result = await db
|
||||
.collection<MonkeyTypesResult>("results")
|
||||
.findOne({ _id: new ObjectId(id), uid });
|
||||
if (!result) throw new MonkeyError(404, "Result not found");
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function getLastResult(uid: string): Promise<MonkeyTypesResult> {
|
||||
const [lastResult] = await db
|
||||
.collection<MonkeyTypesResult>("results")
|
||||
.find({ uid })
|
||||
.sort({ timestamp: -1 })
|
||||
.limit(1)
|
||||
.toArray();
|
||||
if (!lastResult) throw new MonkeyError(404, "No results found");
|
||||
return lastResult;
|
||||
}
|
||||
|
||||
export async function getResultByTimestamp(
|
||||
uid: string,
|
||||
timestamp
|
||||
): Promise<MonkeyTypesResult | null> {
|
||||
return await db
|
||||
.collection<MonkeyTypesResult>("results")
|
||||
.findOne({ uid, timestamp });
|
||||
}
|
||||
|
||||
export async function getResults(
|
||||
uid: string,
|
||||
start?: number,
|
||||
end?: number
|
||||
): Promise<MonkeyTypesResult[]> {
|
||||
start = start ?? 0;
|
||||
end = end ?? 1000;
|
||||
const results = await db
|
||||
.collection<MonkeyTypesResult>("results")
|
||||
.find({ uid })
|
||||
.sort({ timestamp: -1 })
|
||||
.skip(start)
|
||||
.limit(end)
|
||||
.toArray(); // this needs to be changed to later take patreon into consideration
|
||||
if (!results) throw new MonkeyError(404, "Result not found");
|
||||
return results;
|
||||
}
|
||||
2
backend/types/types.d.ts
vendored
2
backend/types/types.d.ts
vendored
|
|
@ -155,7 +155,7 @@ declare namespace MonkeyTypes {
|
|||
}
|
||||
|
||||
interface Result<M extends Mode> {
|
||||
_id: string;
|
||||
_id: ObjectId;
|
||||
wpm: number;
|
||||
rawWpm: number;
|
||||
charStats: number[];
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue