feat: Implement (most) API endpoints

This commit is contained in:
Alexander Polynomdivision 2018-09-29 19:34:22 +02:00
parent 9ea5078c5c
commit e6e7505383
6 changed files with 50 additions and 36 deletions

View File

@ -8,6 +8,7 @@ classRouter.use(bodyparser.json());
classRouter.use(authRoute); classRouter.use(authRoute);
classRouter.get("/:id/topTen", async (req: Request, res: Response) => { classRouter.get("/:id/topTen", async (req: Request, res: Response) => {
// TODO: Deprecate this?
if (!req.params) { if (!req.params) {
res.send({ res.send({
error: "400", error: "400",
@ -18,7 +19,7 @@ classRouter.get("/:id/topTen", async (req: Request, res: Response) => {
return; return;
} }
console.log("Stub(get): /class/:id/topTen"); console.log("STUB(get): /class/:id/topTen");
let users: any[] = []; let users: any[] = [];
let nr = 10; let nr = 10;

View File

@ -5,7 +5,7 @@ import { authRoute } from "../security/token";
import { userFromSession } from "../utils/user"; import { userFromSession } from "../utils/user";
import { IUser } from "../models/user"; import { IUser, IUserDBModel } from "../models/user";
import { LRequest } from "../types/express"; import { LRequest } from "../types/express";
import { Db } from "mongodb"; import { Db } from "mongodb";
@ -62,24 +62,31 @@ export enum VocabType {
}; };
// Gets the user's review queue // Gets the user's review queue
userRouter.get("/queue", async (req, res) => { userRouter.get("/queue", async (req: LRequest, res) => {
console.log("STUB: /user/queue"); // TODO: if (user)
const { token, db } = req;
const user = await userFromSession(token, db);
// Fetch all vocab ids from the vocabulary collection
const vocabRaw = await db.collection("vocabulary").find({
id: { $in: user.queue },
}, {
// TODO: Make this configurable?
limit: 20,
}).toArray();
// Remove unneeded data
const vocab = vocabRaw.map(el => {
let tmp = Object.assign({}, el);
delete tmp._id;
return tmp;
});
// TODO: Stub
res.send({ res.send({
error: "0", error: "0",
data: { data: {
queue: [{ queue: vocab,
german: ["Wein"],
hint: "Worte auf '-um' sind meistens NeutrUM",
type: VocabType.NOMEN,
latin: {
grundform: "Vinum",
genitiv: "Vini",
genus: "Neutrum"
},
id: 0
}]
}, },
}); });
}); });

View File

@ -50,17 +50,16 @@ const assert = require('assert');
app.use("/api/class", ClassRouter); app.use("/api/class", ClassRouter);
app.use("/api/user", UserRouter); app.use("/api/user", UserRouter);
app.get("/api/levels", async (req, res) => { app.get("/api/levels", async (req, res) => {
const levels = [{ // TODO: if (levels)
name: "Der Bauer auf dem Feld", const levels = (await db.collection("levels").find()
desc: "So fängt alles an: Du bist ein einfacher Bauer und musst dich die Karriereleiter mit deinen freshen Latein-Skills hinaufarbeiten", .toArray())
level: 1, .map((el) => {
done: true, let tmp = Object.assign({}, el);
}, { delete tmp.queue;
name: "???", delete tmp._id;
desc: "Warum schreibe ich überhaupt was?dsd dddddddddddddddddddddd",
level: 2, return tmp;
done: false, });
}];
res.send({ res.send({
error: "0", error: "0",

View File

@ -1,4 +1,4 @@
export interface IUser { export interface IUserDBModel {
username: string; username: string;
uid: string; uid: string;
showWelcome: boolean; showWelcome: boolean;
@ -13,4 +13,16 @@ export interface IUser {
}; };
lastLevel: number; lastLevel: number;
queue: number[];
};
export interface IUser {
username: string;
uid: string;
showWelcome: boolean;
score: number;
classId: string;
sessionToken: string;
}; };

View File

@ -31,14 +31,9 @@ export async function performLogin(username: string, password: string, db: Db):
username: user.username, username: user.username,
uid: user.uid, uid: user.uid,
showWelcome: user.showWelcome, showWelcome: user.showWelcome,
//@ts-ignore score: user.score,
classId: user.classId, classId: user.classId,
lastReview: user.lastReview,
lastLevel: user.lastLevel,
// TODO: Implement
score: 4,
sessionToken, sessionToken,
}; };
} else { } else {

View File

@ -1,8 +1,8 @@
import { Db } from "mongodb"; import { Db } from "mongodb";
import { IUser } from "../models/user"; import { IUserDBModel } from "../models/user";
export async function userFromSession(token: string, db: Db): Promise<IUser> { export async function userFromSession(token: string, db: Db): Promise<IUserDBModel> {
// Get the username // Get the username
const session = await db.collection("sessions").findOne({ token, }); const session = await db.collection("sessions").findOne({ token, });
if (session) { if (session) {