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