Search in sources :

Example 26 with Result

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

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

the class FeedbackResource method updateFeedback.

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

Example 28 with Result

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

the class JiraAuthenticationProvider method getUsernameForEmail.

/**
 * Checks if an JIRA user for the given email address exists.
 *
 * @param email The JIRA user email address
 * @return Optional String of JIRA username
 * @throws ArtemisAuthenticationException
 */
@Override
public Optional<String> getUsernameForEmail(String email) throws ArtemisAuthenticationException {
    HttpHeaders headers = HeaderUtil.createAuthorization(JIRA_USER, JIRA_PASSWORD);
    HttpEntity<?> entity = new HttpEntity<>(headers);
    RestTemplate restTemplate = new RestTemplate();
    try {
        ResponseEntity<ArrayList> authenticationResponse = restTemplate.exchange(JIRA_URL + "/rest/api/2/user/search?username=" + email, HttpMethod.GET, entity, ArrayList.class);
        ArrayList results = authenticationResponse.getBody();
        if (results.size() == 0) {
            // no result
            return Optional.empty();
        }
        Map firstResult = (Map) results.get(0);
        return Optional.of((String) firstResult.get("name"));
    } catch (HttpClientErrorException e) {
        log.error("Could not get JIRA username for email address " + email, e);
        throw new ArtemisAuthenticationException("Error while checking eMail address");
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) RestTemplate(org.springframework.web.client.RestTemplate) ArtemisAuthenticationException(de.tum.in.www1.artemis.exception.ArtemisAuthenticationException)

Example 29 with Result

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

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

the class QuizSubmissionService method submitForPractice.

/**
 * Submit the given submission for practice
 *
 * @param quizSubmission the submission to submit
 * @param quizExercise the exercise to submit in
 * @param participation the participation where the result should be saved
 * @return the result entity
 */
@Transactional
public Result submitForPractice(QuizSubmission quizSubmission, QuizExercise quizExercise, Participation participation) {
    // update submission properties
    quizSubmission.setSubmitted(true);
    quizSubmission.setType(SubmissionType.MANUAL);
    // calculate scores
    quizSubmission.calculateAndUpdateScores(quizExercise);
    // create and save result
    Result result = new Result().participation(participation).submission(quizSubmission);
    result.setRated(false);
    result.setCompletionDate(ZonedDateTime.now());
    // calculate score and update result accordingly
    result.evaluateSubmission();
    // save result
    resultRepository.save(result);
    // replace proxy with submission, because of Lazy-fetching
    result.setSubmission(quizSubmission);
    // add result to statistics
    QuizScheduleService.addResultToStatistic(quizExercise.getId(), result);
    return result;
}
Also used : Result(de.tum.in.www1.artemis.domain.Result) Transactional(org.springframework.transaction.annotation.Transactional)

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