How to use the routing-controllers.BadRequestError function in routing-controllers

To help you get started, we’ve selected a few routing-controllers examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github geli-lms / geli / api / src / controllers / NotificationSettingsController.ts View on Github external
async createNotificationSettings(@Body() data: any) {
    if (!data.user || !data.course) {
      throw new BadRequestError('NotificationSettings need course and user');
    }
    const notificationSettings: INotificationSettingsModel =
      await NotificationSettings.findOne({'user': data.user, 'course': data.course});
    if (notificationSettings) {
      throw new BadRequestError('NotificationSettings for user:' + data.user + ' with course: ' + data.course + ' already exist');
    }
    const settings: INotificationSettingsModel = await new NotificationSettings({
      'user': data.user,
      'course': data.course,
      'notificationType': API_NOTIFICATION_TYPE_ALL_CHANGES,
      'emailNotification': false
    }).save();
    return settings.toObject();
  }
github SammyLiang97 / Mikey / src / controllers / user.controller.ts View on Github external
async createAdministrator(@BodyParam('username', {required: true}) username: string,
                              @BodyParam('password', {required: true}) password: string
    ) {
        if (Environment.identity === 'development') {
            if (await this.userService.userModel.count({username: username})) {
                throw new BadRequestError('重复的用户名')
            } else {
                return await this.userService.userModel.create({
                    username: username,
                    password: password,
                    usertype: 1
                })
            }
        } else {
            throw new BadRequestError()
        }
    }
github geli-lms / geli / api / src / models / units / CodeKataUnit.ts View on Github external
function validateTestArea(testArea: any) {
  if (!testArea.match(new RegExp('function(.|\t)*validate\\(\\)(.|\n|\t)*{(.|\n|\t)*}', 'gmi'))) {
    throw new BadRequestError('The test section must contain a validate function');
  }
  if (!testArea.match(new RegExp('function(.|\t)*validate\\(\\)(.|\n|\t)*{(.|\n|\t)*return(.|\n|\t)*}', 'gmi'))) {
    throw new BadRequestError('The validate function must return something');
  }
  if (!testArea.match(new RegExp('validate\\(\\);', 'gmi'))) {
    throw new BadRequestError('The test section must call the validate function');
  }

  return true;
}
github geli-lms / geli / api / src / controllers / UnitController.ts View on Github external
protected checkPostParam(data: any) {
    if (!data.lectureId) {
      throw new BadRequestError('No lecture ID was submitted.');
    }

    if (!data.model) {
      throw new BadRequestError('No unit was submitted.');
    }

    if (!data.model._course) {
      throw new BadRequestError('Unit has no _course set');
    }
  }
}
github geli-lms / geli / api / src / controllers / StudentConfigController.ts View on Github external
async updateStudentConfig(@Param('id') id: string, @Body() config: IStudentConfig, @CurrentUser() currentUser: IUser) {
    if (currentUser._id !== config.user) {
      throw new BadRequestError('Student id does not match');
    }
    const newConfig = await StudentConfig.findByIdAndUpdate(id, config, {'new': false});
    return newConfig.toObject({virtuals: true});
  }
github geli-lms / geli / api / src / controllers / CourseController.ts View on Github external
.then((existingCourse) => {
        if (existingCourse) {
          throw new BadRequestError(errorCodes.errorCodes.course.duplicateName.code);
        }
        return new Course(course).save()
          .then((c) => c.toObject());
      });
  }
github geli-lms / geli / api / src / controllers / StudentConfigController.ts View on Github external
async postUnitProgress(@Body() config: IStudentConfig, @CurrentUser() currentUser: IUser) {
    const result = await StudentConfig.findOne({'user': config.user});
      if (result) {
        throw new BadRequestError('Student config already existed');
      }
    if (currentUser._id !== config.user) {
      throw new BadRequestError('Student id does not match');
    }
    const newConfig = await new StudentConfig(config).save();
    return newConfig.toObject();
  }
github geli-lms / geli / api / src / controllers / WhitelistController.ts View on Github external
async updateWhitelistUser(@Param('id') id: string, @Body() whitelistUser: IWhitelistUser) {
    let updatedWhitelistUser;
    const foundWhitelistUser = await WhitelistUser.findById(id);
    try {
      updatedWhitelistUser = await WhitelistUser.findOneAndUpdate(
        this.toMongooseObjectId(whitelistUser),
        {'new': true});
    } catch (err) {
      throw new BadRequestError(errorCodes.whitelist.duplicateWhitelistUser.text);
    }
    await this.deleteUserIfFound(foundWhitelistUser);
    await this.addUserIfFound(updatedWhitelistUser);
    return updatedWhitelistUser ? updatedWhitelistUser.toObject() : undefined;
  }
github geli-lms / geli / api / src / controllers / NotificationController.ts View on Github external
async createNotificationForStudent(@Param('id') userId: string, @Body() data: any) {
    if (!data.changedCourse && !data.text) {
      throw new BadRequestError('Notification needs at least the field changedCourse or text');
    }
    const user = await User.findById(userId);
    if (!user) {
      throw new BadRequestError('Could not create notification because user not found');
    }
    await this.createNotification(user, data.text, data.changedCourse, data.changedLecture, data.changedUnit);
    return {notified: true};
  }
github geli-lms / geli / api / src / models / units / CodeKataUnit.ts View on Github external
function validateTestArea(testArea: any) {
  if (!testArea.match(new RegExp('function(.|\t)*validate\\(\\)(.|\n|\t)*{(.|\n|\t)*}', 'gmi'))) {
    throw new BadRequestError('The test section must contain a validate function');
  }
  if (!testArea.match(new RegExp('function(.|\t)*validate\\(\\)(.|\n|\t)*{(.|\n|\t)*return(.|\n|\t)*}', 'gmi'))) {
    throw new BadRequestError('The validate function must return something');
  }
  if (!testArea.match(new RegExp('validate\\(\\);', 'gmi'))) {
    throw new BadRequestError('The test section must call the validate function');
  }

  return true;
}