refactor: Create a better API scheme

This commit is contained in:
Alexander Polynomdivision 2018-09-23 22:17:35 +02:00
parent a7dc7464a1
commit 2c4011f631
6 changed files with 226 additions and 80 deletions

42
backend/src/api/class.ts Normal file
View File

@ -0,0 +1,42 @@
import { Router, Request, Response } from "express";
import * as bodyparser from "body-parser";
import { authRoute } from "../security/token";
const classRouter = Router();
classRouter.use(bodyparser.json());
classRouter.use(authRoute);
classRouter.get("/:id/topTen", async (req: Request, res: Response) => {
if (!req.params) {
res.send({
error: "400",
data: {
msg: "No class specified",
},
});
return;
}
console.log("Stub(get): /class/:id/topTen");
let users: any[] = [];
let nr = 10;
for (let i = 0; i < 10; i++)
users = users.concat({
username: `Test User ${i}`,
score: 100 * i,
level: Math.floor(Math.random() * Math.floor(10)),
nr: nr--,
});
// TODO: Implement
res.send({
error: "0",
data: {
topTen: users,
}
});
});
export default classRouter;

42
backend/src/api/level.ts Normal file
View File

@ -0,0 +1,42 @@
import { Router, Request, Response } from "express";
import * as bodyparser from "body-parser";
import { authRoute } from "../security/token";
const levelRouter = Router();
levelRouter.use(bodyparser.json());
levelRouter.use(authRoute);
levelRouter.get("/:id/vocab", async (req: Request, res: Response) => {
// TODO: Implement
console.log("Stub(get): /level/:id/vocab");
if (!req.params) {
res.send({
error: "400",
data: {
msg: "No level specified",
},
});
return;
}
res.send({
error: "0",
data: {
vocab: [{
german: ["Wein"],
hint: "Worte auf '-um' sind meistens NeutrUM",
type: 0,
latin: {
grundform: "Vinum",
genitiv: "Vini",
genus: "Neutrum"
},
id: 0
}],
}
});
});
export default levelRouter;

108
backend/src/api/user.ts Normal file
View File

@ -0,0 +1,108 @@
import * as express from "express";
import * as bodyparser from "body-parser";
import { authRoute } from "../security/token";
const userRouter = express.Router();
userRouter.use(bodyparser.json());
userRouter.use(authRoute);
// Return the user object if the user is still authenticated
userRouter.get("/get", async (req, res) => {
console.log("STUB: /user/get");
// TODO: Stub
res.send({
error: "0",
data: {
test: 0,
},
});
});
// Removes the user's session
userRouter.get("/logout", async (req, res) => {
console.log("STUB: /user/logout");
// TODO: Stub
res.send({
error: "0",
data: {},
});
});
// Gets the user's review queue
userRouter.get("/queue", async (req, res) => {
console.log("STUB: /user/queue");
// TODO: Stub
res.send({
error: "0",
data: {},
});
});
// Get ot set the last review results
userRouter.get("/lastReview", async (req, res) => {
console.log("STUB(get): /user/lastReview");
// TODO: Stub
res.send({
error: "0",
data: {},
});
});
userRouter.post("/lastReview", async (req, res) => {
console.log("STUB(post): /user/lastReview");
// TODO: Stub
res.send({
error: "0",
data: {},
});
});
// Get the next level
userRouter.get("/nextLevel", async (req, res) => {
console.log("STUB: /user/nextLevel");
// TODO: Stub
res.send({
error: "0",
data: {
name: "Test level",
desc: "Just a test",
level: 3,
done: false,
},
});
});
// Mark a level as done
userRouter.post("/level/:id", async (req, res) => {
console.log("STUB(post): /user/level/:id");
// TODO: Stub
res.send({
error: "0",
data: {},
});
});
// Get the data needed for the dashboard
userRouter.post("/dashboard", async (req, res) => {
console.log("STUB(post): /user/dashboard");
// TODO: Stub
res.send({
error: "0",
data: {
nextLevel: {},
topTen: {},
lastReview: {},
},
});
});
export default userRouter;

View File

@ -4,6 +4,10 @@ import * as bodyparser from "body-parser";
import { isAuthenticated, performLogin } from "./security/auth";
import UserRouter from "./api/user";
import ClassRouter from "./api/class";
import LevelRouter from "./api/level";
const baseRouter = express.Router();
const authRouter = express.Router();
@ -27,94 +31,20 @@ authRouter.use(async (req, res, next) => {
});
}
});
authRouter.get("/class/:id/topTen", async (req, res) => {
if (!req.params) {
res.send({
error: "400",
data: {
msg: "No class specified",
},
});
return;
}
console.log("Stub: /auth/class/:id/topTen");
let users: any[] = [];
let nr = 10;
for (let i = 0; i < 10; i++)
users = users.concat({
username: `Test User ${i}`,
score: 100 * i,
level: Math.floor(Math.random() * Math.floor(10)),
nr: nr--,
});
// TODO: Implement
res.send({
error: "0",
data: {
topTen: users,
}
});
});
authRouter.get("/level/:id/vocab", async (req, res) => {
// TODO: Implement
console.log("Stub: /auth/level/:id/vocab");
if (!req.params) {
res.send({
error: "400",
data: {
msg: "No level specified",
},
});
return;
}
res.send({
error: "0",
data: {
vocab: [{
german: ["Wein"],
hint: "Worte auf '-um' sind meistens NeutrUM",
type: 0,
latin: {
grundform: "Vinum",
genitiv: "Vini",
genus: "Neutrum"
},
id: 0
}],
}
});
});
authRouter.get("/user/nextLevel", async (req, res) => {
console.log("Stub: /auth/user/nextLevel");
res.send({
error: "0",
data: {
name: "Test level",
desc: "Just a test",
level: 3,
done: false,
},
});
});
const app = express();
app.use(bodyparser.json());
app.use((req, res, next) => {
// TODO: Change this to our domain
res.append("Access-Control-Allow-Origin", "*");
res.append("Access-Control-Allow-Headers", "Content-Type,Token");
next();
});
app.use("/auth", authRouter);
app.get("/health", (req, res) => {
app.use("/api/level", LevelRouter);
app.use("/api/class", ClassRouter);
app.use("/api/user", UserRouter);
app.get("/api/health", (req, res) => {
res.send({
error: "0",
data: {
@ -122,7 +52,7 @@ app.get("/health", (req, res) => {
},
});
});
app.post("/login", async (req, res) => {
app.post("/api/login", async (req, res) => {
const { body } = req;
console.log("Stub: /login");

View File

@ -0,0 +1,23 @@
import { Request, Response } from "express";
import { isAuthenticated } from "../security/auth";
export async function authRoute(req: Request, res: Response, next: () => void) {
const token = req.get("Token");
if (token) {
// Check if were authenticated
const auth = await isAuthenticated(token);
if (auth)
next();
else
res.send({
error: "401",
data: {},
});
} else {
res.send({
error: "401",
data: {},
});
}
};

View File

@ -1,6 +1,7 @@
{
"compilerOptions": {
"target": "ESnext",
"lib": ["ES2015"],
"module": "CommonJS",
"noImplicitAny": true,
"removeComments": true,