diff --git a/backend/karaoqueue-backend/src/containers/appState.container.ts b/backend/karaoqueue-backend/src/containers/appState.container.ts new file mode 100644 index 0000000..1524a6d --- /dev/null +++ b/backend/karaoqueue-backend/src/containers/appState.container.ts @@ -0,0 +1,14 @@ +import AppState from "../models/appState.model"; +import fs from "fs"; + +const appState = new AppState(); + +if (fs.existsSync("/tmp/.kqueue_eventlock")) { + appState.currentlyInEvent=true; +} else { + appState.currentlyInEvent=false; +} + +appState.registrationEnabled=false; + +export default appState; \ No newline at end of file diff --git a/backend/karaoqueue-backend/src/controllers/queue.controller.ts b/backend/karaoqueue-backend/src/controllers/queue.controller.ts index 0e4738e..3133f95 100644 --- a/backend/karaoqueue-backend/src/controllers/queue.controller.ts +++ b/backend/karaoqueue-backend/src/controllers/queue.controller.ts @@ -1,4 +1,7 @@ -import { Controller, Get, Res, Post, Delete, Patch, Req } from "routing-controllers"; +import { Request, Response } from "express"; +import { Controller, Get, Res, Post, Delete, Patch, Req, Authorized, CurrentUser } from "routing-controllers"; +import appState from "../containers/appState.container"; +import DataStoredInToken from "../interfaces/dataStoredInToken.interface"; @Controller("/queue") @@ -8,25 +11,33 @@ export class QueueController { * Fetch entry Queue content */ @Get() - getQueue(@Req() req: any, @Res() res: any) { + getQueue(@Req() req: Request, @Res() res: Response) { res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify({ placeholder: "//TODO fetch" })); + res.send(JSON.stringify({ placeholder: "//TODO fetch" })); + return res; } /* * Add entry to Queue */ @Post() - addEntry(@Req() req: any, @Res() res: any) { + addEntry(@Req() req: Request, @Res() res: Response) { res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify({ placeholder: "//TODO add" })); + if (appState.registrationEnabled) { + res.send(JSON.stringify({ placeholder: "//TODO add" })); + } else { + res.status(403) + res.send("Entry submission is currently not allowed.") + } + return res; } /* * */ @Delete() - clearQueue(@Req() req: any, @Res() res: any) { + @Authorized() + clearQueue(@Req() req: Request, @Res() res: Response) { return "//TODO clear"; } @@ -34,21 +45,32 @@ export class QueueController { * */ @Get("/:entry:id") - getEntry(@Req() req: any, @Res() res: any) { + getEntry(@Req() req: Request, @Res() res: Response) { res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify({ placeholder: "//TODO get" })); + res.send(JSON.stringify({ placeholder: "//TODO get" })); + return res; } /* * */ @Patch("/:entry_id") - editEntry(@Req() req: any, @Res() res: any) { - return "//TODO edit" + editEntry(@Req() req: Request, @Res() res: Response, @CurrentUser() user?: DataStoredInToken) { + if (user) { + return "You're "+user._id; + } else { + /*TODO: Require song-specific auth to modify queue entry */ + return "You are not logged in." + } } @Delete("/:entry_id") - deleteEntry(@Req() req: any, @Res() res: any) { - return "//TODO delete" + deleteEntry(@Req() req: Request, @Res() res: Response, @CurrentUser() user?: DataStoredInToken) { + if (user) { + return "You're " + user._id; + } else { + /*TODO: Require song-specific auth to delete queue entry */ + return "You are not logged in." + } } } \ No newline at end of file diff --git a/backend/karaoqueue-backend/src/controllers/rpc.controller.ts b/backend/karaoqueue-backend/src/controllers/rpc.controller.ts index e6d7ccb..cb43a28 100644 --- a/backend/karaoqueue-backend/src/controllers/rpc.controller.ts +++ b/backend/karaoqueue-backend/src/controllers/rpc.controller.ts @@ -1,25 +1,52 @@ -import { Controller, Get, Param, QueryParam } from "routing-controllers"; +import { Response } from "express"; +import { Authorized, Controller, Get, Param, QueryParam, Res } from "routing-controllers"; +import appState from "../containers/appState.container"; +import fs from "fs"; @Controller("/rpc") +@Authorized() export class RpcController { @Get("/start_event") doStartEvent() { - return "//TODO start_event" + /* TODO: Wipe state (queue and song playbacks) when starting new event. Maybe also refresh Song database automatically? */ + appState.currentlyInEvent = true; + fs.openSync("/tmp/.kqueue_eventlock", 'w') + return 200; } @Get("/end_event") doEndEvent() { - return "//TODO end_event" + appState.currentlyInEvent = false; + fs.unlinkSync("/tmp/.kqueue_eventlock"); + return 200; } @Get("/enable_registration") - doEnableRegistration() { - return "//TODO enable_registration" + doEnableRegistration(@Res() res: Response) { + if (appState.currentlyInEvent) { + appState.registrationEnabled = true; + return 200; + } else { + res.status(403).send("No event currently active") + return res; + } } @Get("/disable_registration") - doDisableRegistration() { - return "//TODO disable_registration" + doDisableRegistration(@Res() res: Response) { + if (appState.currentlyInEvent) { + appState.registrationEnabled = false; + return 200; + } else { + res.status(403).send("No event currently active") + return res; + } + + } + + @Get("/get_state") + doGetState() { + return appState; } @Get("/get_playstats") diff --git a/backend/karaoqueue-backend/src/models/appState.model.ts b/backend/karaoqueue-backend/src/models/appState.model.ts new file mode 100644 index 0000000..c89aadb --- /dev/null +++ b/backend/karaoqueue-backend/src/models/appState.model.ts @@ -0,0 +1,6 @@ +class AppState { + registrationEnabled: boolean; + currentlyInEvent: boolean; +} + +export default AppState; \ No newline at end of file