Skip to content

Commit

Permalink
✨ feat: 보고서 작성 기능 첨부파일 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
이소정 authored and 이소정 committed Feb 6, 2024
1 parent b849e17 commit e5b9792
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ public class ReportController {
public ApiResponse<ReportCreateResponse> createReport(
@AuthOrganization Organization authOrganization,
@RequestPart("request") @Valid ReportCreateRequest request,
@RequestPart(value = "images") List<MultipartFile> images) {
return ApiResponse.onSuccess(reportService.createReport(authOrganization, request, images));
@RequestPart(value = "images") List<MultipartFile> images,
@RequestPart(value = "attachments") List<MultipartFile> attachments) {
return ApiResponse.onSuccess(
reportService.createReport(
authOrganization,
request,
images,
attachments));
}

@PatchMapping("/{reportId}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.sponus.sponusbe.domain.report.dto;

import com.sponus.sponusbe.domain.report.entity.ReportAttachment;
import com.sponus.sponusbe.domain.report.entity.ReportImage;

import lombok.Builder;

@Builder
Expand All @@ -8,5 +11,13 @@ public record ReportAttachmentResponse(
String name,
String url
) {

public static ReportAttachmentResponse from(ReportAttachment attachment) {
return ReportAttachmentResponse.builder()
.id(attachment.getId())
.name(attachment.getName())
.url(attachment.getUrl())
.build();
}
}

15 changes: 12 additions & 3 deletions src/main/java/com/sponus/sponusbe/domain/report/entity/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.hibernate.annotations.DynamicUpdate;

import com.sponus.sponusbe.domain.organization.entity.Organization;
import com.sponus.sponusbe.domain.propose.entity.ProposeAttachment;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
Expand Down Expand Up @@ -48,9 +49,6 @@ public class Report {
@Column(name = "report_content", nullable = false)
private String content;

@OneToMany(mappedBy = "report")
private List<ReportAttachment> reportAttachments;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "organization_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private Organization writer;
Expand All @@ -59,6 +57,10 @@ public class Report {
@OneToMany(mappedBy = "report", cascade = CascadeType.ALL, orphanRemoval = true)
private List<ReportImage> reportImages = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "report", cascade = CascadeType.ALL)
private List<ReportAttachment> reportAttachments = new ArrayList<>();

public void update(String title, String content) {
this.title = title == null ? this.title : title;
this.content = content == null ? this.content : content;
Expand All @@ -70,4 +72,11 @@ public void updateImages(List<ReportImage> images) {
this.reportImages.addAll(images);
}
}

public void updateAttachments(List<ReportAttachment> attachments) {
if (attachments != null) {
this.reportAttachments.clear();
this.reportAttachments.addAll(attachments);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,12 @@ public class ReportAttachment {
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "report_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private Report report;

public void setReport(Report report) {
if (this.report != null) {
this.report.getReportAttachments().remove(this);
}
this.report = report;
report.getReportAttachments().add(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.sponus.sponusbe.domain.report.dto.ReportCreateRequest;
import com.sponus.sponusbe.domain.report.dto.ReportCreateResponse;
import com.sponus.sponusbe.domain.report.entity.Report;
import com.sponus.sponusbe.domain.report.entity.ReportAttachment;
import com.sponus.sponusbe.domain.report.entity.ReportImage;
import com.sponus.sponusbe.domain.report.exception.ReportErrorCode;
import com.sponus.sponusbe.domain.report.exception.ReportException;
Expand All @@ -28,13 +29,17 @@ public class ReportService {
public ReportCreateResponse createReport(
Organization authOrganization,
ReportCreateRequest request,
List<MultipartFile> images
List<MultipartFile> images,
List<MultipartFile> attachments
) {
final Report report = request.toEntity(authOrganization);
setReportImages(images, report);
setReportAttachments(attachments, report);
return ReportCreateResponse.from(reportRepository.save(report));
}



public ReportCreateResponse updateReport(Long reportId, ReportCreateRequest request) {
final Report report = reportRepository.findById(reportId)
.orElseThrow(() -> new ReportException(ReportErrorCode.REPORT_NOT_FOUND));
Expand All @@ -57,5 +62,17 @@ private void setReportImages(List<MultipartFile> images, Report report) {
});
}

private void setReportAttachments(List<MultipartFile> attachments, Report report) {
report.getReportAttachments().clear();
attachments.forEach(attachment -> {
final String url = s3Service.uploadFile(attachment);
ReportAttachment reportAttachment = ReportAttachment.builder()
.name(attachment.getOriginalFilename())
.url(url)
.build();
reportAttachment.setReport(report);
});
}

}

0 comments on commit e5b9792

Please sign in to comment.