Skip to content

Commit

Permalink
Merge pull request #73 from Leets-Official/feat/#70/post,notice-파일-업로…
Browse files Browse the repository at this point in the history
…드-수정

Feat #73 post,notice, receipt 파일 업로드 수정
  • Loading branch information
hyxklee authored Nov 24, 2024
2 parents 327a913 + e600e46 commit 9e56411
Show file tree
Hide file tree
Showing 35 changed files with 339 additions and 127 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package leets.weeth.domain.account.application.dto;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import leets.weeth.domain.file.application.dto.request.FileSaveRequest;
import leets.weeth.domain.file.application.dto.response.FileResponse;

import java.time.LocalDate;
import java.util.List;
Expand All @@ -12,13 +15,14 @@ public record Response(
String description,
Integer amount,
LocalDate date,
List<String> images
List<FileResponse> fileUrls
) {}

public record Save(
String description,
@NotNull Integer amount,
@NotNull LocalDate date,
@NotNull Integer cardinal
@NotNull Integer cardinal,
@Valid List<@NotNull FileSaveRequest> files
) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import leets.weeth.domain.account.application.dto.ReceiptDTO;
import leets.weeth.domain.account.domain.entity.Account;
import leets.weeth.domain.account.domain.entity.Receipt;
import leets.weeth.domain.file.application.dto.response.FileResponse;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;
Expand All @@ -15,8 +16,10 @@ public interface ReceiptMapper {

List<ReceiptDTO.Response> to(List<Receipt> account);

ReceiptDTO.Response to(Receipt receipt, List<FileResponse> fileUrls);

@Mapping(target = "id", ignore = true)
@Mapping(target = "description", source = "dto.description")
@Mapping(target = "account", source = "account")
Receipt from(ReceiptDTO.Save dto, List<String> images, Account account);
Receipt from(ReceiptDTO.Save dto, Account account);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

import leets.weeth.domain.account.application.dto.AccountDTO;
import leets.weeth.domain.account.application.dto.ReceiptDTO;
import leets.weeth.domain.account.application.exception.AccountExistsException;
import leets.weeth.domain.account.application.mapper.AccountMapper;
import leets.weeth.domain.account.application.mapper.ReceiptMapper;
import leets.weeth.domain.account.domain.entity.Account;
import leets.weeth.domain.account.domain.entity.Receipt;
import leets.weeth.domain.account.domain.service.AccountGetService;
import leets.weeth.domain.account.domain.service.AccountSaveService;
import leets.weeth.domain.account.application.exception.AccountExistsException;
import leets.weeth.domain.file.application.dto.response.FileResponse;
import leets.weeth.domain.file.application.mapper.FileMapper;
import leets.weeth.domain.file.domain.service.FileGetService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -19,14 +23,20 @@ public class AccountUseCaseImpl implements AccountUseCase {

private final AccountGetService accountGetService;
private final AccountSaveService accountSaveService;
private final FileGetService fileGetService;
private final AccountMapper accountMapper;
private final ReceiptMapper receiptMapper;
private final FileMapper fileMapper;

@Override
public AccountDTO.Response find(Integer cardinal) {
Account account = accountGetService.find(cardinal);
List<ReceiptDTO.Response> receipts = receiptMapper.to(account.getReceipts());
return accountMapper.to(account, receipts);
List<Receipt> receipts = account.getReceipts();
List<ReceiptDTO.Response> response = receipts.stream()
.map(receipt -> receiptMapper.to(receipt, getFiles(receipt.getId())))
.toList();

return accountMapper.to(account, response);
}

@Override
Expand All @@ -36,7 +46,13 @@ public void save(AccountDTO.Save dto) {
}

private void validate(AccountDTO.Save dto) {
if(accountGetService.validate(dto.cardinal()))
if (accountGetService.validate(dto.cardinal()))
throw new AccountExistsException();
}

private List<FileResponse> getFiles(Long receiptId) {
return fileGetService.findAllByReceipt(receiptId).stream()
.map(fileMapper::toFileResponse)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.List;

public interface ReceiptUseCase {
void save(ReceiptDTO.Save dto, List<MultipartFile> images);
void save(ReceiptDTO.Save dto);

void delete(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
import leets.weeth.domain.account.domain.service.ReceiptDeleteService;
import leets.weeth.domain.account.domain.service.ReceiptGetService;
import leets.weeth.domain.account.domain.service.ReceiptSaveService;
import leets.weeth.domain.file.application.mapper.FileMapper;
import leets.weeth.domain.file.domain.entity.File;
import leets.weeth.domain.file.domain.service.FileDeleteService;
import leets.weeth.domain.file.domain.service.FileGetService;
import leets.weeth.domain.file.domain.service.FileSaveService;
import leets.weeth.domain.file.service.S3Service;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -20,25 +25,39 @@
@RequiredArgsConstructor
public class ReceiptUseCaseImpl implements ReceiptUseCase {

private final ReceiptGetService receiptGetService;
private final ReceiptDeleteService receiptDeleteService;
private final ReceiptSaveService receiptSaveService;
private final S3Service s3Service;
private final ReceiptMapper mapper;
private final AccountGetService accountGetService;
private final ReceiptGetService receiptGetService;

@Override @Transactional
public void save(ReceiptDTO.Save dto, List<MultipartFile> files) {
List<String> images = s3Service.uploadFiles(files);
private final FileGetService fileGetService;
private final FileSaveService fileSaveService;
private final FileDeleteService fileDeleteService;

private final ReceiptMapper mapper;
private final FileMapper fileMapper;


@Override
@Transactional
public void save(ReceiptDTO.Save dto) {
Account account = accountGetService.find(dto.cardinal());
Receipt receipt = receiptSaveService.save(mapper.from(dto, images, account));
Receipt receipt = receiptSaveService.save(mapper.from(dto, account));
account.spend(receipt);

List<File> files = fileMapper.toFileList(dto.files(), receipt);
fileSaveService.save(files);
}

@Override @Transactional
@Override
@Transactional
public void delete(Long id) {
Receipt receipt = receiptGetService.find(id);
List<File> fileList = fileGetService.findAllByReceipt(id);

receipt.getAccount().cancel(receipt);

fileDeleteService.delete(fileList);
receiptDeleteService.delete(receipt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public class ReceiptAdminController {

private final ReceiptUseCase receiptUseCase;

@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@PostMapping
@Operation(summary="회비 사용 내역 기입")
public CommonResponse<Void> save(@RequestPart @Valid ReceiptDTO.Save dto, @RequestPart(required = false) List<MultipartFile> images) {
receiptUseCase.save(dto, images);
public CommonResponse<Void> save(@RequestBody @Valid ReceiptDTO.Save dto) {
receiptUseCase.save(dto);
return CommonResponse.createSuccess(RECEIPT_SAVE_SUCCESS.getMessage());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package leets.weeth.domain.board.application.dto;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import leets.weeth.domain.comment.application.dto.CommentDTO;
import leets.weeth.domain.file.application.dto.request.FileSaveRequest;
import leets.weeth.domain.file.application.dto.request.FileUpdateRequest;
import leets.weeth.domain.file.application.dto.response.FileResponse;
import lombok.Builder;

import java.time.LocalDateTime;
Expand All @@ -12,14 +16,18 @@ public class NoticeDTO {
@Builder
public record Save(
@NotNull String title,
@NotNull String content
){}
@NotNull String content,
@Valid List<@NotNull FileSaveRequest> files
) {
}

@Builder
public record Update(
@NotNull String title,
@NotNull String content
){}
@NotNull String content,
@Valid List<@NotNull FileSaveRequest> files
) {
}

@Builder
public record Response(
Expand All @@ -29,9 +37,10 @@ public record Response(
String content,
LocalDateTime time,//modifiedAt
Integer commentCount,
List<String> fileUrls,
List<CommentDTO.Response> comments
){}
List<CommentDTO.Response> comments,
List<FileResponse> fileUrls
) {
}

@Builder
public record ResponseAll(
Expand All @@ -41,6 +50,7 @@ public record ResponseAll(
String content,
LocalDateTime time,//modifiedAt
Integer commentCount
){}
) {
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package leets.weeth.domain.board.application.dto;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import leets.weeth.domain.comment.application.dto.CommentDTO;
import leets.weeth.domain.file.application.dto.request.FileSaveRequest;
import leets.weeth.domain.file.application.dto.request.FileUpdateRequest;
import leets.weeth.domain.file.application.dto.response.FileResponse;
import lombok.Builder;

import java.time.LocalDateTime;
Expand All @@ -12,13 +16,15 @@ public class PostDTO {
@Builder
public record Save(
@NotNull String title,
@NotNull String content
@NotNull String content,
@Valid List<@NotNull FileSaveRequest> files
){}

@Builder
public record Update(
@NotNull String title,
@NotNull String content
@NotNull String content,
@Valid List<@NotNull FileSaveRequest> files
){}

@Builder
Expand All @@ -29,8 +35,8 @@ public record Response(
String content,
LocalDateTime time,//modifiedAt
Integer commentCount,
List<String> fileUrls,
List<CommentDTO.Response> comments
List<CommentDTO.Response> comments,
List<FileResponse> fileUrls
){}

@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import leets.weeth.domain.comment.application.dto.CommentDTO;
import leets.weeth.domain.comment.application.mapper.CommentMapper;
import leets.weeth.domain.comment.domain.entity.Comment;
import leets.weeth.domain.file.application.dto.response.FileResponse;
import leets.weeth.domain.file.domain.entity.File;
import leets.weeth.domain.user.domain.entity.User;
import org.mapstruct.*;

Expand All @@ -19,7 +21,7 @@ public interface NoticeMapper {
@Mapping(target = "id", ignore = true),
@Mapping(target = "user", source = "user")
})
Notice fromNoticeDto(NoticeDTO.Save dto, List<String> fileUrls, User user);
Notice fromNoticeDto(NoticeDTO.Save dto, User user);

@Mappings({
@Mapping(target = "name", source = "user.name"),
Expand All @@ -28,11 +30,11 @@ public interface NoticeMapper {
NoticeDTO.ResponseAll toAll(Notice notice);

@Mappings({
@Mapping(target = "name", source = "user.name"),
@Mapping(target = "name", source = "notice.user.name"),
@Mapping(target = "comments", expression = "java(filterParentComments(notice.getComments()))"),
@Mapping(target = "time", source = "modifiedAt")
@Mapping(target = "time", source = "notice.modifiedAt")
})
NoticeDTO.Response toNoticeDto(Notice notice);
NoticeDTO.Response toNoticeDto(Notice notice, List<FileResponse> fileUrls);

default List<CommentDTO.Response> filterParentComments(List<Comment> comments) {
if (comments == null || comments.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import leets.weeth.domain.comment.application.dto.CommentDTO;
import leets.weeth.domain.comment.application.mapper.CommentMapper;
import leets.weeth.domain.comment.domain.entity.Comment;
import leets.weeth.domain.file.application.dto.response.FileResponse;
import leets.weeth.domain.user.domain.entity.User;
import org.mapstruct.*;

Expand All @@ -19,7 +20,7 @@ public interface PostMapper {
@Mapping(target = "id", ignore = true),
@Mapping(target = "user", source = "user")
})
Post fromPostDto(PostDTO.Save dto, List<String> fileUrls, User user);
Post fromPostDto(PostDTO.Save dto, User user);

@Mappings({
@Mapping(target = "name", source = "user.name"),
Expand All @@ -28,11 +29,11 @@ public interface PostMapper {
PostDTO.ResponseAll toAll(Post post);

@Mappings({
@Mapping(target = "name", source = "user.name"),
@Mapping(target = "name", source = "post.user.name"),
@Mapping(target = "comments", expression = "java(filterParentComments(post.getComments()))"),
@Mapping(target = "time", source = "modifiedAt")
@Mapping(target = "time", source = "post.modifiedAt")
})
PostDTO.Response toPostDto(Post post);
PostDTO.Response toPostDto(Post post, List<FileResponse> fileUrls);

default List<CommentDTO.Response> filterParentComments(List<Comment> comments) {
if (comments == null || comments.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

public interface NoticeUsecase {

void save(NoticeDTO.Save dto, List<MultipartFile> files, Long userId);
void save(NoticeDTO.Save dto, Long userId);

NoticeDTO.Response findNotice(Long noticeId);

List<NoticeDTO.ResponseAll> findNotices(Long noticeId, Integer count);

void update(Long noticeId, NoticeDTO.Update dto, List<MultipartFile> files, Long userId) throws UserNotMatchException;
void update(Long noticeId, NoticeDTO.Update dto, Long userId) throws UserNotMatchException;

void delete(Long noticeId, Long userId) throws UserNotMatchException;

Expand Down
Loading

0 comments on commit 9e56411

Please sign in to comment.