Search in sources :

Example 16 with de.tum.in.www1.artemis.domain.lecture

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

the class AttachmentUnitResource method getAttachmentUnit.

/**
 * GET /lectures/:lectureId/attachment-units/:attachmentUnitId : gets the attachment unit with the specified id
 *
 * @param attachmentUnitId the id of the attachmentUnit to retrieve
 * @param lectureId the id of the lecture to which the unit belongs
 * @return the ResponseEntity with status 200 (OK) and with body the attachment unit, or with status 404 (Not Found)
 */
@GetMapping("/lectures/{lectureId}/attachment-units/{attachmentUnitId}")
@PreAuthorize("hasRole('EDITOR')")
public ResponseEntity<AttachmentUnit> getAttachmentUnit(@PathVariable Long attachmentUnitId, @PathVariable Long lectureId) {
    log.debug("REST request to get AttachmentUnit : {}", attachmentUnitId);
    AttachmentUnit attachmentUnit = attachmentUnitRepository.findByIdElseThrow(attachmentUnitId);
    if (attachmentUnit.getLecture() == null || attachmentUnit.getLecture().getCourse() == null) {
        throw new ConflictException("Lecture unit must be associated to a lecture of a course", "AttachmentUnit", "lectureOrCourseMissing");
    }
    if (!attachmentUnit.getLecture().getId().equals(lectureId)) {
        throw new ConflictException("Requested lecture unit is not part of the specified lecture", "AttachmentUnit", "lectureIdMismatch");
    }
    authorizationCheckService.checkHasAtLeastRoleInCourseElseThrow(Role.EDITOR, attachmentUnit.getLecture().getCourse(), null);
    return ResponseEntity.ok().body(attachmentUnit);
}
Also used : ConflictException(de.tum.in.www1.artemis.web.rest.errors.ConflictException) AttachmentUnit(de.tum.in.www1.artemis.domain.lecture.AttachmentUnit) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 17 with de.tum.in.www1.artemis.domain.lecture

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

the class ExerciseUnitResource method getAllExerciseUnitsOfLecture.

/**
 * GET /lectures/:lectureId/exercise-units : gets the exercise units associated with a lecture
 *
 * @param lectureId the id of the lecture to get the exercise-units for
 * @return the ResponseEntity with status 200 (OK) and with body the found exercise units
 */
