diff --git a/src/entities/RequestHandler.ts b/src/entities/RequestHandler.ts index 3277e0a..b297eab 100644 --- a/src/entities/RequestHandler.ts +++ b/src/entities/RequestHandler.ts @@ -6,6 +6,7 @@ import { AccountHelper } from "../helpers/AccountHelper"; import { UploadedFile } from "express-fileupload"; import { prepareFileCreation, generateNewUserDataFileName, pathUserData } from "../utils/UserDataUtils"; import * as sharp from 'sharp'; +import { UserHelper } from "../helpers/UserHelper"; /** * Response to a request @@ -162,16 +163,39 @@ export class RequestHandler { * Get a boolean included in the request * * @param name The name of the POST field + * @param fallback Fallback value to use if the value is not + * found in the request */ - public postBool(name: string) : boolean { + public postBool(name: string, fallback ?: boolean) : boolean { const param = this.getPostParam(name); - if(param == undefined) + if(param == undefined) { + if(fallback != undefined) + return fallback; + this.error(400, "Missing boolean '" + name + "' in the request!"); + } return param === "true" || param === true; } + /** + * Get the ID of a user specified in a POST request + * + * @param name Name of the POST field + */ + public async postUserId(name: string) : Promise { + const userID = this.postInt(name); + + if(userID < 1) + this.error(400, "Invalid user ID specified in '" + name +"'!"); + + if(!await UserHelper.Exists(userID)) + this.error(404, "User with ID " + userID + " not found!"); + + return userID; + } + /** * Get information about an uploaded file *