Skip to content

Commit

Permalink
fix: change create new photo api for newly created brand column (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmkim20713 committed Jul 4, 2024
1 parent 31e3f3c commit 4420b2b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kr.mafoo.photo.controller.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;
import kr.mafoo.photo.domain.BrandType;
import kr.mafoo.photo.domain.PhotoEntity;

@Schema(description = "사진 응답")
Expand All @@ -11,6 +12,9 @@ public record PhotoResponse(
@Schema(description = "사진 URL", example = "photo_url")
String photoUrl,

@Schema(description = "사진 브랜드", example = "LIFE_FOUR_CUTS")
BrandType brand,

@Schema(description = "앨범 ID", example = "test_album_id")
String albumId
) {
Expand All @@ -20,6 +24,7 @@ public static PhotoResponse fromEntity(
return new PhotoResponse(
entity.getPhotoId(),
entity.getPhotoUrl(),
entity.getBrand(),
entity.getAlbumId()
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kr.mafoo.photo.service;

import kr.mafoo.photo.domain.BrandType;
import kr.mafoo.photo.domain.PhotoEntity;
import kr.mafoo.photo.exception.AlbumNotFoundException;
import kr.mafoo.photo.exception.PhotoNotFoundException;
Expand All @@ -23,11 +24,12 @@ public class PhotoService {
public Mono<PhotoEntity> createNewPhoto(String qrUrl, String requestMemberId) {
return qrService
.getFileFromQrUrl(qrUrl)
.flatMap(objectStorageService::uploadFile)
.flatMap(photoUrl -> {
PhotoEntity photoEntity = PhotoEntity.newPhoto(IdGenerator.generate(), photoUrl, requestMemberId);
return photoRepository.save(photoEntity);
});
.flatMap(fileDto -> objectStorageService.uploadFile(fileDto.fileByte())
.flatMap(photoUrl -> {
PhotoEntity photoEntity = PhotoEntity.newPhoto(IdGenerator.generate(), photoUrl, fileDto.type(), requestMemberId);
return photoRepository.save(photoEntity);
})
);
}

public Flux<PhotoEntity> findAllByAlbumId(String albumId, String requestMemberId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import kr.mafoo.photo.exception.PhotoBrandNotExistsException;
import kr.mafoo.photo.exception.PhotoQrUrlExpiredException;
import kr.mafoo.photo.exception.RedirectUriNotFoundException;
import kr.mafoo.photo.service.dto.FileDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
Expand All @@ -23,17 +24,20 @@ public class QrService {

private final WebClient externalWebClient;

protected Mono<byte[]> getFileFromQrUrl(String qrUrl) {
public Mono<FileDto> getFileFromQrUrl(String qrUrl) {
BrandType brandType = Optional.ofNullable(BrandType.matchBrandType(qrUrl))
.orElseThrow(PhotoBrandNotExistsException::new);

return switch (brandType) {
case LIFE_FOUR_CUTS -> getLifeFourCutsFiles(qrUrl);
case PHOTOISM -> getPhotoismFiles(qrUrl);
case HARU_FILM -> getHaruFilmFiles(qrUrl);
case DONT_LOOK_UP -> getDontLookUpFiles(qrUrl);
case LIFE_FOUR_CUTS -> createFileDto(brandType, getLifeFourCutsFiles(qrUrl));
case PHOTOISM -> createFileDto(brandType, getPhotoismFiles(qrUrl));
case HARU_FILM -> createFileDto(brandType, getHaruFilmFiles(qrUrl));
case DONT_LOOK_UP -> createFileDto(brandType, getDontLookUpFiles(qrUrl));
};
}

private Mono<FileDto> createFileDto(BrandType brandType, Mono<byte[]> fileMono) {
return fileMono.map(file -> new FileDto(brandType, file));
}

private Mono<byte[]> getLifeFourCutsFiles(String qrUrl) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package kr.mafoo.photo.service.dto;

import kr.mafoo.photo.domain.BrandType;

public record FileDto (
BrandType type,
byte[] fileByte
) {
}

0 comments on commit 4420b2b

Please sign in to comment.