Search in sources :

Example 6 with Feedback

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

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

the class FeedbackResourceIntTest method updateFeedback.

@Test
@Transactional
public void updateFeedback() throws Exception {
    // Initialize the database
    feedbackRepository.saveAndFlush(feedback);
    int databaseSizeBeforeUpdate = feedbackRepository.findAll().size();
    // Update the feedback
    Feedback updatedFeedback = feedbackRepository.findOne(feedback.getId());
    updatedFeedback.text(UPDATED_TEXT).detailText(UPDATED_DETAIL_TEXT).positive(UPDATED_POSITIVE).type(UPDATED_TYPE);
    restFeedbackMockMvc.perform(put("/api/feedbacks").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(updatedFeedback))).andExpect(status().isOk());
    // Validate the Feedback in the database
    List<Feedback> feedbackList = feedbackRepository.findAll();
    assertThat(feedbackList).hasSize(databaseSizeBeforeUpdate);
    Feedback testFeedback = feedbackList.get(feedbackList.size() - 1);
    assertThat(testFeedback.getText()).isEqualTo(UPDATED_TEXT);
    assertThat(testFeedback.getDetailText()).isEqualTo(UPDATED_DETAIL_TEXT);
    assertThat(testFeedback.isPositive()).isEqualTo(UPDATED_POSITIVE);
    assertThat(testFeedback.getType()).isEqualTo(UPDATED_TYPE);
}
Also used : Feedback(de.tum.in.www1.artemis.domain.Feedback) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Feedback (de.tum.in.www1.artemis.domain.Feedback)6 Timed (com.codahale.metrics.annotation.Timed)4 Transactional (org.springframework.transaction.annotation.Transactional)4 Test (org.junit.Test)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 URI (java.net.URI)2 de.tum.in.www1.artemis.domain (de.tum.in.www1.artemis.domain)1 ResultRepository (de.tum.in.www1.artemis.repository.ResultRepository)1 de.tum.in.www1.artemis.service (de.tum.in.www1.artemis.service)1 BadRequestAlertException (de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException)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 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 Authentication (org.springframework.security.core.Authentication)1