Skip to content

Commit e5b9792

Browse files
이소정이소정
authored andcommitted
✨ feat: 보고서 작성 기능 첨부파일 추가
1 parent b849e17 commit e5b9792

File tree

5 files changed

+57
-6
lines changed

5 files changed

+57
-6
lines changed

src/main/java/com/sponus/sponusbe/domain/report/controller/ReportController.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,14 @@ public class ReportController {
3535
public ApiResponse<ReportCreateResponse> createReport(
3636
@AuthOrganization Organization authOrganization,
3737
@RequestPart("request") @Valid ReportCreateRequest request,
38-
@RequestPart(value = "images") List<MultipartFile> images) {
39-
return ApiResponse.onSuccess(reportService.createReport(authOrganization, request, images));
38+
@RequestPart(value = "images") List<MultipartFile> images,
39+
@RequestPart(value = "attachments") List<MultipartFile> attachments) {
40+
return ApiResponse.onSuccess(
41+
reportService.createReport(
42+
authOrganization,
43+
request,
44+
images,
45+
attachments));
4046
}
4147

4248
@PatchMapping("/{reportId}")

src/main/java/com/sponus/sponusbe/domain/report/dto/ReportAttachmentResponse.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.sponus.sponusbe.domain.report.dto;
22

3+
import com.sponus.sponusbe.domain.report.entity.ReportAttachment;
4+
import com.sponus.sponusbe.domain.report.entity.ReportImage;
5+
36
import lombok.Builder;
47

58
@Builder
@@ -8,5 +11,13 @@ public record ReportAttachmentResponse(
811
String name,
912
String url
1013
) {
14+
15+
public static ReportAttachmentResponse from(ReportAttachment attachment) {
16+
return ReportAttachmentResponse.builder()
17+
.id(attachment.getId())
18+
.name(attachment.getName())
19+
.url(attachment.getUrl())
20+
.build();
21+
}
1122
}
1223

src/main/java/com/sponus/sponusbe/domain/report/entity/Report.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.hibernate.annotations.DynamicUpdate;
88

99
import com.sponus.sponusbe.domain.organization.entity.Organization;
10+
import com.sponus.sponusbe.domain.propose.entity.ProposeAttachment;
1011

1112
import jakarta.persistence.CascadeType;
1213
import jakarta.persistence.Column;
@@ -48,9 +49,6 @@ public class Report {
4849
@Column(name = "report_content", nullable = false)
4950
private String content;
5051

51-
@OneToMany(mappedBy = "report")
52-
private List<ReportAttachment> reportAttachments;
53-
5452
@ManyToOne(fetch = FetchType.LAZY)
5553
@JoinColumn(name = "organization_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
5654
private Organization writer;
@@ -59,6 +57,10 @@ public class Report {
5957
@OneToMany(mappedBy = "report", cascade = CascadeType.ALL, orphanRemoval = true)
6058
private List<ReportImage> reportImages = new ArrayList<>();
6159

60+
@Builder.Default
61+
@OneToMany(mappedBy = "report", cascade = CascadeType.ALL)
62+
private List<ReportAttachment> reportAttachments = new ArrayList<>();
63+
6264
public void update(String title, String content) {
6365
this.title = title == null ? this.title : title;
6466
this.content = content == null ? this.content : content;
@@ -70,4 +72,11 @@ public void updateImages(List<ReportImage> images) {
7072
this.reportImages.addAll(images);
7173
}
7274
}
75+
76+
public void updateAttachments(List<ReportAttachment> attachments) {
77+
if (attachments != null) {
78+
this.reportAttachments.clear();
79+
this.reportAttachments.addAll(attachments);
80+
}
81+
}
7382
}

src/main/java/com/sponus/sponusbe/domain/report/entity/ReportAttachment.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,12 @@ public class ReportAttachment {
3939
@ManyToOne(fetch = FetchType.LAZY, optional = false)
4040
@JoinColumn(name = "report_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
4141
private Report report;
42+
43+
public void setReport(Report report) {
44+
if (this.report != null) {
45+
this.report.getReportAttachments().remove(this);
46+
}
47+
this.report = report;
48+
report.getReportAttachments().add(this);
49+
}
4250
}

src/main/java/com/sponus/sponusbe/domain/report/service/ReportService.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.sponus.sponusbe.domain.report.dto.ReportCreateRequest;
1010
import com.sponus.sponusbe.domain.report.dto.ReportCreateResponse;
1111
import com.sponus.sponusbe.domain.report.entity.Report;
12+
import com.sponus.sponusbe.domain.report.entity.ReportAttachment;
1213
import com.sponus.sponusbe.domain.report.entity.ReportImage;
1314
import com.sponus.sponusbe.domain.report.exception.ReportErrorCode;
1415
import com.sponus.sponusbe.domain.report.exception.ReportException;
@@ -28,13 +29,17 @@ public class ReportService {
2829
public ReportCreateResponse createReport(
2930
Organization authOrganization,
3031
ReportCreateRequest request,
31-
List<MultipartFile> images
32+
List<MultipartFile> images,
33+
List<MultipartFile> attachments
3234
) {
3335
final Report report = request.toEntity(authOrganization);
3436
setReportImages(images, report);
37+
setReportAttachments(attachments, report);
3538
return ReportCreateResponse.from(reportRepository.save(report));
3639
}
3740

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

65+
private void setReportAttachments(List<MultipartFile> attachments, Report report) {
66+
report.getReportAttachments().clear();
67+
attachments.forEach(attachment -> {
68+
final String url = s3Service.uploadFile(attachment);
69+
ReportAttachment reportAttachment = ReportAttachment.builder()
70+
.name(attachment.getOriginalFilename())
71+
.url(url)
72+
.build();
73+
reportAttachment.setReport(report);
74+
});
75+
}
76+
6077
}
6178

0 commit comments

Comments
 (0)