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);
}
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);
}
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);
}
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);
}
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);
}
}
Aggregations