feat: Implement updating the done levels
This commit is contained in:
@@ -21,10 +21,15 @@ levelRouter.get("/:id/vocab", async (req: LRequest, res: Response) => {
|
||||
}
|
||||
|
||||
const levelId = parseInt(req.params.id);
|
||||
// TODO: Handle this
|
||||
// if (levelId === NaN) {
|
||||
// // Something
|
||||
// }
|
||||
if (levelId === NaN) {
|
||||
res.send({
|
||||
error: "400",
|
||||
data: {
|
||||
msg: "Invalid level id",
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Find the level
|
||||
// TODO: if (level)
|
||||
@@ -33,12 +38,9 @@ levelRouter.get("/:id/vocab", async (req: LRequest, res: Response) => {
|
||||
// TODO: This aint safe, boi
|
||||
level: levelId,
|
||||
});
|
||||
console.log(level);
|
||||
|
||||
// Fetch all the vocabulary
|
||||
const vocab = await db.collection("vocabulary").find({ id: { $in: level.vocab } }).toArray();
|
||||
console.log(vocab);
|
||||
|
||||
res.send({
|
||||
error: "0",
|
||||
data: {
|
||||
|
||||
@@ -155,12 +155,54 @@ userRouter.get("/nextLevel", async (req: LRequest, res) => {
|
||||
});
|
||||
|
||||
// Mark a level as done
|
||||
userRouter.post("/level/:id", async (req, res) => {
|
||||
console.log("STUB(post): /user/level/:id");
|
||||
userRouter.post("/level/:id", async (req: LRequest, res) => {
|
||||
// Is everything specified?
|
||||
if (!req.params || !req.params.id) {
|
||||
res.send({
|
||||
error: "400",
|
||||
data: {
|
||||
msg: "No level ID specified",
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const id = parseInt(req.params.id);
|
||||
if (id === NaN) {
|
||||
res.send({
|
||||
error: "400",
|
||||
data: {
|
||||
msg: "Invalid level ID",
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: if (user)
|
||||
const { token, db } = req;
|
||||
const user = await userFromSession(token, db);
|
||||
|
||||
if (id in user.levels) {
|
||||
// Nothing
|
||||
} else {
|
||||
// The level is new to the user
|
||||
// Is the new level higher than the "highest" already completed level?
|
||||
let update = {
|
||||
levels: user.levels.concat(id),
|
||||
};
|
||||
if (id > Math.max(...user.levels)) {
|
||||
// Also update the lastLevel attribute
|
||||
Object.assign(update, { lastLevel: id });
|
||||
}
|
||||
await db.collection("users").updateOne({
|
||||
username: user.username,
|
||||
}, {
|
||||
$set: update,
|
||||
});
|
||||
}
|
||||
|
||||
// TODO: Stub
|
||||
res.send({
|
||||
error: "0",
|
||||
error: "200",
|
||||
data: {},
|
||||
});
|
||||
});
|
||||
@@ -206,4 +248,35 @@ userRouter.get("/dashboard", async (req: LRequest, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
userRouter.post("/showWelcome", async (req: LRequest, res) => {
|
||||
const { db, token } = req;
|
||||
|
||||
// Are all arguments specified?
|
||||
if (!req.body || !("state" in req.body)) {
|
||||
res.send({
|
||||
error: "400",
|
||||
data: {
|
||||
msg: "State not specified",
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Find the user
|
||||
// TODO: if (user)
|
||||
const user = await userFromSession(token, db);
|
||||
|
||||
await db.collection("users").updateOne({
|
||||
username: user.username,
|
||||
}, {
|
||||
$set: {
|
||||
showWelcome: req.body.state,
|
||||
},
|
||||
});
|
||||
res.send({
|
||||
error: "200",
|
||||
data: {},
|
||||
});
|
||||
});
|
||||
|
||||
export default userRouter;
|
||||
|
||||
@@ -5,6 +5,8 @@ import * as bodyparser from "body-parser";
|
||||
|
||||
import { isAuthenticated, performLogin } from "./security/auth";
|
||||
|
||||
import { LRequest } from "./types/express";
|
||||
|
||||
import UserRouter from "./api/user";
|
||||
import ClassRouter from "./api/class";
|
||||
import LevelRouter from "./api/level";
|
||||
@@ -39,10 +41,9 @@ const assert = require('assert');
|
||||
app.use(bodyparser.json());
|
||||
app.options("*", cors());
|
||||
|
||||
app.use((req, res, next) => {
|
||||
app.use((req: LRequest, res, next) => {
|
||||
// Every route should have access to the database so that
|
||||
// we can easily make calls to it
|
||||
// @ts-ignore
|
||||
req.db = db;
|
||||
next();
|
||||
});
|
||||
|
||||
@@ -12,9 +12,21 @@ export interface IUserDBModel {
|
||||
wrong: number;
|
||||
};
|
||||
|
||||
// Levels that we have done
|
||||
levels: number[];
|
||||
// The "highest" level the user has done
|
||||
lastLevel: number;
|
||||
|
||||
queue: number[];
|
||||
|
||||
// Vocabulary ID -> SM2 Metadata
|
||||
metadata: {
|
||||
[key: number]: {
|
||||
easiness: number;
|
||||
nextDueDate: number;
|
||||
consecutiveCorrectAnswers: number;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
export interface IUser {
|
||||
|
||||
Reference in New Issue
Block a user