Skip to content

Commit

Permalink
[feature/#251] 첨부파일 개편으로 인한 budget 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
whitem4rk committed Mar 21, 2024
1 parent 5de6cdc commit 22e3e60
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 138 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.inhabas.api.domain.budget.dto;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
Expand Down Expand Up @@ -37,14 +39,22 @@ public class BudgetApplicationRegisterForm {

@NotBlank private String account;

private List<String> files = new ArrayList<>();

@Builder
public BudgetApplicationRegisterForm(
String title, LocalDateTime dateUsed, String details, Integer outcome, String account) {
String title,
LocalDateTime dateUsed,
String details,
Integer outcome,
String account,
List<String> files) {
this.title = title;
this.dateUsed = dateUsed;
this.details = details;
this.outcome = outcome;
this.account = account;
this.files = files == null ? new ArrayList<>() : files;
}

public BudgetSupportApplication toEntity(Menu menu, Member applicant) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.inhabas.api.domain.budget.dto;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
Expand Down Expand Up @@ -33,13 +35,15 @@ public class BudgetHistoryCreateForm {

@NotBlank private String details;

@NotNull private String memberStudentIdReceived;
private String memberStudentIdReceived;

@NotBlank private String memberNameReceived;
private String memberNameReceived;

@PositiveOrZero @NotNull private Integer income;
@PositiveOrZero private Integer income;

@PositiveOrZero @NotNull private Integer outcome;
@PositiveOrZero private Integer outcome;

@NotNull private List<String> files = new ArrayList<>();

private static final Integer ZERO = 0;

Expand All @@ -51,15 +55,16 @@ public BudgetHistoryCreateForm(
String memberStudentIdReceived,
String memberNameReceived,
Integer income,
Integer outcome) {
Integer outcome,
List<String> files) {
this.dateUsed = dateUsed;
this.title = title;
this.details = details;
this.memberStudentIdReceived = memberStudentIdReceived;
this.memberNameReceived = memberNameReceived;
this.income = income;
this.outcome = outcome;

this.files = files == null ? new ArrayList<>() : files;
if (this.details.isBlank()) {
this.details = this.title;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,16 @@

import java.util.List;

import org.springframework.web.multipart.MultipartFile;

import com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.RequestStatus;
import com.inhabas.api.domain.budget.dto.BudgetApplicationDetailDto;
import com.inhabas.api.domain.budget.dto.BudgetApplicationDto;
import com.inhabas.api.domain.budget.dto.BudgetApplicationRegisterForm;

public interface BudgetApplicationService {

Long registerApplication(
BudgetApplicationRegisterForm form, List<MultipartFile> files, Long memberId);
Long registerApplication(BudgetApplicationRegisterForm form, Long memberId);

void updateApplication(
Long applicationId,
BudgetApplicationRegisterForm form,
List<MultipartFile> files,
Long memberId);
void updateApplication(Long applicationId, BudgetApplicationRegisterForm form, Long memberId);

void deleteApplication(Long applicationId, Long memberId);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
package com.inhabas.api.domain.budget.usecase;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.inhabas.api.auth.domain.error.businessException.NotFoundException;
import com.inhabas.api.auth.domain.oauth2.member.domain.entity.Member;
import com.inhabas.api.auth.domain.oauth2.member.domain.exception.MemberNotFoundException;
import com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.RequestStatus;
import com.inhabas.api.auth.domain.oauth2.member.repository.MemberRepository;
import com.inhabas.api.domain.board.exception.OnlyWriterModifiableException;
import com.inhabas.api.domain.board.exception.S3UploadFailedException;
import com.inhabas.api.domain.budget.domain.BudgetSupportApplication;
import com.inhabas.api.domain.budget.dto.BudgetApplicationDetailDto;
import com.inhabas.api.domain.budget.dto.BudgetApplicationDto;
import com.inhabas.api.domain.budget.dto.BudgetApplicationRegisterForm;
import com.inhabas.api.domain.budget.exception.InProcessModifiableException;
import com.inhabas.api.domain.budget.repository.BudgetApplicationRepository;
import com.inhabas.api.domain.file.domain.BoardFile;
import com.inhabas.api.domain.file.usecase.S3Service;
import com.inhabas.api.domain.file.repository.BoardFileRepository;
import com.inhabas.api.domain.menu.domain.Menu;
import com.inhabas.api.domain.menu.repository.MenuRepository;
import com.inhabas.api.global.util.ClassifiedFiles;
import com.inhabas.api.global.util.ClassifyFiles;
import com.inhabas.api.global.util.FileUtil;

@Service
@RequiredArgsConstructor
Expand All @@ -38,7 +33,7 @@ public class BudgetApplicationServiceImpl implements BudgetApplicationService {
private final BudgetApplicationRepository budgetApplicationRepository;
private final MemberRepository memberRepository;
private final MenuRepository menuRepository;
private final S3Service s3Service;
private final BoardFileRepository boardFileRepository;
private static final Integer BUDGET_APPLICATION_MENU_ID = 14;
private static final String DIR_NAME = "budget/";

Expand Down Expand Up @@ -76,25 +71,30 @@ public BudgetApplicationDetailDto getApplicationDetails(Long applicationId) {

@Transactional
@Override
public Long registerApplication(
BudgetApplicationRegisterForm form, List<MultipartFile> files, Long memberId) {
public Long registerApplication(BudgetApplicationRegisterForm form, Long memberId) {
Member applicant =
memberRepository.findById(memberId).orElseThrow(MemberNotFoundException::new);
Menu menu =
menuRepository.findById(BUDGET_APPLICATION_MENU_ID).orElseThrow(NotFoundException::new);
BudgetSupportApplication application =
form.toEntity(menu, applicant).writtenBy(applicant, BudgetSupportApplication.class);

return updateBudgetFiles(files, application);
List<String> fileIdList = form.getFiles();
List<BoardFile> boardFileList =
boardFileRepository.getAllByIdInAndUploader(fileIdList, applicant);
application.updateFiles(boardFileList);

for (BoardFile file : boardFileList) {
file.toBoard(application);
}
application.updateFiles(boardFileList);
return budgetApplicationRepository.save(application).getId();
}

@Transactional
@Override
public void updateApplication(
Long applicationId,
BudgetApplicationRegisterForm form,
List<MultipartFile> files,
Long memberId) {
Long applicationId, BudgetApplicationRegisterForm form, Long memberId) {
Member applicant =
memberRepository.findById(memberId).orElseThrow(MemberNotFoundException::new);
BudgetSupportApplication application =
Expand All @@ -111,7 +111,14 @@ public void updateApplication(
form.getOutcome(),
form.getAccount());

updateBudgetFiles(files, application);
List<String> fileIdList = form.getFiles();
List<BoardFile> boardFileList =
boardFileRepository.getAllByIdInAndUploader(fileIdList, applicant);
application.updateFiles(boardFileList);

for (BoardFile file : boardFileList) {
file.toBoard(application);
}
}

@Transactional
Expand All @@ -126,37 +133,4 @@ public void deleteApplication(Long applicationId, Long memberId) {

budgetApplicationRepository.deleteById(applicationId);
}

private Long updateBudgetFiles(List<MultipartFile> files, BudgetSupportApplication application) {
List<BoardFile> updateReceipts = new ArrayList<>();
List<String> urlListForDelete = new ArrayList<>();

if (files != null) {
try {
updateReceipts =
files.stream()
.map(
file -> {
String path = FileUtil.generateFileName(file, DIR_NAME);
String url = s3Service.uploadS3Image(file, path);
urlListForDelete.add(url);
return BoardFile.builder()
.name(file.getOriginalFilename())
.url(url)
.board(application)
.build();
})
.collect(Collectors.toList());

} catch (RuntimeException e) {
for (String url : urlListForDelete) {
s3Service.deleteS3File(url);
}
throw new S3UploadFailedException();
}
}

application.updateFiles(updateReceipts);
return budgetApplicationRepository.save(application).getId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@

import java.util.List;

import org.springframework.web.multipart.MultipartFile;

import com.inhabas.api.domain.budget.dto.BudgetHistoryCreateForm;
import com.inhabas.api.domain.budget.dto.BudgetHistoryDetailDto;
import com.inhabas.api.domain.budget.dto.BudgetHistoryDto;

public interface BudgetHistoryService {

Long createHistory(BudgetHistoryCreateForm form, List<MultipartFile> files, Long secretaryId);
Long createHistory(BudgetHistoryCreateForm form, Long secretaryId);

void modifyHistory(
Long historyId, BudgetHistoryCreateForm form, List<MultipartFile> files, Long secretaryId);
void modifyHistory(Long historyId, BudgetHistoryCreateForm form, Long secretaryId);

void deleteHistory(Long historyId, Long secretaryId);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
package com.inhabas.api.domain.budget.usecase;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.inhabas.api.auth.domain.error.businessException.NotFoundException;
import com.inhabas.api.auth.domain.oauth2.member.domain.entity.Member;
import com.inhabas.api.auth.domain.oauth2.member.domain.exception.MemberNotFoundException;
import com.inhabas.api.auth.domain.oauth2.member.repository.MemberRepository;
import com.inhabas.api.domain.board.exception.OnlyWriterUpdateException;
import com.inhabas.api.domain.board.exception.S3UploadFailedException;
import com.inhabas.api.domain.budget.domain.BudgetHistory;
import com.inhabas.api.domain.budget.dto.BudgetHistoryCreateForm;
import com.inhabas.api.domain.budget.dto.BudgetHistoryDetailDto;
import com.inhabas.api.domain.budget.dto.BudgetHistoryDto;
import com.inhabas.api.domain.budget.repository.BudgetHistoryRepository;
import com.inhabas.api.domain.file.domain.BoardFile;
import com.inhabas.api.domain.file.usecase.S3Service;
import com.inhabas.api.domain.file.repository.BoardFileRepository;
import com.inhabas.api.domain.menu.domain.Menu;
import com.inhabas.api.domain.menu.repository.MenuRepository;
import com.inhabas.api.global.util.ClassifiedFiles;
import com.inhabas.api.global.util.ClassifyFiles;
import com.inhabas.api.global.util.FileUtil;

@Service
@RequiredArgsConstructor
Expand All @@ -36,14 +31,12 @@ public class BudgetHistoryServiceImpl implements BudgetHistoryService {
private final BudgetHistoryRepository budgetHistoryRepository;
private final MemberRepository memberRepository;
private final MenuRepository menuRepository;
private final S3Service s3Service;
private final BoardFileRepository boardFileRepository;
private static final Integer BUDGET_HISTORY_MENU_ID = 15;
private static final String DIR_NAME = "budget/";

@Override
@Transactional
public Long createHistory(
BudgetHistoryCreateForm form, List<MultipartFile> files, Long secretaryId) {
public Long createHistory(BudgetHistoryCreateForm form, Long secretaryId) {

Member secretary =
memberRepository.findById(secretaryId).orElseThrow(MemberNotFoundException::new);
Expand All @@ -57,13 +50,20 @@ public Long createHistory(
BudgetHistory newHistory =
form.toEntity(menu, secretary, memberReceived).writtenBy(secretary, BudgetHistory.class);

return updateBudgetFiles(files, newHistory);
List<String> fileIdList = form.getFiles();
List<BoardFile> boardFileList =
boardFileRepository.getAllByIdInAndUploader(fileIdList, secretary);
newHistory.updateFiles(boardFileList);

for (BoardFile file : boardFileList) {
file.toBoard(newHistory);
}
return budgetHistoryRepository.save(newHistory).getId();
}

@Override
@Transactional
public void modifyHistory(
Long historyId, BudgetHistoryCreateForm form, List<MultipartFile> files, Long secretaryId) {
public void modifyHistory(Long historyId, BudgetHistoryCreateForm form, Long secretaryId) {
Member secretary =
memberRepository.findById(secretaryId).orElseThrow(MemberNotFoundException::new);
Member memberReceived =
Expand All @@ -84,7 +84,14 @@ public void modifyHistory(
form.getDetails(),
memberReceived);

updateBudgetFiles(files, budgetHistory);
List<String> fileIdList = form.getFiles();
List<BoardFile> boardFileList =
boardFileRepository.getAllByIdInAndUploader(fileIdList, secretary);
budgetHistory.updateFiles(boardFileList);

for (BoardFile file : boardFileList) {
file.toBoard(budgetHistory);
}
}

@Override
Expand Down Expand Up @@ -147,37 +154,4 @@ public List<Integer> getAllYearOfHistory() {
public Integer getBalance() {
return budgetHistoryRepository.getBalance();
}

private Long updateBudgetFiles(List<MultipartFile> files, BudgetHistory budgetHistory) {
List<BoardFile> updateFiles = new ArrayList<>();
List<String> urlListForDelete = new ArrayList<>();

if (files != null) {
try {
updateFiles =
files.stream()
.map(
file -> {
String path = FileUtil.generateFileName(file, DIR_NAME);
String url = s3Service.uploadS3Image(file, path);
urlListForDelete.add(url);
return BoardFile.builder()
.name(file.getOriginalFilename())
.url(url)
.board(budgetHistory)
.build();
})
.collect(Collectors.toList());

} catch (RuntimeException e) {
for (String url : urlListForDelete) {
s3Service.deleteS3File(url);
}
throw new S3UploadFailedException();
}
}

budgetHistory.updateFiles(updateFiles);
return budgetHistoryRepository.save(budgetHistory).getId();
}
}
Loading

0 comments on commit 22e3e60

Please sign in to comment.