Search in sources :

Example 6 with Attachment

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

the class AttachmentResource method createAttachment.

/**
 * POST /attachments : Create a new attachment.
 *
 * @param attachment the attachment to create
 * @return the ResponseEntity with status 201 (Created) and with body the new attachment, or with status 400 (Bad Request) if the attachment has already an ID
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PostMapping("/attachments")
@PreAuthorize("hasRole('EDITOR')")
public ResponseEntity<Attachment> createAttachment(@RequestBody Attachment attachment) throws URISyntaxException {
    log.debug("REST request to save Attachment : {}", attachment);
    if (attachment.getId() != null) {
        throw new BadRequestAlertException("A new attachment cannot already have an ID", ENTITY_NAME, "idexists");
    }
    Attachment result = attachmentRepository.save(attachment);
    this.cacheManager.getCache("files").evict(fileService.actualPathForPublicPath(result.getLink()));
    return ResponseEntity.created(new URI("/api/attachments/" + result.getId())).headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, result.getId().toString())).body(result);
}
Also used : BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) Attachment(de.tum.in.www1.artemis.domain.Attachment) URI(java.net.URI) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 7 with Attachment

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

the class LectureImportServiceTest method testImportLectureToCourse.

@Test
@WithMockUser(username = "instructor1", roles = "INSTRUCTOR")
public void testImportLectureToCourse() {
    int lectureCount = this.course2.getLectures().size();
    lectureImportService.importLecture(this.lecture1, this.course2);
    assertThat(this.course2.getLectures().size()).isEqualTo(lectureCount + 1);
    // Find the imported lecture and fetch it with lecture units
    Long lecture2Id = this.course2.getLectures().stream().skip(lectureCount).findFirst().get().getId();
    Lecture lecture2 = this.lectureRepository.findByIdWithLectureUnitsElseThrow(lecture2Id);
    assertThat(lecture2.getTitle()).isEqualTo(this.lecture1.getTitle());
    // Assert that all lecture units (except exercise units) were copied
    assertThat(lecture2.getLectureUnits().stream().map(LectureUnit::getName).toList()).containsExactlyElementsOf(this.lecture1.getLectureUnits().stream().filter(lectureUnit -> !(lectureUnit instanceof ExerciseUnit)).map(LectureUnit::getName).toList());
    assertThat(lecture2.getAttachments().stream().map(Attachment::getName).toList()).containsExactlyElementsOf(this.lecture1.getAttachments().stream().map(Attachment::getName).toList());
    lectureRepository.delete(lecture2);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CourseRepository(de.tum.in.www1.artemis.repository.CourseRepository) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Course(de.tum.in.www1.artemis.domain.Course) Autowired(org.springframework.beans.factory.annotation.Autowired) Lecture(de.tum.in.www1.artemis.domain.Lecture) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) Attachment(de.tum.in.www1.artemis.domain.Attachment) WithMockUser(org.springframework.security.test.context.support.WithMockUser) LectureUnit(de.tum.in.www1.artemis.domain.lecture.LectureUnit) ExerciseUnit(de.tum.in.www1.artemis.domain.lecture.ExerciseUnit) LectureRepository(de.tum.in.www1.artemis.repository.LectureRepository) AbstractSpringIntegrationBambooBitbucketJiraTest(de.tum.in.www1.artemis.AbstractSpringIntegrationBambooBitbucketJiraTest) Lecture(de.tum.in.www1.artemis.domain.Lecture) ExerciseUnit(de.tum.in.www1.artemis.domain.lecture.ExerciseUnit) LectureUnit(de.tum.in.www1.artemis.domain.lecture.LectureUnit) Attachment(de.tum.in.www1.artemis.domain.Attachment) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.jupiter.api.Test) AbstractSpringIntegrationBambooBitbucketJiraTest(de.tum.in.www1.artemis.AbstractSpringIntegrationBambooBitbucketJiraTest)

Example 8 with Attachment

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

the class GroupNotificationFactoryTest method setUp.

/**
 * sets up all needed mocks and their wanted behavior once for all test cases.
 */
@BeforeAll
public static void setUp() {
    course = new Course();
    course.setId(COURSE_ID);
    lecture = new Lecture();
    lecture.setId(LECTURE_ID);
    lecture.setCourse(course);
    exam = new Exam();
    exam.setId(EXAM_ID);
    exam.setCourse(course);
    exerciseGroup = new ExerciseGroup();
    exerciseGroup.setExam(exam);
    exercise = new TextExercise();
    exercise.setId(EXERCISE_ID);
    exercise.setTitle(EXERCISE_TITLE);
    exercise.setCourse(course);
    exercise.setProblemStatement(PROBLEM_STATEMENT);
    programmingExercise = new ProgrammingExercise();
    programmingExercise.setId(EXERCISE_ID);
    programmingExercise.setTitle(EXERCISE_TITLE);
    programmingExercise.setCourse(course);
    programmingExercise.setProblemStatement(PROBLEM_STATEMENT);
    examExercise = new TextExercise();
    examExercise.setId(EXERCISE_ID);
    examExercise.setTitle(EXERCISE_TITLE);
    examExercise.setCourse(course);
    examExercise.setExerciseGroup(exerciseGroup);
    examExercise.setProblemStatement(PROBLEM_STATEMENT);
    attachment = new Attachment();
    attachment.setLecture(lecture);
    post = new Post();
    post.setExercise(exercise);
    post.setLecture(lecture);
    answerPost = new AnswerPost();
    answerPost.setPost(post);
}
Also used : AnswerPost(de.tum.in.www1.artemis.domain.metis.AnswerPost) Post(de.tum.in.www1.artemis.domain.metis.Post) ExerciseGroup(de.tum.in.www1.artemis.domain.exam.ExerciseGroup) Exam(de.tum.in.www1.artemis.domain.exam.Exam) AnswerPost(de.tum.in.www1.artemis.domain.metis.AnswerPost) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 9 with Attachment

