Search in sources :

Example 6 with AttachmentUnit

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

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

the class FileIntegrationTest method createLectureWithLectureUnits.

public Lecture createLectureWithLectureUnits(HttpStatus expectedStatus) throws Exception {
    Lecture lecture = database.createCourseWithLecture(true);
    lecture.setTitle("Test title");
    lecture.setDescription("Test");
    lecture.setStartDate(ZonedDateTime.now().minusHours(1));
    lectureRepo.save(lecture);
    Long lectureId = lecture.getId();
    // create pdf file 1
    AttachmentUnit unit1;
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PDDocument doc1 = new PDDocument()) {
        doc1.addPage(new PDPage());
        doc1.addPage(new PDPage());
        doc1.addPage(new PDPage());
        doc1.save(outputStream);
        MockMultipartFile file1 = new MockMultipartFile("file", "file.pdf", "application/json", outputStream.toByteArray());
        unit1 = uploadAttachmentUnit(file1, lectureId, expectedStatus);
    }
    // create image file
    MockMultipartFile file2 = new MockMultipartFile("file", "filename2.png", "application/json", "some text".getBytes());
    AttachmentUnit unit2 = uploadAttachmentUnit(file2, lectureId, expectedStatus);
    // create pdf file 3
    AttachmentUnit unit3;
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PDDocument doc2 = new PDDocument()) {
        doc2.addPage(new PDPage());
        doc2.addPage(new PDPage());
        doc2.save(outputStream);
        MockMultipartFile file3 = new MockMultipartFile("file", "filename3.pdf", "application/json", outputStream.toByteArray());
        unit3 = uploadAttachmentUnit(file3, lectureId, expectedStatus);
    }
    lecture = database.addLectureUnitsToLecture(lecture, Set.of(unit1, unit2, unit3));
    return lecture;
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) PDPage(org.apache.pdfbox.pdmodel.PDPage) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) AttachmentUnit(de.tum.in.www1.artemis.domain.lecture.AttachmentUnit) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 8 with AttachmentUnit

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

the class AttachmentUnitResource method createAttachmentUnit.

/**
 * POST /lectures/:lectureId/attachment-units : creates a new attachment unit.
 *
 * @param lectureId      the id of the lecture to which the attachment unit should be added
 * @param attachmentUnit the attachment unit that should be created
 * @return the ResponseEntity with status 201 (Created) and with body the new attachment unit
 * @throws URISyntaxException if the Location URI syntax is incorrect
 */
@PostMapping("/lectures/{lectureId}/attachment-units")
@PreAuthorize("hasRole('EDITOR')")
public ResponseEntity<AttachmentUnit> createAttachmentUnit(@PathVariable Long lectureId, @RequestBody AttachmentUnit attachmentUnit) throws URISyntaxException {
    log.debug("REST request to create AttachmentUnit : {}", attachmentUnit);
    if (attachmentUnit.getId() != null) {
        throw new BadRequestException();
    }
    Lecture lecture = lectureRepository.findByIdWithLectureUnitsElseThrow(lectureId);
    if (lecture.getCourse() == null) {
        throw new ConflictException("Specified lecture is not part of a course", "AttachmentUnit", "courseMissing");
    }
    authorizationCheckService.checkHasAtLeastRoleInCourseElseThrow(Role.EDITOR, lecture.getCourse(), null);
    // persist lecture unit before lecture to prevent "null index column for collection" error
    attachmentUnit.setLecture(null);
    attachmentUnit = attachmentUnitRepository.saveAndFlush(attachmentUnit);
    attachmentUnit.setLecture(lecture);
    lecture.addLectureUnit(attachmentUnit);
    lectureRepository.save(lecture);
    // cleanup before sending to client
    attachmentUnit.getLecture().setLectureUnits(null);
    attachmentUnit.getLecture().setAttachments(null);
    attachmentUnit.getLecture().setPosts(null);
    return ResponseEntity.created(new URI("/api/attachment-units/" + attachmentUnit.getId())).headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, "")).body(attachmentUnit);
}
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) URI(java.net.URI) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 9 with AttachmentUnit

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

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

the class AttachmentUnitIntegrationTest method updateAttachmentUnit_asInstructor_shouldKeepOrdering.

@Test
@WithMockUser(username = "instructor1", roles = "INSTRUCTOR")
public void updateAttachmentUnit_asInstructor_shouldKeepOrdering() throws Exception {
    persistAttachmentUnitWithLecture();
    // Add a second lecture unit
    AttachmentUnit attachmentUnit = database.createAttachmentUnit(false);
    lecture1.addLectureUnit(attachmentUnit);
    lecture1 = lectureRepository.save(lecture1);
    List<LectureUnit> orderedUnits = lecture1.getLectureUnits();
    // Updating the lecture unit should not influence order
    request.putWithResponseBody("/api/lectures/" + lecture1.getId() + "/attachment-units", attachmentUnit, AttachmentUnit.class, HttpStatus.OK);
    List<LectureUnit> updatedOrderedUnits = lectureRepository.findByIdWithLectureUnits(lecture1.getId()).get().getLectureUnits();
    assertThat(updatedOrderedUnits).containsExactlyElementsOf(orderedUnits);
}
Also used : LectureUnit(de.tum.in.www1.artemis.domain.lecture.LectureUnit) AttachmentUnit(de.tum.in.www1.artemis.domain.lecture.AttachmentUnit) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.jupiter.api.Test)

Aggregations

AttachmentUnit (de.tum.in.www1.artemis.domain.lecture.AttachmentUnit)16 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)10 ConflictException (de.tum.in.www1.artemis.web.rest.errors.ConflictException)8 LectureUnit (de.tum.in.www1.artemis.domain.lecture.LectureUnit)6 Lecture (de.tum.in.www1.artemis.domain.Lecture)4 BadRequestException (javax.ws.rs.BadRequestException)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Attachment (de.tum.in.www1.artemis.domain.Attachment)2 ExerciseUnit (de.tum.in.www1.artemis.domain.lecture.ExerciseUnit)2 EntityNotFoundException (de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 URI (java.net.URI)2 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)2 PDPage (org.apache.pdfbox.pdmodel.PDPage)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Test (org.junit.jupiter.api.Test)2 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)2 WithMockUser (org.springframework.security.test.context.support.WithMockUser)2