mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2024-11-10 17:04:49 +08:00
Small Dao dals (#2927)
* converted preset dao to dal * psa dao > dal * typo * config dao > dal * public stats dao > dal * fixed bad merge * removed this * removed comment
This commit is contained in:
parent
9b7b5feb71
commit
e6982f293d
8 changed files with 92 additions and 104 deletions
|
@ -1,4 +1,4 @@
|
|||
import ConfigDAO from "../../dao/config";
|
||||
import * as ConfigDAL from "../../dao/config";
|
||||
import { MonkeyResponse } from "../../utils/monkey-response";
|
||||
|
||||
export async function getConfig(
|
||||
|
@ -6,7 +6,7 @@ export async function getConfig(
|
|||
): Promise<MonkeyResponse> {
|
||||
const { uid } = req.ctx.decodedToken;
|
||||
|
||||
const data = await ConfigDAO.getConfig(uid);
|
||||
const data = await ConfigDAL.getConfig(uid);
|
||||
return new MonkeyResponse("Configuration retrieved", data);
|
||||
}
|
||||
|
||||
|
@ -16,7 +16,7 @@ export async function saveConfig(
|
|||
const { config } = req.body;
|
||||
const { uid } = req.ctx.decodedToken;
|
||||
|
||||
await ConfigDAO.saveConfig(uid, config);
|
||||
await ConfigDAL.saveConfig(uid, config);
|
||||
|
||||
return new MonkeyResponse("Config updated");
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import PresetDAO from "../../dao/preset";
|
||||
import * as PresetDAL from "../../dao/preset";
|
||||
import { MonkeyResponse } from "../../utils/monkey-response";
|
||||
|
||||
export async function getPresets(
|
||||
|
@ -6,7 +6,7 @@ export async function getPresets(
|
|||
): Promise<MonkeyResponse> {
|
||||
const { uid } = req.ctx.decodedToken;
|
||||
|
||||
const data = await PresetDAO.getPresets(uid);
|
||||
const data = await PresetDAL.getPresets(uid);
|
||||
return new MonkeyResponse("Preset retrieved", data);
|
||||
}
|
||||
|
||||
|
@ -16,7 +16,7 @@ export async function addPreset(
|
|||
const { name, config } = req.body;
|
||||
const { uid } = req.ctx.decodedToken;
|
||||
|
||||
const data = await PresetDAO.addPreset(uid, name, config);
|
||||
const data = await PresetDAL.addPreset(uid, name, config);
|
||||
|
||||
return new MonkeyResponse("Preset created", data);
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ export async function editPreset(
|
|||
const { _id, name, config } = req.body;
|
||||
const { uid } = req.ctx.decodedToken;
|
||||
|
||||
await PresetDAO.editPreset(uid, _id, name, config);
|
||||
await PresetDAL.editPreset(uid, _id, name, config);
|
||||
|
||||
return new MonkeyResponse("Preset updated");
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ export async function removePreset(
|
|||
const { presetId } = req.params;
|
||||
const { uid } = req.ctx.decodedToken;
|
||||
|
||||
await PresetDAO.removePreset(uid, presetId);
|
||||
await PresetDAL.removePreset(uid, presetId);
|
||||
|
||||
return new MonkeyResponse("Preset deleted");
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import PsaDAO from "../../dao/psa";
|
||||
import * as PsaDAL from "../../dao/psa";
|
||||
import { MonkeyResponse } from "../../utils/monkey-response";
|
||||
|
||||
export async function getPsas(
|
||||
_req: MonkeyTypes.Request
|
||||
): Promise<MonkeyResponse> {
|
||||
const data = await PsaDAO.get();
|
||||
const data = await PsaDAL.get();
|
||||
return new MonkeyResponse("PSAs retrieved", data);
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ import {
|
|||
incrementBananas,
|
||||
updateTypingStats,
|
||||
} from "../../dao/user";
|
||||
import PublicStatsDAO from "../../dao/public-stats";
|
||||
import * as PublicStatsDAL from "../../dao/public-stats";
|
||||
import * as BotDAL from "../../dao/bot";
|
||||
import { roundTo2, stdDev } from "../../utils/misc";
|
||||
import objectHash from "object-hash";
|
||||
|
@ -288,7 +288,7 @@ export async function addResult(
|
|||
}
|
||||
tt = result.testDuration + result.incompleteTestSeconds - afk;
|
||||
updateTypingStats(uid, result.restartCount, tt);
|
||||
PublicStatsDAO.updateStats(result.restartCount, tt);
|
||||
PublicStatsDAL.updateStats(result.restartCount, tt);
|
||||
|
||||
if (result.bailedOut === false) delete result.bailedOut;
|
||||
if (result.blindMode === false) delete result.blindMode;
|
||||
|
|
|
@ -2,19 +2,17 @@ import { UpdateResult } from "mongodb";
|
|||
import db from "../init/db";
|
||||
import _ from "lodash";
|
||||
|
||||
class ConfigDAO {
|
||||
static async saveConfig(uid: string, config: object): Promise<UpdateResult> {
|
||||
const configChanges = _.mapKeys(config, (_value, key) => `config.${key}`);
|
||||
return await db
|
||||
.collection<any>("configs")
|
||||
.updateOne({ uid }, { $set: configChanges }, { upsert: true });
|
||||
}
|
||||
|
||||
static async getConfig(uid: string): Promise<any> {
|
||||
const config = await db.collection<any>("configs").findOne({ uid });
|
||||
// if (!config) throw new MonkeyError(404, "Config not found");
|
||||
return config;
|
||||
}
|
||||
export async function saveConfig(
|
||||
uid: string,
|
||||
config: object
|
||||
): Promise<UpdateResult> {
|
||||
const configChanges = _.mapKeys(config, (_value, key) => `config.${key}`);
|
||||
return await db
|
||||
.collection<any>("configs")
|
||||
.updateOne({ uid }, { $set: configChanges }, { upsert: true });
|
||||
}
|
||||
|
||||
export default ConfigDAO;
|
||||
export async function getConfig(uid: string): Promise<any> {
|
||||
const config = await db.collection<any>("configs").findOne({ uid });
|
||||
return config;
|
||||
}
|
||||
|
|
|
@ -16,57 +16,56 @@ interface PresetCreationResult {
|
|||
presetId: string;
|
||||
}
|
||||
|
||||
class PresetDAO {
|
||||
// TODO: Add typings for presets/configs, must look into shared type declarations.
|
||||
static async getPresets(uid: string): Promise<any[]> {
|
||||
const presets = await db
|
||||
.collection(COLLECTION_NAME)
|
||||
.find({ uid })
|
||||
.sort({ timestamp: -1 })
|
||||
.toArray(); // this needs to be changed to later take patreon into consideration
|
||||
return presets;
|
||||
}
|
||||
|
||||
static async addPreset(
|
||||
uid: string,
|
||||
name: string,
|
||||
config: any
|
||||
): Promise<PresetCreationResult> {
|
||||
const presets = await this.getPresets(uid);
|
||||
if (presets.length >= MAX_PRESETS) {
|
||||
throw new MonkeyError(409, "Too many presets");
|
||||
}
|
||||
|
||||
const preset = await db
|
||||
.collection(COLLECTION_NAME)
|
||||
.insertOne({ uid, name, config } as any);
|
||||
return {
|
||||
presetId: preset.insertedId.toHexString(),
|
||||
};
|
||||
}
|
||||
|
||||
static async editPreset(
|
||||
uid: string,
|
||||
presetId: string,
|
||||
name: string,
|
||||
config: any
|
||||
): Promise<void> {
|
||||
const presetUpdates =
|
||||
config && Object.keys(config).length > 0 ? { name, config } : { name };
|
||||
await db
|
||||
.collection(COLLECTION_NAME)
|
||||
.updateOne(getPresetKeyFilter(uid, presetId), { $set: presetUpdates });
|
||||
}
|
||||
|
||||
static async removePreset(uid: string, presetId: string): Promise<void> {
|
||||
const deleteResult = await db
|
||||
.collection(COLLECTION_NAME)
|
||||
.deleteOne(getPresetKeyFilter(uid, presetId));
|
||||
|
||||
if (deleteResult.deletedCount === 0) {
|
||||
throw new MonkeyError(404, "Preset not found");
|
||||
}
|
||||
}
|
||||
// TODO: Add typings for presets/configs, must look into shared type declarations.
|
||||
export async function getPresets(uid: string): Promise<any[]> {
|
||||
const presets = await db
|
||||
.collection(COLLECTION_NAME)
|
||||
.find({ uid })
|
||||
.sort({ timestamp: -1 })
|
||||
.toArray(); // this needs to be changed to later take patreon into consideration
|
||||
return presets;
|
||||
}
|
||||
|
||||
export default PresetDAO;
|
||||
export async function addPreset(
|
||||
uid: string,
|
||||
name: string,
|
||||
config: any
|
||||
): Promise<PresetCreationResult> {
|
||||
const presets = await getPresets(uid);
|
||||
if (presets.length >= MAX_PRESETS) {
|
||||
throw new MonkeyError(409, "Too many presets");
|
||||
}
|
||||
|
||||
const preset = await db
|
||||
.collection(COLLECTION_NAME)
|
||||
.insertOne({ uid, name, config } as any);
|
||||
return {
|
||||
presetId: preset.insertedId.toHexString(),
|
||||
};
|
||||
}
|
||||
|
||||
export async function editPreset(
|
||||
uid: string,
|
||||
presetId: string,
|
||||
name: string,
|
||||
config: any
|
||||
): Promise<void> {
|
||||
const presetUpdates =
|
||||
config && Object.keys(config).length > 0 ? { name, config } : { name };
|
||||
await db
|
||||
.collection(COLLECTION_NAME)
|
||||
.updateOne(getPresetKeyFilter(uid, presetId), { $set: presetUpdates });
|
||||
}
|
||||
|
||||
export async function removePreset(
|
||||
uid: string,
|
||||
presetId: string
|
||||
): Promise<void> {
|
||||
const deleteResult = await db
|
||||
.collection(COLLECTION_NAME)
|
||||
.deleteOne(getPresetKeyFilter(uid, presetId));
|
||||
|
||||
if (deleteResult.deletedCount === 0) {
|
||||
throw new MonkeyError(404, "Preset not found");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,5 @@
|
|||
import db from "../init/db";
|
||||
|
||||
class PsaDAO {
|
||||
static async get(): Promise<MonkeyTypes.PSA[]> {
|
||||
return await db.collection<MonkeyTypes.PSA>("psa").find().toArray();
|
||||
}
|
||||
export async function get(): Promise<MonkeyTypes.PSA[]> {
|
||||
return await db.collection<MonkeyTypes.PSA>("psa").find().toArray();
|
||||
}
|
||||
|
||||
export default PsaDAO;
|
||||
|
|
|
@ -1,25 +1,20 @@
|
|||
import db from "../init/db";
|
||||
import { roundTo2 } from "../utils/misc";
|
||||
|
||||
class PublicStatsDAO {
|
||||
//needs to be rewritten, this is public stats not user stats
|
||||
static async updateStats(
|
||||
restartCount: number,
|
||||
time: number
|
||||
): Promise<boolean> {
|
||||
await db.collection<MonkeyTypes.PublicStats>("public").updateOne(
|
||||
{ type: "stats" },
|
||||
{
|
||||
$inc: {
|
||||
testsCompleted: 1,
|
||||
testsStarted: restartCount + 1,
|
||||
timeTyping: roundTo2(time),
|
||||
},
|
||||
export async function updateStats(
|
||||
restartCount: number,
|
||||
time: number
|
||||
): Promise<boolean> {
|
||||
await db.collection<MonkeyTypes.PublicStats>("public").updateOne(
|
||||
{ type: "stats" },
|
||||
{
|
||||
$inc: {
|
||||
testsCompleted: 1,
|
||||
testsStarted: restartCount + 1,
|
||||
timeTyping: roundTo2(time),
|
||||
},
|
||||
{ upsert: true }
|
||||
);
|
||||
return true;
|
||||
}
|
||||
},
|
||||
{ upsert: true }
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
export default PublicStatsDAO;
|
||||
|
|
Loading…
Reference in a new issue