Search in sources :

Example 1 with BadRequestAlertException

use of de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException in project ArTEMiS by ls1intum.

the class CourseResource method createCourse.

/**
 * POST  /courses : Create a new course.
 *
 * @param course the course to create
 * @return the ResponseEntity with status 201 (Created) and with body the new course, or with status 400 (Bad Request) if the course has already an ID
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PostMapping("/courses")
@PreAuthorize("hasAnyRole('ADMIN')")
@Timed
public ResponseEntity<Course> createCourse(@RequestBody Course course) throws URISyntaxException {
    log.debug("REST request to save Course : {}", course);
    if (course.getId() != null) {
        throw new BadRequestAlertException("A new course cannot already have an ID", ENTITY_NAME, "idexists");
    }
    Course result = courseService.save(course);
    return ResponseEntity.created(new URI("/api/courses/" + result.getId())).headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString())).body(result);
}
Also used : BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) URI(java.net.URI) Timed(com.codahale.metrics.annotation.Timed) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 2 with BadRequestAlertException

use of de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException in project ArTEMiS by ls1intum.

the class ParticipationResource method createParticipation.

/**
 * POST  /participations : Create a new participation.
 *
 * @param participation the participation to create
 * @return the ResponseEntity with status 201 (Created) and with body the new participation, or with status 400 (Bad Request) if the participation has already an ID
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PostMapping("/participations")
@PreAuthorize("hasAnyRole('TA', 'INSTRUCTOR', 'ADMIN')")
@Timed
public ResponseEntity<Participation> createParticipation(@RequestBody Participation participation) throws URISyntaxException {
    log.debug("REST request to save Participation : {}", participation);
    Course course = participation.getExercise().getCourse();
    if (!courseService.userHasTAPermissions(course)) {
        return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
    }
    if (participation.getId() != null) {
        throw new BadRequestAlertException("A new participation cannot already have an ID", ENTITY_NAME, "idexists");
    }
    Participation result = participationService.save(participation);
    return ResponseEntity.created(new URI("/api/participations/" + result.getId())).headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString())).body(result);
}
Also used : BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) URI(java.net.URI) Timed(com.codahale.metrics.annotation.Timed) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 3 with BadRequestAlertException

use of de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException in project ArTEMiS by ls1intum.

the class ExerciseResource method createExercise.

/**
 * POST  /exercises : Create a new exercise.
 *
 * @param exercise the exercise to create
 * @return the ResponseEntity with status 201 (Created) and with body the new exercise, or with status 400 (Bad Request) if the exercise has already an ID
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PostMapping("/exercises")
@PreAuthorize("hasAnyRole('TA', 'INSTRUCTOR', 'ADMIN')")
@Timed
public // TODO: test if it still works with abstract entity in body
ResponseEntity<Exercise> createExercise(@RequestBody Exercise exercise) throws URISyntaxException {
    log.debug("REST request to save Exercise : {}", exercise);
    Course course = exercise.getCourse();
    User user = userService.getUserWithGroupsAndAuthorities();
    if (!authCheckService.isTeachingAssistantInCourse(course, user) && !authCheckService.isInstructorInCourse(course, user) && !authCheckService.isAdmin()) {
        return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
    }
    if (exercise.getId() != null) {
        throw new BadRequestAlertException("A new exercise cannot already have an ID", ENTITY_NAME, "idexists");
    }
    if (exercise instanceof ProgrammingExercise) {
        ResponseEntity<Exercise> errorResponse = checkProgrammingExerciseForError((ProgrammingExercise) exercise);
        if (errorResponse != null) {
            return errorResponse;
        }
    }
    Exercise result = exerciseRepository.save(exercise);
    return ResponseEntity.created(new URI("/api/exercises/" + result.getId())).headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString())).body(result);
}
Also used : BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) URI(java.net.URI) Timed(com.codahale.metrics.annotation.Timed) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 4 with BadRequestAlertException

use of de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException in project ArTEMiS by ls1intum.

the class ResultResource method createResult.

/**
 * POST  /results : Create a new manual result.
 *
 * @param result the result to create
 * @return the ResponseEntity with status 201 (Created) and with body the new result, or with status 400 (Bad Request) if the result has already an ID
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PostMapping("/results")
@PreAuthorize("hasAnyRole('TA', 'INSTRUCTOR', 'ADMIN')")
@Timed
public ResponseEntity<Result> createResult(@RequestBody Result result) throws URISyntaxException {
    log.debug("REST request to save Result : {}", result);
    Participation participation = result.getParticipation();
    Course course = participation.getExercise().getCourse();
    User user = userService.getUserWithGroupsAndAuthorities();
    if (!authCheckService.isTeachingAssistantInCourse(course, user) && !authCheckService.isInstructorInCourse(course, user) && !authCheckService.isAdmin()) {
        return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
    }
    if (result.getId() != null) {
        throw new BadRequestAlertException("A new result cannot already have an ID.", ENTITY_NAME, "idexists");
    } else if (result.getResultString() == null) {
        throw new BadRequestAlertException("Result string is required.", ENTITY_NAME, "resultStringNull");
    } else if (result.getScore() == null) {
        throw new BadRequestAlertException("Score is required.", ENTITY_NAME, "scoreNull");
    } else if (result.getScore() != 100 && result.isSuccessful()) {
        throw new BadRequestAlertException("Only result with score 100% can be successful.", ENTITY_NAME, "scoreAndSuccessfulNotMatching");
    } else if (!result.getFeedbacks().isEmpty() && result.getFeedbacks().stream().filter(feedback -> feedback.getText() == null).count() != 0) {
        throw new BadRequestAlertException("In case feedback is present, feedback text and detail text are mandatory.", ENTITY_NAME, "feedbackTextOrDetailTextNull");
    }
    if (!result.getFeedbacks().isEmpty()) {
        result.setHasFeedback(true);
    }
    Result savedResult = resultRepository.save(result);
    result.getFeedbacks().forEach(feedback -> {
        feedback.setResult(savedResult);
        feedbackService.save(feedback);
    });
    ltiService.ifPresent(ltiService -> ltiService.onNewBuildResult(savedResult.getParticipation()));
    return ResponseEntity.created(new URI("/api/results/" + result.getId())).headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString())).body(result);
}
Also used : BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) de.tum.in.www1.artemis.service(de.tum.in.www1.artemis.service) java.util(java.util) Logger(org.slf4j.Logger) BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) URISyntaxException(java.net.URISyntaxException) ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) Timed(com.codahale.metrics.annotation.Timed) HttpStatus(org.springframework.http.HttpStatus) de.tum.in.www1.artemis.domain(de.tum.in.www1.artemis.domain) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) ResponseEntity(org.springframework.http.ResponseEntity) URI(java.net.URI) Authentication(org.springframework.security.core.Authentication) ResultRepository(de.tum.in.www1.artemis.repository.ResultRepository) HeaderUtil(de.tum.in.www1.artemis.web.rest.util.HeaderUtil) Transactional(org.springframework.transaction.annotation.Transactional) URI(java.net.URI) Timed(com.codahale.metrics.annotation.Timed) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 5 with BadRequestAlertException

use of de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException in project ArTEMiS by ls1intum.

the class UserResource method createUser.

/**
 * POST  /users  : Creates a new user.
 * <p>
 * Creates a new user if the login and email are not already used, and sends an
 * mail with an activation link.
 * The user needs to be activated on creation.
 *
 * @param managedUserVM the user to create
 * @return the ResponseEntity with status 201 (Created) and with body the new user, or with status 400 (Bad Request) if the login or email is already in use
 * @throws URISyntaxException if the Location URI syntax is incorrect
 * @throws BadRequestAlertException 400 (Bad Request) if the login or email is already in use
 */
