-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathUpdateTaskContentService.java
More file actions
105 lines (91 loc) · 4.69 KB
/
UpdateTaskContentService.java
File metadata and controls
105 lines (91 loc) · 4.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package clap.server.application.service.task;
import clap.server.adapter.inbound.web.dto.task.request.UpdateTaskLabelRequest;
import clap.server.adapter.inbound.web.dto.task.request.UpdateTaskRequest;
import clap.server.application.mapper.AttachmentMapper;
import clap.server.application.port.inbound.domain.CategoryService;
import clap.server.application.port.inbound.domain.LabelService;
import clap.server.application.port.inbound.domain.MemberService;
import clap.server.application.port.inbound.domain.TaskService;
import clap.server.application.port.inbound.task.UpdateTaskLabelUsecase;
import clap.server.application.port.inbound.task.UpdateTaskUsecase;
import clap.server.application.port.outbound.s3.S3UploadPort;
import clap.server.application.port.outbound.task.CommandAttachmentPort;
import clap.server.application.port.outbound.task.LoadAttachmentPort;
import clap.server.common.annotation.architecture.ApplicationService;
import clap.server.common.constants.FilePathConstants;
import clap.server.domain.model.task.Attachment;
import clap.server.domain.model.task.Category;
import clap.server.domain.model.task.Label;
import clap.server.domain.model.task.Task;
import clap.server.exception.ApplicationException;
import clap.server.exception.code.TaskErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import static clap.server.domain.policy.task.TaskPolicyConstants.TASK_MAX_FILE_COUNT;
@ApplicationService
@RequiredArgsConstructor
@Slf4j
public class UpdateTaskContentService implements UpdateTaskLabelUsecase, UpdateTaskUsecase {
private final CategoryService categoryService;
private final MemberService memberService;
private final LabelService labelService;
private final TaskService taskService;
private final LoadAttachmentPort loadAttachmentPort;
private final CommandAttachmentPort commandAttachmentPort;
private final S3UploadPort s3UploadPort;
@Override
@Transactional
public void updateTask(Long requesterId, Long taskId, UpdateTaskRequest request, List<MultipartFile> files) {
memberService.findActiveMember(requesterId);
Category category = categoryService.findById(request.categoryId());
Task task = taskService.findById(taskId);
int attachmentCount = getAttachmentCount(request, files, task);
if (!request.attachmentsToDelete().isEmpty()) {
deleteAttachments(request, task);
}
if (files != null) {
updateAttachments(files, task);
}
task.updateTask(requesterId, category, request.title(), request.description(), attachmentCount);
taskService.upsert(task);
}
private void deleteAttachments(UpdateTaskRequest request, Task task) {
List<Attachment> attachmentsToDelete = validateAndGetAttachments(request.attachmentsToDelete(), task);
attachmentsToDelete.stream()
.peek(Attachment::softDelete)
.forEach(commandAttachmentPort::save);
}
private void updateAttachments(List<MultipartFile> files, Task task) {
List<String> fileUrls = s3UploadPort.uploadFiles(FilePathConstants.TASK_FILE, files);
List<Attachment> attachments = AttachmentMapper.toTaskAttachments(task, files, fileUrls);
commandAttachmentPort.saveAll(attachments);
}
private static int getAttachmentCount(UpdateTaskRequest request, List<MultipartFile> files, Task task) {
int attachmentToAdd = files == null ? 0 : files.size();
int attachmentCount = task.getAttachmentCount() - request.attachmentsToDelete().size() + attachmentToAdd;
if (attachmentCount > TASK_MAX_FILE_COUNT) {
throw new ApplicationException(TaskErrorCode.FILE_COUNT_EXCEEDED);
}
return attachmentCount;
}
private List<Attachment> validateAndGetAttachments(List<Long> attachmentIdsToDelete, Task task) {
List<Attachment> attachmentsOfTask = loadAttachmentPort.findAllByTaskIdAndAttachmentId(task.getTaskId(), attachmentIdsToDelete);
if (attachmentsOfTask.size() != attachmentIdsToDelete.size()) {
throw new ApplicationException(TaskErrorCode.TASK_ATTACHMENT_NOT_FOUND);
}
return attachmentsOfTask;
}
@Transactional
@Override
public void updateTaskLabel(Long taskId, Long memberId, UpdateTaskLabelRequest request) {
memberService.findActiveMember(memberId);
memberService.findReviewer(memberId);
Task task = taskService.findById(taskId);
Label label = labelService.findById(request.labelId());
task.updateLabel(label);
taskService.upsert(task);
}
}