partey_workadventure/back/src/Controller/MapController.ts

53 lines
1.9 KiB
TypeScript
Raw Normal View History

import {OK} from "http-status-codes";
import {URL_ROOM_STARTED} from "../Enum/EnvironmentVariable";
2020-09-28 18:52:54 +02:00
import {HttpRequest, HttpResponse, TemplatedApp} from "uWebSockets.js";
import {BaseController} from "./BaseController";
2020-10-13 15:12:24 +02:00
import {parse} from "query-string";
import {adminApi} from "../Services/AdminApi";
2020-09-28 15:02:37 +02:00
//todo: delete this
2020-09-28 18:52:54 +02:00
export class MapController extends BaseController{
2020-09-28 18:52:54 +02:00
constructor(private App : TemplatedApp) {
super();
this.App = App;
2020-10-13 15:12:24 +02:00
this.getMapUrl();
}
// Returns a map mapping map name to file name of the map
2020-10-13 15:12:24 +02:00
getMapUrl() {
this.App.options("/map", (res: HttpResponse, req: HttpRequest) => {
2020-09-28 18:52:54 +02:00
this.addCorsHeaders(res);
res.end();
});
2020-10-13 15:12:24 +02:00
this.App.get("/map", (res: HttpResponse, req: HttpRequest) => {
2020-09-28 18:52:54 +02:00
this.addCorsHeaders(res);
2020-10-13 15:12:24 +02:00
const query = parse(req.getQuery());
if (typeof query.organizationSlug !== 'string') {
console.error('Expected organizationSlug parameter');
res.writeStatus("400 Bad request").end("Expected organizationSlug parameter");
}
if (typeof query.worldSlug !== 'string') {
console.error('Expected worldSlug parameter');
res.writeStatus("400 Bad request").end("Expected worldSlug parameter");
}
if (typeof query.roomSlug !== 'string' && query.roomSlug !== undefined) {
console.error('Expected only one roomSlug parameter');
res.writeStatus("400 Bad request").end("Expected only one roomSlug parameter");
}
(async () => {
const mapDetails = await adminApi.fetchMapDetails(query.organizationSlug as string, query.worldSlug as string, query.roomSlug as string|undefined);
res.writeStatus("200 OK").end(JSON.stringify(mapDetails));
})();
});
}
}