Search in sources :

Example 56 with ExerciseHint

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

the class ExerciseHintResource method getExerciseHint.

/**
 * {@code GET  exercises/:exerciseId/exercise-hints/:exerciseHintId} : get the exerciseHint with the given id.
 *
 * @param exerciseHintId the id of the exerciseHint to retrieve.
 * @param exerciseId the exerciseId of the exercise of which to retrieve the exerciseHint
 * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the exerciseHint,
 * or with status {@code 404 (Not Found)},
 * or with status {@code 409 (Conflict)} if the exerciseId is not valid.
 */
@GetMapping("exercises/{exerciseId}/exercise-hints/{exerciseHintId}")
@PreAuthorize("hasRole('EDITOR')")
public ResponseEntity<ExerciseHint> getExerciseHint(@PathVariable Long exerciseId, @PathVariable Long exerciseHintId) {
    log.debug("REST request to get ExerciseHint : {}", exerciseHintId);
    ProgrammingExercise exercise = programmingExerciseRepository.findByIdElseThrow(exerciseId);
    authCheckService.checkHasAtLeastRoleForExerciseElseThrow(Role.EDITOR, exercise, null);
    var exerciseHint = exerciseHintRepository.findByIdElseThrow(exerciseHintId);
    if (!exerciseHint.getExercise().getId().equals(exerciseId)) {
        throw new ConflictException("An exercise hint can only be retrieved if the exerciseIds match.", EXERCISE_HINT_ENTITY_NAME, "exerciseIdsMismatch");
    }
    return ResponseEntity.ok().body(exerciseHint);
}
Also used : ConflictException(de.tum.in.www1.artemis.web.rest.errors.ConflictException) ProgrammingExercise(de.tum.in.www1.artemis.domain.ProgrammingExercise) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 57 with ExerciseHint

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

the class ExerciseHintResource method createExerciseHint.

/**
 * {@code POST  exercises/:exerciseId/exercise-hints} : Create a new exerciseHint for an exercise.
 *
 * @param exerciseHint the exerciseHint to create
 * @param exerciseId the exerciseId of the exercise of which to create the exerciseHint
 * @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new exerciseHint,
 * or with status {@code 400 (Bad Request)} if the exerciseHint is a codeHint,
 * or with status {@code 409 (Conflict)} if the exerciseId is invalid,
 * @throws URISyntaxException if the Location URI syntax is incorrect.
 */
