-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInquiryAttachmentService.java
More file actions
73 lines (60 loc) · 2.69 KB
/
Copy pathInquiryAttachmentService.java
File metadata and controls
73 lines (60 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package life.mosu.mosuserver.application.inquiry;
import java.util.List;
import life.mosu.mosuserver.domain.inquiry.entity.InquiryAttachmentJpaEntity;
import life.mosu.mosuserver.domain.inquiry.entity.InquiryJpaEntity;
import life.mosu.mosuserver.domain.inquiry.repository.InquiryAttachmentJpaRepository;
import life.mosu.mosuserver.infra.persistence.s3.AttachmentService;
import life.mosu.mosuserver.infra.persistence.s3.FileUploadHelper;
import life.mosu.mosuserver.infra.persistence.s3.S3Service;
import life.mosu.mosuserver.presentation.common.FileRequest;
import life.mosu.mosuserver.presentation.inquiry.dto.InquiryDetailResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class InquiryAttachmentService implements AttachmentService<InquiryJpaEntity, FileRequest> {
private final InquiryAttachmentJpaRepository inquiryAttachmentJpaRepository;
private final FileUploadHelper fileUploadHelper;
private final S3Service s3Service;
@Override
public void createAttachment(List<FileRequest> requests, InquiryJpaEntity inquiryEntity) {
fileUploadHelper.saveAttachments(
requests,
inquiryEntity.getId(),
inquiryAttachmentJpaRepository,
(req, id) -> req.toInquiryAttachmentEntity(inquiryEntity.getId()),
FileRequest::s3Key
);
}
@Override
public void deleteAttachment(InquiryJpaEntity entity) {
List<InquiryAttachmentJpaEntity> attachments = inquiryAttachmentJpaRepository.findAllByInquiryId(
entity.getId());
inquiryAttachmentJpaRepository.deleteAll(attachments);
}
@Override
public void updateAttachment(
List<FileRequest> requests,
InquiryJpaEntity inquiryEntity
) {
deleteAttachment(inquiryEntity);
createAttachment(requests, inquiryEntity);
}
public List<InquiryDetailResponse.AttachmentDetailResponse> toAttachmentResponses(
InquiryJpaEntity inquiry) {
List<InquiryAttachmentJpaEntity> attachments = inquiryAttachmentJpaRepository.findAllByInquiryId(
inquiry.getId());
return attachments.stream()
.map(this::createAttachDetailResponse)
.toList();
}
private InquiryDetailResponse.AttachmentDetailResponse createAttachDetailResponse(
InquiryAttachmentJpaEntity attachment) {
String publicUrl = s3Service.getPublicUrl(attachment.getS3Key());
return new InquiryDetailResponse.AttachmentDetailResponse(
attachment.getFileName(),
publicUrl,
attachment.getS3Key()
);
}
}