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