@PostMapping("exercises/{exerciseId}/exercise-hints")
@PreAuthorize("hasRole('EDITOR')")
public ResponseEntity<ExerciseHint> createExerciseHint(@RequestBody ExerciseHint exerciseHint, @PathVariable Long exerciseId) throws URISyntaxException {
    log.debug("REST request to save ExerciseHint : {}", exerciseHint);
    // Reload the exercise from the database as we can't trust data from the client
    Exercise exercise = exerciseRepository.findByIdElseThrow(exerciseId);
    authCheckService.checkHasAtLeastRoleForExerciseElseThrow(Role.EDITOR, exercise, null);
    if (exerciseHint instanceof CodeHint) {
        throw new BadRequestAlertException("A code hint cannot be created manually.", CODE_HINT_ENTITY_NAME, "manualCodeHintOperation");
    }
    if (exerciseHint.getExercise() == null) {
        throw new ConflictException("An exercise hint can only be created if the exercise is defined.", EXERCISE_HINT_ENTITY_NAME, "exerciseNotDefined");
    }
    if (!exerciseHint.getExercise().getId().equals(exerciseId)) {
        throw new ConflictException("An exercise hint can only be created if the exerciseIds match.", EXERCISE_HINT_ENTITY_NAME, "exerciseIdMismatch");
    }
    // Hints for exam exercises are not supported at the moment
    if (exercise.isExamExercise()) {
        throw new AccessForbiddenException("Exercise hints for exams are currently not supported");
    }
    ExerciseHint result = exerciseHintRepository.save(exerciseHint);
    return ResponseEntity.created(new URI("/api/exercises/" + exerciseHint.getExercise().getId() + "/exercise-hints/" + result.getId())).headers(HeaderUtil.createEntityCreationAlert(applicationName, true, EXERCISE_HINT_ENTITY_NAME, result.getId().toString())).body(result);
}
Also used : BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) ProgrammingExercise(de.tum.in.www1.artemis.domain.ProgrammingExercise) Exercise(de.tum.in.www1.artemis.domain.Exercise) ConflictException(de.tum.in.www1.artemis.web.rest.errors.ConflictException) ExerciseHint(de.tum.in.www1.artemis.domain.hestia.ExerciseHint) CodeHint(de.tum.in.www1.artemis.domain.hestia.CodeHint) URI(java.net.URI) AccessForbiddenException(de.tum.in.www1.artemis.web.rest.errors.AccessForbiddenException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 58 with ExerciseHint

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

the class ExerciseHintIntegrationTest method updateCodeHintShouldFail.

@Test
@WithMockUser(username = "instructor1", roles = "INSTRUCTOR")
public void updateCodeHintShouldFail() throws Exception {
    CodeHint codeHint = new CodeHint();
    codeHint.setTitle("Hint 1");
    codeHint.setExercise(exercise);
    exerciseHintRepository.save(codeHint);
    codeHint.setTitle("New Title");
    request.put("/api/exercises/" + codeHint.getExercise().getId() + "/exercise-hints/" + codeHint.getId(), codeHint, HttpStatus.BAD_REQUEST);
    Optional<ExerciseHint> hintAfterSave = exerciseHintRepository.findById(codeHint.getId());
    assertThat(hintAfterSave).isPresent();
    assertThat(hintAfterSave.get()).isInstanceOf(CodeHint.class);
    assertThat((hintAfterSave.get()).getTitle()).isEqualTo("Hint 1");
}
Also used : ExerciseHint(de.tum.in.www1.artemis.domain.hestia.ExerciseHint) CodeHint(de.tum.in.www1.artemis.domain.hestia.CodeHint) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.jupiter.api.Test) AbstractSpringIntegrationBambooBitbucketJiraTest(de.tum.in.www1.artemis.AbstractSpringIntegrationBambooBitbucketJiraTest)

Aggregations

ExerciseHint (de.tum.in.www1.artemis.domain.hestia.ExerciseHint)52 AbstractSpringIntegrationBambooBitbucketJiraTest (de.tum.in.www1.artemis.AbstractSpringIntegrationBambooBitbucketJiraTest)38 Test (org.junit.jupiter.api.Test)38 WithMockUser (org.springframework.security.test.context.support.WithMockUser)38 ProgrammingExercise (de.tum.in.www1.artemis.domain.ProgrammingExercise)14 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)12 CodeHint (de.tum.in.www1.artemis.domain.hestia.CodeHint)10 BadRequestAlertException (de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException)8 ConflictException (de.tum.in.www1.artemis.web.rest.errors.ConflictException)8 AccessForbiddenException (de.tum.in.www1.artemis.web.rest.errors.AccessForbiddenException)6 Exercise (de.tum.in.www1.artemis.domain.Exercise)4 Feedback (de.tum.in.www1.artemis.domain.Feedback)2 Submission (de.tum.in.www1.artemis.domain.Submission)2 User (de.tum.in.www1.artemis.domain.User)2 ExerciseHintActivation (de.tum.in.www1.artemis.domain.hestia.ExerciseHintActivation)2 ProgrammingExerciseTask (de.tum.in.www1.artemis.domain.hestia.ProgrammingExerciseTask)2 StudentParticipationRepository (de.tum.in.www1.artemis.repository.StudentParticipationRepository)2 ExerciseHintActivationRepository (de.tum.in.www1.artemis.repository.hestia.ExerciseHintActivationRepository)2 ExerciseHintRepository (de.tum.in.www1.artemis.repository.hestia.ExerciseHintRepository)2 Role (de.tum.in.www1.artemis.security.Role)2