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