@GetMapping("/lectures/{lectureId}/exercise-units")
@PreAuthorize("hasRole('EDITOR')")
public ResponseEntity<List<ExerciseUnit>> getAllExerciseUnitsOfLecture(@PathVariable Long lectureId) {
    log.debug("REST request to get all exercise units for lecture : {}", lectureId);
    Lecture lecture = lectureRepository.findByIdWithPostsAndLectureUnitsAndLearningGoalsElseThrow(lectureId);
    if (lecture.getCourse() == null) {
        throw new ConflictException("Specified lecture is not part of a course", "ExerciseUnit", "courseMissing");
    }
    authorizationCheckService.checkHasAtLeastRoleForLectureElseThrow(Role.EDITOR, lecture, null);
    List<ExerciseUnit> exerciseUnitsOfLecture = exerciseUnitRepository.findByLectureId(lectureId);
    return ResponseEntity.ok().body(exerciseUnitsOfLecture);
}
Also used : Lecture(de.tum.in.www1.artemis.domain.Lecture) ExerciseUnit(de.tum.in.www1.artemis.domain.lecture.ExerciseUnit) ConflictException(de.tum.in.www1.artemis.web.rest.errors.ConflictException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 18 with de.tum.in.www1.artemis.domain.lecture

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

the class LectureUnitResource method deleteLectureUnit.

/**
 * DELETE lectures/:lectureId/lecture-units/:lectureUnitId
 *
 * @param lectureId     the id of the lecture to which the unit belongs
 * @param lectureUnitId the id of the lecture unit to remove
 * @return the ResponseEntity with status 200 (OK)
 */
@DeleteMapping("/lectures/{lectureId}/lecture-units/{lectureUnitId}")
@PreAuthorize("hasRole('INSTRUCTOR')")
public ResponseEntity<Void> deleteLectureUnit(@PathVariable Long lectureUnitId, @PathVariable Long lectureId) {
    log.info("REST request to delete lecture unit: {}", lectureUnitId);
    LectureUnit lectureUnit = lectureUnitRepository.findByIdWithLearningGoalsBidirectionalElseThrow(lectureUnitId);
    if (lectureUnit.getLecture() == null || lectureUnit.getLecture().getCourse() == null) {
        throw new ConflictException("Lecture unit must be associated to a lecture of a course", "LectureUnit", "lectureOrCourseMissing");
    }
    if (!lectureUnit.getLecture().getId().equals(lectureId)) {
        throw new ConflictException("Requested lecture unit is not part of the specified lecture", "LectureUnit", "lectureIdMismatch");
    }
    authorizationCheckService.checkHasAtLeastRoleInCourseElseThrow(Role.INSTRUCTOR, lectureUnit.getLecture().getCourse(), null);
    String lectureUnitName;
    if (lectureUnit instanceof ExerciseUnit && ((ExerciseUnit) lectureUnit).getExercise() != null) {
        lectureUnitName = ((ExerciseUnit) lectureUnit).getExercise().getTitle();
    } else if (lectureUnit instanceof AttachmentUnit && ((AttachmentUnit) lectureUnit).getAttachment() != null) {
        lectureUnitName = ((AttachmentUnit) lectureUnit).getAttachment().getName();
    } else {
        lectureUnitName = lectureUnit.getName();
    }
    if (Objects.isNull(lectureUnitName)) {
        lectureUnitName = "lectureUnitWithoutName";
    }
    lectureUnitService.removeLectureUnit(lectureUnit);
    return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(applicationName, true, ENTITY_NAME, lectureUnitName)).build();
}
Also used : ExerciseUnit(de.tum.in.www1.artemis.domain.lecture.ExerciseUnit) LectureUnit(de.tum.in.www1.artemis.domain.lecture.LectureUnit) ConflictException(de.tum.in.www1.artemis.web.rest.errors.ConflictException) AttachmentUnit(de.tum.in.www1.artemis.domain.lecture.AttachmentUnit) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 19 with de.tum.in.www1.artemis.domain.lecture

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

the class TextUnitResource method getTextUnit.

/**
 * GET lectures/:lectureId/text-units/:textUnitId : gets the text unit with the specified id
 *
 * @param textUnitId the id of the textUnit to retrieve
 * @param lectureId the id of the lecture to which the unit belongs
 * @return the ResponseEntity with status 200 (OK) and with body the text unit, or with status 404 (Not Found)
 */
