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

View File

@ -5,7 +5,7 @@ import { authRoute } from "../security/token";
import { userFromSession } from "../utils/user";
import { IUser } from "../models/user";
import { IUser, IUserDBModel } from "../models/user";
import { LRequest } from "../types/express";
import { Db } from "mongodb";
@ -62,24 +62,31 @@ export enum VocabType {
};
// Gets the user's review queue
userRouter.get("/queue", async (req, res) => {
console.log("STUB: /user/queue");
userRouter.get("/queue", async (req: LRequest, res) => {
// 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({
error: "0",
data: {
queue: [{
german: ["Wein"],
hint: "Worte auf '-um' sind meistens NeutrUM",
type: VocabType.NOMEN,
latin: {
grundform: "Vinum",
genitiv: "Vini",
genus: "Neutrum"
},
id: 0
}]
queue: vocab,
},
});
});

View File

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

View File

@ -1,4 +1,4 @@
export interface IUser {
export interface IUserDBModel {
username: string;
uid: string;
showWelcome: boolean;
@ -13,4 +13,16 @@ export interface IUser {
};
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,
uid: user.uid,
showWelcome: user.showWelcome,
//@ts-ignore
score: user.score,
classId: user.classId,
lastReview: user.lastReview,
lastLevel: user.lastLevel,
// TODO: Implement
score: 4,
sessionToken,
};
} else {

View File

@ -1,8 +1,8 @@
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
const session = await db.collection("sessions").findOne({ token, });
if (session) {