Skip to content

Commit

Permalink
[DDING-039] 공지사항 도메인 파일 정책 변경사항 대응 (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
wonjunYou authored Nov 4, 2024
1 parent 22d0250 commit e27c1b0
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ public enum DomainType {
CLUB_PROFILE,
CLUB_INTRODUCTION,
FIX_ZONE_IMAGE,
NOTICE_IMAGE,
NOTICE_FILE,
DOCUMENT_FILE
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import ddingdong.ddingdongBE.domain.filemetadata.entity.DomainType;
import ddingdong.ddingdongBE.domain.filemetadata.entity.FileMetaData;
import ddingdong.ddingdongBE.domain.filemetadata.repository.FileMetaDataRepository;
import jakarta.persistence.EntityManager;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
Expand All @@ -18,6 +19,7 @@
public class FileMetaDataServiceImpl implements FileMetaDataService {

private final FileMetaDataRepository fileMetaDataRepository;
private final EntityManager entityManager;

@Override
@Transactional
Expand All @@ -31,13 +33,15 @@ public List<FileMetaData> getCoupledAllByDomainTypeAndEntityId(DomainType domain
return fileMetaDataRepository.findAllByDomainTypeAndEntityIdWithFileStatus(domainType, entityId, COUPLED);
}

@Transactional
@Override
public void updateStatusToCoupled(List<String> ids, DomainType domainType, Long entityId) {
ids.forEach(id -> {
updateStatusToCoupled(id, domainType, entityId);
});
}

@Transactional
@Override
public void updateStatusToCoupled(String id, DomainType domainType, Long entityId) {
UUID fileMetaDataId = UUID.fromString(id);
Expand All @@ -49,6 +53,7 @@ public void updateStatusToCoupled(String id, DomainType domainType, Long entityI
fileMetaData.updateStatus(COUPLED);
}

@Transactional
@Override
public void update(String id, DomainType domainType, Long entityId) {
updateStatusToDelete(domainType, entityId);
Expand All @@ -58,6 +63,7 @@ public void update(String id, DomainType domainType, Long entityId) {
updateStatusToCoupled(id, domainType, entityId);
}

@Transactional
@Override
public void update(List<String> ids, DomainType domainType, Long entityId) {
updateStatusToDelete(domainType, entityId);
Expand All @@ -67,12 +73,15 @@ public void update(List<String> ids, DomainType domainType, Long entityId) {
updateStatusToCoupled(ids, domainType, entityId);
}

@Transactional
@Override
public void updateStatusToDelete(DomainType domainType, Long entityId) {
List<FileMetaData> fileMetaDatas = getCoupledAllByDomainTypeAndEntityId(domainType, entityId);
fileMetaDatas.forEach(fileMetaData -> {
fileMetaData.updateStatus(DELETED);
entityManager.flush();
fileMetaDataRepository.delete(fileMetaData);
});
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ddingdong.ddingdongBE.domain.notice.controller.dto.request;

import ddingdong.ddingdongBE.common.vo.FileInfo;
import ddingdong.ddingdongBE.domain.notice.service.dto.command.CreateNoticeCommand;
import ddingdong.ddingdongBE.domain.user.entity.User;
import io.swagger.v3.oas.annotations.media.Schema;
Expand All @@ -16,20 +15,20 @@ public record CreateNoticeRequest(
@Schema(description = "공지사항 내용", example = "공지사항 내용")
String content,

@Schema(description = "공지사항 이미지 key 목록")
List<String> imageKeys,
@Schema(description = "공지사항 이미지 식별자 목록", example = "[\"0192c828-ffce-7ee8-94a8-d9d4c8cdec00\", \"0192c828-ffce-7ee8-94a8-d9d4c8cdec00\"]")
List<String> images,

@Schema(description = "공지사항 파일 정보 목록")
List<FileInfo> fileInfos
@Schema(description = "공지사항 파일 식별자 목록", example = "[\"0192c828-ffce-7ee8-94a8-d9d4c8cdec00\", \"0192c828-ffce-7ee8-94a8-d9d4c8cdec00\"]")
List<String> files
) {

public CreateNoticeCommand toCommand(User user) {
return CreateNoticeCommand.builder()
.user(user)
.title(title)
.content(content)
.imageKeys(imageKeys)
.fileInfos(fileInfos)
.images(images)
.files(files)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ddingdong.ddingdongBE.domain.notice.controller.dto.request;

import ddingdong.ddingdongBE.common.vo.FileInfo;
import ddingdong.ddingdongBE.domain.notice.service.dto.command.UpdateNoticeCommand;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
Expand All @@ -15,20 +14,20 @@ public record UpdateNoticeRequest(
@Schema(description = "공지사항 내용", example = "공지사항 내용")
String content,

@Schema(description = "공지사항 이미지 key 목록")
List<String> imageKeys,
@Schema(description = "공지사항 이미지 식별자 목록", example = "[\"0192c828-ffce-7ee8-94a8-d9d4c8cdec00\", \"0192c828-ffce-7ee8-94a8-d9d4c8cdec00\"]")
List<String> images,

@Schema(description = "공지사항 파일 정보 목록")
List<FileInfo> fileInfos
@Schema(description = "공지사항 파일 식별자 목록", example = "[\"0192c828-ffce-7ee8-94a8-d9d4c8cdec00\", \"0192c828-ffce-7ee8-94a8-d9d4c8cdec00\"]")
List<String> files
) {

public UpdateNoticeCommand toCommand(Long noticeId) {
return UpdateNoticeCommand.builder()
.noticeId(noticeId)
.title(title)
.content(content)
.imageKeys(imageKeys)
.fileInfos(fileInfos)
.images(images)
.files(files)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package ddingdong.ddingdongBE.domain.notice.controller.dto.response;

import ddingdong.ddingdongBE.domain.notice.service.dto.query.NoticeQuery;
import ddingdong.ddingdongBE.file.service.dto.query.UploadedFileUrlAndNameQuery;
import ddingdong.ddingdongBE.file.service.dto.query.UploadedFileUrlQuery;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Schema;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.IntStream;
import lombok.Builder;

@Builder
Expand All @@ -21,45 +21,20 @@ public record NoticeResponse(
LocalDateTime createdAt,

@ArraySchema(schema = @Schema(description = "이미지 Urls", example = "https://%s.s3.%s.amazonaws.com/%s/%s/%s"))
List<UploadedFileUrlQuery> imageUrls,
List<UploadedFileUrlQuery> images,

@ArraySchema(schema = @Schema(description = "파일 상세 정보", implementation = FileDetail.class))
List<FileDetail> fileInfos
@ArraySchema(schema = @Schema(description = "파일 상세 정보"))
List<UploadedFileUrlAndNameQuery> files
) {

public static NoticeResponse from(NoticeQuery query) {
List<FileDetail> fileDetails = IntStream.range(0, query.fileNames().size())
.mapToObj(i -> FileDetail.from(query, i))
.toList();

return NoticeResponse.builder()
.title(query.title())
.content(query.content())
.createdAt(query.createdAt())
.imageUrls(query.imageUrls())
.fileInfos(fileDetails)
.images(query.images())
.files(query.files())
.build();
}

@Builder
public record FileDetail(
@Schema(description = "파일 이름", example = "첨부파일1.pdf")
String fileName,

@Schema(description = "원본 파일 URL", example = "https://example-origin.com/file.pdf")
String originUrl,

@Schema(description = "CDN 파일 URL", example = "https://example-cdn.com/file.pdf")
String cdnUrl
) {

public static FileDetail from(NoticeQuery query, int index) {
return FileDetail.builder()
.fileName(query.fileNames().get(index))
.originUrl(query.fileUrls().get(index).originUrl())
.cdnUrl(query.fileUrls().get(index).cdnUrl())
.build();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package ddingdong.ddingdongBE.domain.notice.entity;

import ddingdong.ddingdongBE.common.BaseEntity;
import ddingdong.ddingdongBE.common.converter.StringListConverter;
import ddingdong.ddingdongBE.domain.user.entity.User;
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
Expand All @@ -13,7 +11,6 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import java.time.LocalDateTime;
import java.util.List;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -40,31 +37,19 @@ public class Notice extends BaseEntity {

private String content;

@Convert(converter = StringListConverter.class)
@Column(name = "notice_image_keys")
private List<String> imageKeys;

@Column(columnDefinition = "json", name = "notice_file_infos")
private String fileInfos;

@Column(name = "deleted_at", columnDefinition = "TIMESTAMP")
private LocalDateTime deletedAt;

@Builder
public Notice(User user, String title, String content, List<String> imageKeys,
String fileInfos) {
public Notice(User user, String title, String content) {
this.user = user;
this.title = title;
this.content = content;
this.imageKeys = imageKeys;
this.fileInfos = fileInfos;
}

public void update(Notice notice) {
this.title = notice.getTitle();
this.content = notice.getContent();
this.imageKeys = notice.imageKeys;
this.fileInfos = notice.fileInfos;
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package ddingdong.ddingdongBE.domain.notice.service;

import ddingdong.ddingdongBE.common.converter.FileInfoConverter;
import ddingdong.ddingdongBE.common.vo.FileInfo;
import ddingdong.ddingdongBE.domain.filemetadata.entity.FileCategory;
import ddingdong.ddingdongBE.domain.filemetadata.entity.FileMetaData;
import ddingdong.ddingdongBE.domain.filemetadata.entity.DomainType;
import ddingdong.ddingdongBE.domain.filemetadata.service.FileMetaDataService;
import ddingdong.ddingdongBE.domain.notice.entity.Notice;
import ddingdong.ddingdongBE.domain.notice.service.dto.command.CreateNoticeCommand;
import ddingdong.ddingdongBE.domain.notice.service.dto.command.UpdateNoticeCommand;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -20,51 +16,36 @@ public class FacadeAdminNoticeServiceImpl implements FacadeAdminNoticeService {

private final NoticeServiceImpl noticeService;
private final FileMetaDataService fileMetaDataService;
private final FileInfoConverter fileInfoConverter;

@Transactional
public void create(CreateNoticeCommand command) {
String fileInfoJsonString = fileInfoConverter.convertToString(command.fileInfos());
Notice notice = command.toEntity(fileInfoJsonString);
noticeService.save(notice);
Notice notice = command.toEntity();
Long createdNoticeId = noticeService.save(notice);

createFileMetaData(command.imageKeys());

List<String> fileKeys = command.fileInfos().stream()
.map(FileInfo::fileKey)
.toList();
createFileMetaData(fileKeys);
fileMetaDataService.updateToCoupled(command.images(), DomainType.NOTICE_IMAGE,
createdNoticeId);
fileMetaDataService.updateToCoupled(command.files(), DomainType.NOTICE_FILE,
createdNoticeId);
}

@Transactional
public void update(UpdateNoticeCommand command) {
Notice notice = noticeService.getById(command.noticeId());
notice.update(command.toEntity());

String fileInfoJsonString = fileInfoConverter.convertToString(command.fileInfos());
notice.update(command.toEntity(fileInfoJsonString));

createFileMetaData(command.imageKeys());

List<String> fileKeys = command.fileInfos().stream()
.map(FileInfo::fileKey)
.toList();
createFileMetaData(fileKeys);
fileMetaDataService.update(command.images(), DomainType.NOTICE_IMAGE,
command.noticeId());
fileMetaDataService.update(command.files(), DomainType.NOTICE_FILE,
command.noticeId());
}

@Transactional
public void delete(Long noticeId) {
Notice notice = noticeService.getById(noticeId);
noticeService.delete(notice);
}

private void createFileMetaData(List<String> fileKeys) {
// if (!fileKeys.isEmpty()) {
// fileMetaDataService.save(
// fileKeys.stream()
// .map(fileKey -> FileMetaData.of(fileKey, FileCategory.NOTICE_FILE))
// .toList()
// );
// }
fileMetaDataService.updateToDelete(DomainType.NOTICE_IMAGE, noticeId);
fileMetaDataService.updateToDelete(DomainType.NOTICE_FILE, noticeId);
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package ddingdong.ddingdongBE.domain.notice.service;

import ddingdong.ddingdongBE.common.converter.FileInfoConverter;
import ddingdong.ddingdongBE.common.vo.FileInfo;
import ddingdong.ddingdongBE.domain.filemetadata.entity.DomainType;
import ddingdong.ddingdongBE.domain.filemetadata.service.FileMetaDataService;
import ddingdong.ddingdongBE.domain.notice.entity.Notice;
import ddingdong.ddingdongBE.domain.notice.service.dto.command.GetNoticeListCommand;
import ddingdong.ddingdongBE.domain.notice.service.dto.query.NoticeListPagingQuery;
import ddingdong.ddingdongBE.domain.notice.service.dto.query.NoticeListPagingQuery.NoticeInfo;
import ddingdong.ddingdongBE.domain.notice.service.dto.query.NoticeQuery;
import ddingdong.ddingdongBE.file.service.S3FileService;
import ddingdong.ddingdongBE.file.service.dto.query.UploadedFileUrlAndNameQuery;
import ddingdong.ddingdongBE.file.service.dto.query.UploadedFileUrlQuery;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -20,8 +21,8 @@
public class FacadeNoticeServiceImpl implements FacadeNoticeService {

private final NoticeServiceImpl noticeService;
private final FileMetaDataService fileMetaDataService;
private final S3FileService s3FileService;
private final FileInfoConverter fileInfoConverter;

@Override
public NoticeListPagingQuery getNoticeList(GetNoticeListCommand command) {
Expand All @@ -39,22 +40,20 @@ public NoticeListPagingQuery getNoticeList(GetNoticeListCommand command) {
public NoticeQuery getNotice(Long noticeId) {
Notice notice = noticeService.getById(noticeId);

List<UploadedFileUrlQuery> imageUrls = notice.getImageKeys().stream()
.map(s3FileService::getUploadedFileUrl)
.toList();

List<FileInfo> fileInfos = fileInfoConverter.convertToJson(notice.getFileInfos());

List<String> fileNames = fileInfos
List<UploadedFileUrlQuery> imageUrlQueries = fileMetaDataService
.getCoupledAllByDomainTypeAndEntityId(DomainType.NOTICE_IMAGE, noticeId)
.stream()
.map(FileInfo::fileName)
.map(fileMetaData -> s3FileService.getUploadedFileUrl(fileMetaData.getFileKey()))
.toList();

List<UploadedFileUrlQuery> fileUrls = fileInfos.stream()
.map(fileInfo -> s3FileService.getUploadedFileUrl(fileInfo.fileKey()))
List<UploadedFileUrlAndNameQuery> fileUrlAndNameQueries = fileMetaDataService
.getCoupledAllByDomainTypeAndEntityId(DomainType.NOTICE_FILE, noticeId)
.stream()
.map(fileMetaData -> s3FileService.getUploadedFileUrlAndName(fileMetaData.getFileKey(),
fileMetaData.getFileName()))
.toList();

return NoticeQuery.of(notice, imageUrls, fileNames, fileUrls);
return NoticeQuery.of(notice, imageUrlQueries, fileUrlAndNameQueries);
}

}
Loading

0 comments on commit e27c1b0

Please sign in to comment.