@GetMapping("lectures/{lectureId}/text-units/{textUnitId}")
@PreAuthorize("hasRole('EDITOR')")
public ResponseEntity<TextUnit> getTextUnit(@PathVariable Long textUnitId, @PathVariable Long lectureId) {
    log.debug("REST request to get TextUnit : {}", textUnitId);
    Optional<TextUnit> optionalTextUnit = textUnitRepository.findById(textUnitId);
    if (optionalTextUnit.isEmpty()) {
        throw new EntityNotFoundException("TextUnit");
    }
    TextUnit textUnit = optionalTextUnit.get();
    if (textUnit.getLecture() == null || textUnit.getLecture().getCourse() == null || !textUnit.getLecture().getId().equals(lectureId)) {
        throw new ConflictException("Input data not valid", ENTITY_NAME, "inputInvalid");
    }
    authorizationCheckService.checkHasAtLeastRoleForLectureElseThrow(Role.EDITOR, textUnit.getLecture(), null);
    return ResponseEntity.ok().body(textUnit);
}
Also used : ConflictException(de.tum.in.www1.artemis.web.rest.errors.ConflictException) TextUnit(de.tum.in.www1.artemis.domain.lecture.TextUnit) EntityNotFoundException(de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 20 with de.tum.in.www1.artemis.domain.lecture

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

the class VideoUnitResource method createVideoUnit.

/**
 * POST /lectures/:lectureId/video-units : creates a new video unit.
 *
 * @param lectureId      the id of the lecture to which the video unit should be added
 * @param videoUnit the video unit that should be created
 * @return the ResponseEntity with status 201 (Created) and with body the new video unit
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PostMapping("/lectures/{lectureId}/video-units")
@PreAuthorize("hasRole('EDITOR')")
public ResponseEntity<VideoUnit> createVideoUnit(@PathVariable Long lectureId, @RequestBody VideoUnit videoUnit) throws URISyntaxException {
    log.debug("REST request to create VideoUnit : {}", videoUnit);
    if (videoUnit.getId() != null) {
        throw new BadRequestException();
    }
    normalizeVideoUrl(videoUnit);
    validateVideoUrl(videoUnit);
    Lecture lecture = lectureRepository.findByIdWithPostsAndLectureUnitsAndLearningGoalsElseThrow(lectureId);
    if (lecture.getCourse() == null) {
        throw new ConflictException("Specified lecture is not part of a course", "VideoUnit", "courseMissing");
    }
    authorizationCheckService.checkHasAtLeastRoleInCourseElseThrow(Role.EDITOR, lecture.getCourse(), null);
    // persist lecture unit before lecture to prevent "null index column for collection" error
    videoUnit.setLecture(null);
    videoUnit = videoUnitRepository.saveAndFlush(videoUnit);
    videoUnit.setLecture(lecture);
    lecture.addLectureUnit(videoUnit);
    Lecture updatedLecture = lectureRepository.save(lecture);
    VideoUnit persistedVideoUnit = (VideoUnit) updatedLecture.getLectureUnits().get(updatedLecture.getLectureUnits().size() - 1);
    return ResponseEntity.created(new URI("/api/video-units/" + persistedVideoUnit.getId())).headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, "")).body(persistedVideoUnit);
}
Also used : Lecture(de.tum.in.www1.artemis.domain.Lecture) ConflictException(de.tum.in.www1.artemis.web.rest.errors.ConflictException) BadRequestException(javax.ws.rs.BadRequestException) VideoUnit(de.tum.in.www1.artemis.domain.lecture.VideoUnit) URI(java.net.URI) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)50 Lecture (de.tum.in.www1.artemis.domain.Lecture)42 LectureUnit (de.tum.in.www1.artemis.domain.lecture.LectureUnit)34 WithMockUser (org.springframework.security.test.context.support.WithMockUser)32 Post (de.tum.in.www1.artemis.domain.metis.Post)31 Test (org.junit.jupiter.api.Test)30 ConflictException (de.tum.in.www1.artemis.web.rest.errors.ConflictException)28 ExerciseUnit (de.tum.in.www1.artemis.domain.lecture.ExerciseUnit)22 Course (de.tum.in.www1.artemis.domain.Course)20 TextUnit (de.tum.in.www1.artemis.domain.lecture.TextUnit)18 BadRequestAlertException (de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException)18 AbstractSpringIntegrationBambooBitbucketJiraTest (de.tum.in.www1.artemis.AbstractSpringIntegrationBambooBitbucketJiraTest)16 AttachmentUnit (de.tum.in.www1.artemis.domain.lecture.AttachmentUnit)16 URI (java.net.URI)14 User (de.tum.in.www1.artemis.domain.User)13 BadRequestException (javax.ws.rs.BadRequestException)12 BeforeEach (org.junit.jupiter.api.BeforeEach)12 LearningGoal (de.tum.in.www1.artemis.domain.LearningGoal)10 AnswerPost (de.tum.in.www1.artemis.domain.metis.AnswerPost)10 EntityNotFoundException (de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException)10