use of de.tum.in.www1.artemis.domain.Attachment in project Artemis by ls1intum.

the class AttachmentUnitResource method updateAttachmentUnit.

/**
 * PUT /lectures/:lectureId/attachment-units : Updates an existing attachment unit .
 *
 * @param lectureId      the id of the lecture to which the attachment unit belongs to update
 * @param attachmentUnit the attachment unit to update
 * @return the ResponseEntity with status 200 (OK) and with body the updated attachmentUnit
 */
@PutMapping("/lectures/{lectureId}/attachment-units")
@PreAuthorize("hasRole('EDITOR')")
public ResponseEntity<AttachmentUnit> updateAttachmentUnit(@PathVariable Long lectureId, @RequestBody AttachmentUnit attachmentUnit) {
    log.debug("REST request to update an attachment unit : {}", attachmentUnit);
    if (attachmentUnit.getId() == null) {
        throw new BadRequestException();
    }
    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);
    // Make sure that the original references are preserved.
    AttachmentUnit originalAttachmentUnit = attachmentUnitRepository.findById(attachmentUnit.getId()).get();
    attachmentUnit.setAttachment(originalAttachmentUnit.getAttachment());
    AttachmentUnit result = attachmentUnitRepository.save(attachmentUnit);
    return ResponseEntity.ok().headers(HeaderUtil.createEntityUpdateAlert(applicationName, true, ENTITY_NAME, attachmentUnit.getId().toString())).body(result);
}
Also used : ConflictException(de.tum.in.www1.artemis.web.rest.errors.ConflictException) AttachmentUnit(de.tum.in.www1.artemis.domain.lecture.AttachmentUnit) BadRequestException(javax.ws.rs.BadRequestException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 10 with Attachment

use of de.tum.in.www1.artemis.domain.Attachment in project Artemis by ls1intum.

the class ExerciseUnitResource method createExerciseUnit.

/**
 * POST /lectures/:lectureId/exercise-units : creates a new exercise unit.
 *
 * @param lectureId    the id of the lecture to which the attachment unit should be added
 * @param exerciseUnit the exercise unit that should be created
 * @return the ResponseEntity with status 201 (Created) and with body the new exercise unit
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PostMapping("/lectures/{lectureId}/exercise-units")
@PreAuthorize("hasRole('EDITOR')")
public ResponseEntity<ExerciseUnit> createExerciseUnit(@PathVariable Long lectureId, @RequestBody ExerciseUnit exerciseUnit) throws URISyntaxException {
    log.debug("REST request to create ExerciseUnit : {}", exerciseUnit);
    if (exerciseUnit.getId() != null) {
        throw new BadRequestException();
    }
    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);
    // persist lecture unit before lecture to prevent "null index column for collection" error
    exerciseUnit.setLecture(null);
    exerciseUnit = exerciseUnitRepository.saveAndFlush(exerciseUnit);
    exerciseUnit.setLecture(lecture);
    lecture.addLectureUnit(exerciseUnit);
    Lecture updatedLecture = lectureRepository.save(lecture);
    ExerciseUnit persistedExerciseUnit = (ExerciseUnit) updatedLecture.getLectureUnits().get(updatedLecture.getLectureUnits().size() - 1);
    return ResponseEntity.created(new URI("/api/exercise-units/" + persistedExerciseUnit.getId())).headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, "")).body(persistedExerciseUnit);
}
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) BadRequestException(javax.ws.rs.BadRequestException) URI(java.net.URI) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)16 Attachment (de.tum.in.www1.artemis.domain.Attachment)14 Lecture (de.tum.in.www1.artemis.domain.Lecture)12 AttachmentUnit (de.tum.in.www1.artemis.domain.lecture.AttachmentUnit)8 ConflictException (de.tum.in.www1.artemis.web.rest.errors.ConflictException)8 LectureUnit (de.tum.in.www1.artemis.domain.lecture.LectureUnit)6 URI (java.net.URI)6 BadRequestException (javax.ws.rs.BadRequestException)6 Test (org.junit.jupiter.api.Test)6 WithMockUser (org.springframework.security.test.context.support.WithMockUser)6 AbstractSpringIntegrationBambooBitbucketJiraTest (de.tum.in.www1.artemis.AbstractSpringIntegrationBambooBitbucketJiraTest)4 Course (de.tum.in.www1.artemis.domain.Course)4 ExerciseGroup (de.tum.in.www1.artemis.domain.exam.ExerciseGroup)4 ExerciseUnit (de.tum.in.www1.artemis.domain.lecture.ExerciseUnit)4 AnswerPost (de.tum.in.www1.artemis.domain.metis.AnswerPost)4 Post (de.tum.in.www1.artemis.domain.metis.Post)4 BadRequestAlertException (de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 JsonParser.parseString (com.google.gson.JsonParser.parseString)2 User (de.tum.in.www1.artemis.domain.User)2