Skip to content

Commit

Permalink
feat: add display index setting logic for createNewPhotoFileUrls
Browse files Browse the repository at this point in the history
  • Loading branch information
gmkim20713 committed Oct 25, 2024
1 parent c1267c6 commit 3ddada2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Flux<PhotoResponse> uploadFileUrlPhoto(
PhotoFileUrlUploadRequest request
){
return photoService
.createNewPhotoFileUrl(request.fileUrls(), request.albumId(), memberId)
.createNewPhotoFileUrls(request.fileUrls(), request.albumId(), memberId)
.map(PhotoResponse::fromEntity);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ public PhotoEntity updateDisplayIndex(Integer displayIndex) {
return this;
}

public static PhotoEntity newPhoto(String photoId, String photoUrl, BrandType brandType, String albumId, String ownerMemberId) {
public static PhotoEntity newPhoto(String photoId, String photoUrl, BrandType brandType, String albumId, Integer displayIndex, String ownerMemberId) {
PhotoEntity photo = new PhotoEntity();
photo.photoId = photoId;
photo.photoUrl = photoUrl;
photo.brand = brandType;
photo.ownerMemberId = ownerMemberId;
photo.albumId = albumId;
photo.displayIndex = 0;
photo.displayIndex = displayIndex;
photo.isNew = true;
photo.createdAt = LocalDateTime.now();
photo.updatedAt = LocalDateTime.now();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

import java.util.concurrent.atomic.AtomicInteger;


@Slf4j
@RequiredArgsConstructor
Expand All @@ -35,24 +37,34 @@ public Mono<PhotoEntity> createNewPhotoByQrUrl(String qrUrl, String requestMembe
return qrService
.getFileFromQrUrl(qrUrl)
.flatMap(fileDto -> objectStorageService.uploadFile(fileDto.fileByte())
.flatMap(photoUrl -> createNewPhoto(photoUrl, fileDto.type(), null, requestMemberId))
.flatMap(photoUrl -> createNewPhoto(photoUrl, fileDto.type(), requestMemberId))
);
}

@Transactional
public Flux<PhotoEntity> createNewPhotoFileUrl(String[] fileUrls, String albumId, String requestMemberId) {
public Flux<PhotoEntity> createNewPhotoFileUrls(String[] fileUrls, String albumId, String requestMemberId) {
return albumService.findByAlbumId(albumId, requestMemberId)
.flatMap(albumEntity -> albumService.increaseAlbumPhotoCount(albumId, fileUrls.length, requestMemberId))
.thenMany(
Flux.fromArray(fileUrls)
.flatMap(fileUrl -> objectStorageService.setObjectPublicRead(fileUrl)
.flatMap(fileLink -> createNewPhoto(fileLink, BrandType.EXTERNAL, albumId, requestMemberId))
)
);
.flatMapMany(albumEntity -> {
AtomicInteger displayIndex = new AtomicInteger(albumEntity.getPhotoCount());

return Flux.fromArray(fileUrls)
.concatMap(fileUrl ->
createNewPhotoFileUrl(fileUrl, BrandType.EXTERNAL, albumId, displayIndex.getAndIncrement(), requestMemberId)
);
});
}

private Mono<PhotoEntity> createNewPhotoFileUrl(String fileUrl, BrandType type, String albumId, Integer displayIndex, String requestMemberId) {
return objectStorageService.setObjectPublicRead(fileUrl)
.flatMap(fileLink -> {
PhotoEntity photoEntity = PhotoEntity.newPhoto(IdGenerator.generate(), fileLink, type, albumId, displayIndex, requestMemberId);
return albumService.increaseAlbumPhotoCount(albumId, 1, requestMemberId)
.then(photoRepository.save(photoEntity));
});
}

private Mono<PhotoEntity> createNewPhoto(String photoUrl, BrandType type, String albumId, String requestMemberId) {
PhotoEntity photoEntity = PhotoEntity.newPhoto(IdGenerator.generate(), photoUrl, type, albumId, requestMemberId);
private Mono<PhotoEntity> createNewPhoto(String photoUrl, BrandType type, String requestMemberId) {
PhotoEntity photoEntity = PhotoEntity.newPhoto(IdGenerator.generate(), photoUrl, type, null, 0, requestMemberId);
return photoRepository.save(photoEntity);
}

Expand All @@ -74,7 +86,7 @@ public Flux<PhotoEntity> uploadPhoto(Flux<FilePart> files, String requestMemberI
})
.flatMap(bytes -> objectStorageService.uploadFile(bytes)
.flatMap(photoUrl -> {
PhotoEntity photoEntity = PhotoEntity.newPhoto(IdGenerator.generate(), photoUrl, BrandType.EXTERNAL, null, requestMemberId);
PhotoEntity photoEntity = PhotoEntity.newPhoto(IdGenerator.generate(), photoUrl, BrandType.EXTERNAL, null, 0, requestMemberId);
return photoRepository.save(photoEntity);
}))
.subscribeOn(Schedulers.boundedElastic())
Expand Down

0 comments on commit 3ddada2

Please sign in to comment.