Search in sources :

Example 6 with Result

use of de.tum.in.www1.artemis.domain.Result 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 7 with Result

use of de.tum.in.www1.artemis.domain.Result in project ArTEMiS by ls1intum.

the class ModelingExerciseResource method updateModelingExercise.

/**
 * PUT  /modeling-exercises : Updates an existing modelingExercise.
 *
 * @param modelingExercise the modelingExercise to update
 * @return the ResponseEntity with status 200 (OK) and with body the updated modelingExercise,
 * or with status 400 (Bad Request) if the modelingExercise is not valid,
 * or with status 500 (Internal Server Error) if the modelingExercise couldn't be updated
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PutMapping("/modeling-exercises")
@Timed
public ResponseEntity<ModelingExercise> updateModelingExercise(@RequestBody ModelingExercise modelingExercise) throws URISyntaxException {
    log.debug("REST request to update ModelingExercise : {}", modelingExercise);
    if (modelingExercise.getId() == null) {
        return createModelingExercise(modelingExercise);
    }
    ModelingExercise result = modelingExerciseRepository.save(modelingExercise);
    return ResponseEntity.ok().headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, modelingExercise.getId().toString())).body(result);
}
Also used : ModelingExercise(de.tum.in.www1.artemis.domain.ModelingExercise) Timed(com.codahale.metrics.annotation.Timed)

Example 8 with Result

use of de.tum.in.www1.artemis.domain.Result 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 9 with Result

use of de.tum.in.www1.artemis.domain.Result in project ArTEMiS by ls1intum.

the class ProgrammingExerciseResource method updateProgrammingExercise.

/**
 * PUT  /programming-exercises : Updates an existing programmingExercise.
 *
 * @param programmingExercise the programmingExercise to update
 * @return the ResponseEntity with status 200 (OK) and with body the updated programmingExercise,
 * or with status 400 (Bad Request) if the programmingExercise is not valid,
 * or with status 500 (Internal Server Error) if the programmingExercise couldn't be updated
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PutMapping("/programming-exercises")
@PreAuthorize("hasAnyRole('TA', 'INSTRUCTOR', 'ADMIN')")
@Timed
public ResponseEntity<ProgrammingExercise> updateProgrammingExercise(@RequestBody ProgrammingExercise programmingExercise) throws URISyntaxException {
    log.debug("REST request to update ProgrammingExercise : {}", programmingExercise);
    if (programmingExercise.getId() == null) {
        return createProgrammingExercise(programmingExercise);
    }
    // fetch course from database to make sure client didn't change groups
    Course course = courseService.findOne(programmingExercise.getCourse().getId());
    if (course == null) {
        return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "courseNotFound", "The course belonging to this programming exercise does not exist")).body(null);
    }
    User user = userService.getUserWithGroupsAndAuthorities();
    if (!authCheckService.isTeachingAssistantInCourse(course, user) && !authCheckService.isInstructorInCourse(course, user) && !authCheckService.isAdmin()) {
        return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
    }
    ResponseEntity<ProgrammingExercise> errorResponse = checkProgrammingExerciseForError(programmingExercise);
    if (errorResponse != null) {
        return errorResponse;
    }
    ProgrammingExercise result = programmingExerciseRepository.save(programmingExercise);
    return ResponseEntity.ok().headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, programmingExercise.getId().toString())).body(result);
}
Also used : User(de.tum.in.www1.artemis.domain.User) ProgrammingExercise(de.tum.in.www1.artemis.domain.ProgrammingExercise) Course(de.tum.in.www1.artemis.domain.Course) Timed(com.codahale.metrics.annotation.Timed) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 10 with Result

use of de.tum.in.www1.artemis.domain.Result in project ArTEMiS by ls1intum.

the class ExceptionTranslator method handleMethodArgumentNotValid.

@Override
public ResponseEntity<Problem> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, @Nonnull NativeWebRequest request) {
    BindingResult result = ex.getBindingResult();
    List<FieldErrorVM> fieldErrors = result.getFieldErrors().stream().map(f -> new FieldErrorVM(f.getObjectName(), f.getField(), f.getCode())).collect(Collectors.toList());
    Problem problem = Problem.builder().withType(ErrorConstants.CONSTRAINT_VIOLATION_TYPE).withTitle("Method argument not valid").withStatus(defaultConstraintViolationStatus()).with("message", ErrorConstants.ERR_VALIDATION).with("fieldErrors", fieldErrors).build();
    return create(ex, problem, request);
}
Also used : ControllerAdvice(org.springframework.web.bind.annotation.ControllerAdvice) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) ClosedChannelException(java.nio.channels.ClosedChannelException) BindingResult(org.springframework.validation.BindingResult) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ProblemHandling(org.zalando.problem.spring.web.advice.ProblemHandling) Collectors(java.util.stream.Collectors) DefaultProblem(org.zalando.problem.DefaultProblem) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) List(java.util.List) Problem(org.zalando.problem.Problem) ProblemBuilder(org.zalando.problem.ProblemBuilder) Status(org.zalando.problem.Status) ConstraintViolationProblem(org.zalando.problem.spring.web.advice.validation.ConstraintViolationProblem) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseEntity(org.springframework.http.ResponseEntity) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) HeaderUtil(de.tum.in.www1.artemis.web.rest.util.HeaderUtil) BindingResult(org.springframework.validation.BindingResult) DefaultProblem(org.zalando.problem.DefaultProblem) Problem(org.zalando.problem.Problem) ConstraintViolationProblem(org.zalando.problem.spring.web.advice.validation.ConstraintViolationProblem)

Aggregations

Timed (com.codahale.metrics.annotation.Timed)15 URI (java.net.URI)10 Result (de.tum.in.www1.artemis.domain.Result)8 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)8 Test (org.junit.Test)7 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)7 BadRequestAlertException (de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException)6 RestTemplate (org.springframework.web.client.RestTemplate)6 MalformedURLException (java.net.MalformedURLException)5 Transactional (org.springframework.transaction.annotation.Transactional)5 Participation (de.tum.in.www1.artemis.domain.Participation)4 BambooException (de.tum.in.www1.artemis.exception.BambooException)4 GitException (de.tum.in.www1.artemis.exception.GitException)4 HeaderUtil (de.tum.in.www1.artemis.web.rest.util.HeaderUtil)4 IOException (java.io.IOException)4 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)4 HttpEntity (org.springframework.http.HttpEntity)4 HttpHeaders (org.springframework.http.HttpHeaders)4 ResponseEntity (org.springframework.http.ResponseEntity)4 de.tum.in.www1.artemis.domain (de.tum.in.www1.artemis.domain)3