Skip to content

Commit

Permalink
Update Attachment Unit update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
eceeeren committed Jan 1, 2025
1 parent 37d1d0c commit 336fbb7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,18 @@ public ResponseEntity<AttachmentUnit> getAttachmentUnit(@PathVariable Long attac
* @param attachmentUnit the attachment unit with updated content
* @param attachment the attachment with updated content
* @param file the optional file to upload
* @param hiddenPages the pages to be hidden in the attachment unit
* @param studentVersion the student version of the file to upload
* @param keepFilename specifies if the original filename should be kept or not
* @param notificationText the text to be used for the notification. No notification will be sent if the parameter is not set
* @param hiddenPages the pages to be hidden in the attachment unit. No hidden pages will be sent if the parameter is not set
* @return the ResponseEntity with status 200 (OK) and with body the updated attachmentUnit
*/
@PutMapping(value = "lectures/{lectureId}/attachment-units/{attachmentUnitId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@EnforceAtLeastEditor
public ResponseEntity<AttachmentUnit> updateAttachmentUnit(@PathVariable Long lectureId, @PathVariable Long attachmentUnitId, @RequestPart AttachmentUnit attachmentUnit,
@RequestPart Attachment attachment, @RequestPart(required = false) MultipartFile file, @RequestPart(required = false) MultipartFile studentVersion,
@RequestParam(defaultValue = "false") boolean keepFilename, @RequestParam(value = "notificationText", required = false) String notificationText,
@RequestParam(value = "hiddenPages", required = false) String hiddenPages) {
@RequestPart(required = false) String hiddenPages, @RequestParam(defaultValue = "false") boolean keepFilename,
@RequestParam(value = "notificationText", required = false) String notificationText) {
log.debug("REST request to update an attachment unit : {}", attachmentUnit);
AttachmentUnit existingAttachmentUnit = attachmentUnitRepository.findWithSlidesAndCompetenciesByIdElseThrow(attachmentUnitId);
checkAttachmentUnitCourseAndLecture(existingAttachmentUnit, lectureId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ export class AttachmentUnitService {
.pipe(map((res: EntityResponseType) => this.lectureUnitService.convertLectureUnitResponseDatesFromServer(res)));
}

update(lectureId: number, attachmentUnitId: number, formData: FormData, notificationText?: string, hiddenPages?: string): Observable<EntityResponseType> {
update(lectureId: number, attachmentUnitId: number, formData: FormData, notificationText?: string): Observable<EntityResponseType> {
/** Ngsw-worker is bypassed temporarily to fix Chromium file upload issue
* See: https://issues.chromium.org/issues/374550348
**/
return this.httpClient
.put<AttachmentUnit>(
`${this.resourceURL}/lectures/${lectureId}/attachment-units/${attachmentUnitId}?keepFilename=true` +
(notificationText ? `&notificationText=${notificationText}` : '') +
(hiddenPages ? `&hiddenPages=${hiddenPages}` : ''),
(notificationText ? `&notificationText=${notificationText}` : ''),
formData,
{ headers: { 'ngsw-bypass': 'true' }, observe: 'response' },
)
Expand Down
33 changes: 12 additions & 21 deletions src/main/webapp/app/lecture/pdf-preview/pdf-preview.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,30 +180,21 @@ export class PdfPreviewComponent implements OnInit, OnDestroy {

const finalHiddenPages = this.getHiddenPages();

if (finalHiddenPages.length <= 0) {
this.attachmentUnitService.update(this.attachmentUnit()!.lecture!.id!, this.attachmentUnit()!.id!, formData).subscribe({
next: () => {
this.alertService.success('artemisApp.attachment.pdfPreview.attachmentUpdateSuccess');
this.router.navigate(['course-management', this.course()?.id, 'lectures', this.attachmentUnit()!.lecture!.id, 'unit-management']);
},
error: (error) => {
this.alertService.error('artemisApp.attachment.pdfPreview.attachmentUpdateError', { error: error.message });
},
});
} else {
if (finalHiddenPages.length > 0) {
const pdfFileWithHiddenPages = await this.createStudentVersionOfAttachment(finalHiddenPages);
formData.append('studentVersion', pdfFileWithHiddenPages!);

this.attachmentUnitService.update(this.attachmentUnit()!.lecture!.id!, this.attachmentUnit()!.id!, formData, undefined, finalHiddenPages.join(',')).subscribe({
next: () => {
this.alertService.success('artemisApp.attachment.pdfPreview.attachmentUpdateSuccess');
this.router.navigate(['course-management', this.course()?.id, 'lectures', this.attachmentUnit()!.lecture!.id, 'unit-management']);
},
error: (error) => {
this.alertService.error('artemisApp.attachment.pdfPreview.attachmentUpdateError', { error: error.message });
},
});
formData.append('hiddenPages', finalHiddenPages.join(','));
}

this.attachmentUnitService.update(this.attachmentUnit()!.lecture!.id!, this.attachmentUnit()!.id!, formData).subscribe({
next: () => {
this.alertService.success('artemisApp.attachment.pdfPreview.attachmentUpdateSuccess');
this.router.navigate(['course-management', this.course()?.id, 'lectures', this.attachmentUnit()!.lecture!.id, 'unit-management']);
},
error: (error) => {
this.alertService.error('artemisApp.attachment.pdfPreview.attachmentUpdateError', { error: error.message });
},
});
}
}

Expand Down

0 comments on commit 336fbb7

Please sign in to comment.