@PostMapping("/users")
@Timed
@Secured(AuthoritiesConstants.ADMIN)
public ResponseEntity<User> createUser(@Valid @RequestBody ManagedUserVM managedUserVM) throws URISyntaxException {
    log.debug("REST request to save User : {}", managedUserVM);
    if (managedUserVM.getId() != null) {
        throw new BadRequestAlertException("A new user cannot already have an ID", "userManagement", "idexists");
    // Lowercase the user login before comparing with database
    } else if (userRepository.findOneByLogin(managedUserVM.getLogin().toLowerCase()).isPresent()) {
        throw new LoginAlreadyUsedException();
    } else if (userRepository.findOneByEmailIgnoreCase(managedUserVM.getEmail()).isPresent()) {
        throw new EmailAlreadyUsedException();
    } else {
        User newUser = userService.createUser(managedUserVM);
        mailService.sendCreationEmail(newUser);
        return ResponseEntity.created(new URI("/api/users/" + newUser.getLogin())).headers(HeaderUtil.createAlert("userManagement.created", newUser.getLogin())).body(newUser);
    }
}
Also used : BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) User(de.tum.in.www1.artemis.domain.User) LoginAlreadyUsedException(de.tum.in.www1.artemis.web.rest.errors.LoginAlreadyUsedException) URI(java.net.URI) EmailAlreadyUsedException(de.tum.in.www1.artemis.web.rest.errors.EmailAlreadyUsedException) Secured(org.springframework.security.access.annotation.Secured) Timed(com.codahale.metrics.annotation.Timed)

Aggregations

Timed (com.codahale.metrics.annotation.Timed)5 BadRequestAlertException (de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException)5 URI (java.net.URI)5 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 de.tum.in.www1.artemis.domain (de.tum.in.www1.artemis.domain)1 User (de.tum.in.www1.artemis.domain.User)1 ResultRepository (de.tum.in.www1.artemis.repository.ResultRepository)1 de.tum.in.www1.artemis.service (de.tum.in.www1.artemis.service)1 EmailAlreadyUsedException (de.tum.in.www1.artemis.web.rest.errors.EmailAlreadyUsedException)1 LoginAlreadyUsedException (de.tum.in.www1.artemis.web.rest.errors.LoginAlreadyUsedException)1 HeaderUtil (de.tum.in.www1.artemis.web.rest.util.HeaderUtil)1 URISyntaxException (java.net.URISyntaxException)1 ZonedDateTime (java.time.ZonedDateTime)1 java.util (java.util)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 HttpStatus (org.springframework.http.HttpStatus)1 ResponseEntity (org.springframework.http.ResponseEntity)1 Secured (org.springframework.security.access.annotation.Secured)1 Authentication (org.springframework.security.core.Authentication)1