Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DDING-64] presignedUrl - 이미지 순서 적용 구현 #196

Merged
merged 12 commits into from
Jan 5, 2025
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "file_meta_data",indexes = {@Index(columnList = "domainType,entityId,fileStatus")})
@Table(name = "file_meta_data", indexes = {@Index(columnList = "domainType,entityId,fileStatus")})
public class FileMetaData extends BaseEntity {

@Id
Expand All @@ -39,15 +39,19 @@ public class FileMetaData extends BaseEntity {
@Column(nullable = false)
private FileStatus fileStatus;

@Column(name = "file_meta_data_order", nullable = false, columnDefinition = "integer default 0")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default를 0으로 설정해서 null값을 가질 일이 없어지는 것일까요?!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

맞습니다! order를 primitive type으로 해서 nullable하게 할 경우 조회 시 null값일 경우 에러가 발생해 해당 방식으로 지정했습니다.

private int order;

@Builder
private FileMetaData(UUID id, String fileKey, String fileName, DomainType domainType, Long entityId,
FileStatus fileStatus) {
FileStatus fileStatus, int order) {
this.id = id;
this.fileKey = fileKey;
this.fileName = fileName;
this.domainType = domainType;
this.entityId = entityId;
this.fileStatus = fileStatus;
this.order = order;
}

public static FileMetaData createPending(UUID id, String fileKey, String fileName) {
Expand Down Expand Up @@ -79,4 +83,8 @@ public boolean isPending() {
public boolean isOwn(Long entityId) {
return this.entityId.equals(entityId);
}

public void updateOrder(int order) {
this.order = order;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ddingdong.ddingdongBE.domain.filemetadata.entity.DomainType;
import ddingdong.ddingdongBE.domain.filemetadata.entity.FileMetaData;
import ddingdong.ddingdongBE.domain.filemetadata.service.dto.FileMetaDataIdOrderDto;
import java.util.List;
import java.util.UUID;

Expand All @@ -14,17 +15,21 @@ public interface FileMetaDataService {
List<FileMetaData> getCoupledAllByEntityId(Long entityId);

List<FileMetaData> getCoupledAllByDomainTypeAndEntityIdOrderedAsc(DomainType domainType,
Long entityId);
Long entityId);

List<FileMetaData> getCoupledAllByEntityIds(List<Long> entityIds);

void updateStatusToCoupled(List<String> ids, DomainType domainType, Long entityId);

void updateStatusToCoupled(String id, DomainType domainType, Long entityId);

void updateStatusToCoupledWithOrder(List<FileMetaDataIdOrderDto> ids, DomainType domainType, Long entityId);

void updateStatusToDelete(DomainType domainType, Long entityId);

void update(String id, DomainType domainType, Long entityId);

void update(List<String> ids, DomainType domainType, Long entityId);

void updateWithOrder(List<FileMetaDataIdOrderDto> fileMetaDataIdOrderDtos, DomainType domainType, Long entityId);
5uhwann marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
import ddingdong.ddingdongBE.domain.filemetadata.entity.DomainType;
import ddingdong.ddingdongBE.domain.filemetadata.entity.FileMetaData;
import ddingdong.ddingdongBE.domain.filemetadata.repository.FileMetaDataRepository;
import ddingdong.ddingdongBE.domain.filemetadata.service.dto.FileMetaDataIdOrderDto;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -22,8 +25,8 @@ public class FileMetaDataServiceImpl implements FileMetaDataService {

private final FileMetaDataRepository fileMetaDataRepository;

@Override
@Transactional
@Override
public UUID create(FileMetaData fileMetaData) {
FileMetaData savedFileMetaData = fileMetaDataRepository.save(fileMetaData);
return savedFileMetaData.getId();
Expand All @@ -41,9 +44,9 @@ public List<FileMetaData> getCoupledAllByEntityId(Long entityId) {

@Override
public List<FileMetaData> getCoupledAllByDomainTypeAndEntityIdOrderedAsc(DomainType domainType,
Long entityId) {
Long entityId) {
return fileMetaDataRepository.findAllByDomainTypeAndEntityIdWithFileStatusOrderedAsc(
domainType, entityId, COUPLED);
domainType, entityId, COUPLED);
}

@Override
Expand Down Expand Up @@ -82,6 +85,37 @@ public void updateStatusToCoupled(String id, DomainType domainType, Long entityI
fileMetaData.updateStatus(COUPLED);
}

@Override
public void updateStatusToCoupledWithOrder(
List<FileMetaDataIdOrderDto> fileMetaDataIdOrderDtos,
DomainType domainType,
Long entityId
) {
if (fileMetaDataIdOrderDtos == null || fileMetaDataIdOrderDtos.isEmpty()) {
return;
}
List<UUID> fileMetaDataIds = toUUIDs(fileMetaDataIdOrderDtos.stream()
.map(FileMetaDataIdOrderDto::fileMetaDatId)
.toList());
List<FileMetaData> fileMetaDataList = fileMetaDataRepository.findByIdIn(fileMetaDataIds);
if (fileMetaDataIdOrderDtos.size() != fileMetaDataList.size()) {
throw new ResourceNotFound("해당 FileMetaData(id: " + fileMetaDataIds + ")를 찾을 수 없습니다.");
}
Map<UUID, Integer> orderMap = fileMetaDataIdOrderDtos.stream()
.collect(Collectors.toMap(
dto -> UUID.fromString(dto.fileMetaDatId()),
FileMetaDataIdOrderDto::fileMetaDataOrder
));

fileMetaDataList.forEach(fileMetaData -> {
fileMetaData.updateOrder(orderMap.get(fileMetaData.getId()));
if (fileMetaData.isPending()) {
fileMetaData.updateCoupledEntityInfo(domainType, entityId);
fileMetaData.updateStatus(COUPLED);
}
});
}

@Transactional
@Override
public void update(String id, DomainType domainType, Long entityId) {
Expand All @@ -104,6 +138,21 @@ public void update(List<String> ids, DomainType domainType, Long entityId) {
updateStatusToCoupled(newIds, domainType, entityId);
}

@Transactional
@Override
public void updateWithOrder(List<FileMetaDataIdOrderDto> fileMetaDataIdOrderDtos, DomainType domainType,
Long entityId) {
if (fileMetaDataIdOrderDtos == null || fileMetaDataIdOrderDtos.isEmpty()) {
updateStatusToDelete(domainType, entityId);
return;
}
List<String> ids = fileMetaDataIdOrderDtos.stream()
.map(FileMetaDataIdOrderDto::fileMetaDatId)
.toList();
deleteOldIds(ids, domainType, entityId); //ids에 포함된 id를 가진 fileMetaData외에 전부 제거
updateStatusToCoupledWithOrder(fileMetaDataIdOrderDtos, domainType, entityId);
}

@Transactional
@Override
public void updateStatusToDelete(DomainType domainType, Long entityId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ddingdong.ddingdongBE.domain.filemetadata.service.dto;

public record FileMetaDataIdOrderDto(
String fileMetaDatId,
5uhwann marked this conversation as resolved.
Show resolved Hide resolved
int fileMetaDataOrder

) {

public static FileMetaDataIdOrderDto of(String fileMetaDatId, int fileMetaDataOrder) {
return new FileMetaDataIdOrderDto(fileMetaDatId, fileMetaDataOrder);
}

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

import ddingdong.ddingdongBE.domain.fixzone.service.dto.command.CreateFixZoneCommand;
import ddingdong.ddingdongBE.domain.fixzone.service.dto.command.CreateFixZoneCommand.ImageInfo;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import java.util.List;
Expand All @@ -12,17 +13,28 @@ public record CreateFixZoneRequest(
@NotNull
@Schema(description = "내용")
String content,
@Schema(description = "픽스존 이미지 식별자 목록", example = "[\"0192c828-ffce-7ee8-94a8-d9d4c8cdec00\", \"0192c828-ffce-7ee8-94a8-d9d4c8cdec00\"]")
List<String> fixZoneImageIds
@Schema(description = "픽스존 이미지 정보 목록")
List<ImageInfoRequest> images
) {

public CreateFixZoneCommand toCommand(Long userId) {
return new CreateFixZoneCommand(
userId,
title,
content,
fixZoneImageIds
images.stream()
.map(image -> new ImageInfo(image.id, image.order()))
.toList()
);
}

public record ImageInfoRequest(
@Schema(description = "이미지 식별자", example = "0192c828-ffce-7ee8-94a8-d9d4c8cdec00")
String id,
@Schema(description = "이미지 순서", example = "1")
int order
) {

}

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

import ddingdong.ddingdongBE.domain.fixzone.service.dto.command.UpdateFixZoneCommand;
import ddingdong.ddingdongBE.domain.fixzone.service.dto.command.UpdateFixZoneCommand.ImageInfo;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import java.util.List;
Expand All @@ -12,17 +13,28 @@ public record UpdateFixZoneRequest(
@NotNull
@Schema(description = "내용")
String content,
@Schema(description = "픽스존 이미지 식별자 목록", example = "[\"0192c828-ffce-7ee8-94a8-d9d4c8cdec00\", \"0192c828-ffce-7ee8-94a8-d9d4c8cdec00\"]")
List<String> fixZoneImageIds
@Schema(description = "픽스존 이미지 정보 목록")
List<ImageInfoRequest> images
5uhwann marked this conversation as resolved.
Show resolved Hide resolved
) {

public UpdateFixZoneCommand toCommand(Long fixZoneId) {
return new UpdateFixZoneCommand(
fixZoneId,
title,
content,
fixZoneImageIds
images.stream()
.map(image -> new ImageInfo(image.id(), image.order()))
.toList()
);
}

public record ImageInfoRequest(
@Schema(description = "이미지 식별자", example = "0192c828-ffce-7ee8-94a8-d9d4c8cdec00")
String id,
@Schema(description = "이미지 순서", example = "1")
int order
) {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ddingdong.ddingdongBE.domain.fixzone.service.dto.query.AdminFixZoneQuery;
import ddingdong.ddingdongBE.domain.fixzone.service.dto.query.AdminFixZoneQuery.FixZoneCommentQuery;
import ddingdong.ddingdongBE.file.service.dto.query.UploadedFileUrlQuery;
import ddingdong.ddingdongBE.file.service.dto.query.UploadedFileUrlWithOrderQuery;
import io.swagger.v3.oas.annotations.media.Schema;
import java.time.LocalDateTime;
import java.util.List;
Expand Down Expand Up @@ -54,17 +55,19 @@ public static AdminFixZoneResponse from(AdminFixZoneQuery query) {
description = "어드민 - 픽스존 이미지 URL 조회 응답"
)
record FixZoneImageUrlResponse(
@Schema(description = "이미지 순서", example = "1")
int order,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p3)
회의 중에 백엔드 내에서 순서대로 정렬하여 응답해준다고 하여 클라이언트에서는 신경 쓸 필요가 없다고 했는데, 순서를 응답값에 포함한 이유가 있나요!?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update시 순서를 클라이언트에서 인식해서 지정해줘야해서 order를 응답에 포함시켰습니다!

@Schema(description = "원본 url", example = "url")
String originUrl,
@Schema(description = "cdn url", example = "url")
String cdnUrl
) {

public static FixZoneImageUrlResponse from(UploadedFileUrlQuery query) {
public static FixZoneImageUrlResponse from(UploadedFileUrlWithOrderQuery query) {
if (query == null) {
return null;
}
return new FixZoneImageUrlResponse(query.originUrl(), query.cdnUrl());
return new FixZoneImageUrlResponse(query.order(), query.originUrl(), query.cdnUrl());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ddingdong.ddingdongBE.domain.fixzone.service.dto.query.CentralFixZoneQuery;
import ddingdong.ddingdongBE.domain.fixzone.service.dto.query.CentralFixZoneQuery.FixZoneCommentQuery;
import ddingdong.ddingdongBE.file.service.dto.query.UploadedFileUrlQuery;
import ddingdong.ddingdongBE.file.service.dto.query.UploadedFileUrlWithOrderQuery;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Schema;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -57,17 +58,19 @@ public static CentralFixZoneResponse from(CentralFixZoneQuery query) {
record FixZoneImageUrlResponse(
@Schema(description = "파일 식별자", example = "0192c828-ffce-7ee8-94a8-d9d4c8cdec00l")
String id,
@Schema(description = "이미지 순서", example = "1")
int order,
@Schema(description = "원본 url", example = "url")
String originUrl,
@Schema(description = "cdn url", example = "url")
String cdnUrl
) {

public static FixZoneImageUrlResponse from(UploadedFileUrlQuery query) {
public static FixZoneImageUrlResponse from(UploadedFileUrlWithOrderQuery query) {
if (query == null) {
return null;
}
return new FixZoneImageUrlResponse(query.id(), query.originUrl(), query.cdnUrl());
return new FixZoneImageUrlResponse(query.id(), query.order(), query.originUrl(), query.cdnUrl());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ddingdong.ddingdongBE.domain.fixzone.service.dto.query.AdminFixZoneQuery;
import ddingdong.ddingdongBE.file.service.S3FileService;
import ddingdong.ddingdongBE.file.service.dto.query.UploadedFileUrlQuery;
import ddingdong.ddingdongBE.file.service.dto.query.UploadedFileUrlWithOrderQuery;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -32,10 +33,11 @@ public List<AdminFixZoneListQuery> getAll() {
@Override
public AdminFixZoneQuery getFixZone(Long fixZoneId) {
FixZone fixZone = fixZoneService.getById(fixZoneId);
List<UploadedFileUrlQuery> imageUrlQueries = fileMetaDataService
List<UploadedFileUrlWithOrderQuery> imageUrlQueries = fileMetaDataService
.getCoupledAllByDomainTypeAndEntityId(DomainType.FIX_ZONE_IMAGE, fixZoneId)
.stream()
.map(fileMetaData -> s3FileService.getUploadedFileUrl(fileMetaData.getFileKey()))
.map(fileMetaData -> UploadedFileUrlWithOrderQuery.of(
s3FileService.getUploadedFileUrl(fileMetaData.getFileKey()), fileMetaData.getOrder()))
.toList();
Club club = fixZone.getClub();
UploadedFileUrlQuery clubProfileImageQuery = fileMetaDataService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import ddingdong.ddingdongBE.domain.club.service.ClubService;
import ddingdong.ddingdongBE.domain.filemetadata.entity.DomainType;
import ddingdong.ddingdongBE.domain.filemetadata.service.FileMetaDataService;
import ddingdong.ddingdongBE.domain.filemetadata.service.dto.FileMetaDataIdOrderDto;
import ddingdong.ddingdongBE.domain.fixzone.entity.FixZone;
import ddingdong.ddingdongBE.domain.fixzone.service.dto.command.CreateFixZoneCommand;
import ddingdong.ddingdongBE.domain.fixzone.service.dto.command.UpdateFixZoneCommand;
import ddingdong.ddingdongBE.domain.fixzone.service.dto.query.CentralFixZoneQuery;
import ddingdong.ddingdongBE.domain.fixzone.service.dto.query.CentralMyFixZoneListQuery;
import ddingdong.ddingdongBE.file.service.S3FileService;
import ddingdong.ddingdongBE.file.service.dto.query.UploadedFileUrlQuery;
import ddingdong.ddingdongBE.file.service.dto.query.UploadedFileUrlWithOrderQuery;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -32,7 +34,16 @@ public Long create(CreateFixZoneCommand command) {
Club club = clubService.getByUserId(command.userId());
FixZone createdFixZone = command.toEntity(club);
Long createdFixZoneId = fixZoneService.save(createdFixZone);
fileMetaDataService.updateStatusToCoupled(command.fixZoneImageIds(), DomainType.FIX_ZONE_IMAGE, createdFixZoneId);

List<FileMetaDataIdOrderDto> imageFileMetaDataIdOrderDtos = command.imageInfos().stream()
.map(imageInfo -> FileMetaDataIdOrderDto.of(imageInfo.imagId(), imageInfo.order()))
.toList();

fileMetaDataService.updateStatusToCoupledWithOrder(
imageFileMetaDataIdOrderDtos,
DomainType.FIX_ZONE_IMAGE,
createdFixZoneId
);
return createdFixZoneId;
}

Expand All @@ -49,10 +60,11 @@ public List<CentralMyFixZoneListQuery> getMyFixZones(Long userId) {
public CentralFixZoneQuery getFixZone(Long fixZoneId) {
FixZone fixZone = fixZoneService.getById(fixZoneId);
Club club = fixZone.getClub();
List<UploadedFileUrlQuery> imageUrlQueries = fileMetaDataService
List<UploadedFileUrlWithOrderQuery> imageUrlQueries = fileMetaDataService
.getCoupledAllByDomainTypeAndEntityId(DomainType.FIX_ZONE_IMAGE, fixZoneId)
.stream()
.map(fileMetaData -> s3FileService.getUploadedFileUrl(fileMetaData.getFileKey()))
.map(fileMetaData -> UploadedFileUrlWithOrderQuery.of(
s3FileService.getUploadedFileUrl(fileMetaData.getFileKey()), fileMetaData.getOrder()))
.toList();

UploadedFileUrlQuery clubProfileImageKey = fileMetaDataService
Expand All @@ -69,7 +81,10 @@ public CentralFixZoneQuery getFixZone(Long fixZoneId) {
public Long update(UpdateFixZoneCommand command) {
FixZone fixZone = fixZoneService.getById(command.fixZoneId());
fixZone.update(command.toEntity());
fileMetaDataService.update(command.fixZoneImageIds(), DomainType.FIX_ZONE_IMAGE, fixZone.getId());
List<FileMetaDataIdOrderDto> imageFileMetaDataIdOrderDtos = command.imageInfos().stream()
.map(imageInfo -> FileMetaDataIdOrderDto.of(imageInfo.imagId(), imageInfo.order()))
.toList();
fileMetaDataService.updateWithOrder(imageFileMetaDataIdOrderDtos, DomainType.FIX_ZONE_IMAGE, fixZone.getId());
return fixZone.getId();
}

Expand Down
Loading
Loading