Skip to content

Commit

Permalink
Merge pull request #60 from YAPP-Github/feature/MAFOO-102
Browse files Browse the repository at this point in the history
[MAFOO-102] feat: 사진 n건 앨범 변경 API 구현
  • Loading branch information
gmkim20713 authored Aug 19, 2024
2 parents b339d4c + c168764 commit 84bef84
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 16 deletions.
14 changes: 13 additions & 1 deletion photo-service/src/main/java/kr/mafoo/photo/api/PhotoApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import kr.mafoo.photo.annotation.RequestMemberId;
import kr.mafoo.photo.annotation.ULID;
import kr.mafoo.photo.controller.dto.request.PhotoCreateRequest;
import kr.mafoo.photo.controller.dto.request.PhotoBulkUpdateAlbumIdRequest;
import kr.mafoo.photo.controller.dto.request.PhotoUpdateAlbumIdRequest;
import kr.mafoo.photo.controller.dto.response.PhotoResponse;
import org.springframework.validation.annotation.Validated;
Expand Down Expand Up @@ -41,7 +42,7 @@ Mono<PhotoResponse> createPhoto(
PhotoCreateRequest request
);

@Operation(summary = "사진 앨범 수정", description = "사진을 다른 앨범으로 이동시킵니다.")
@Operation(summary = "사진 앨범 단건 수정", description = "사진 한 개를 다른 앨범으로 이동시킵니다.")
@PatchMapping("/{photoId}/album")
Mono<PhotoResponse> updatePhotoAlbum(
@RequestMemberId
Expand All @@ -57,6 +58,17 @@ Mono<PhotoResponse> updatePhotoAlbum(
PhotoUpdateAlbumIdRequest request
);

@Operation(summary = "사진 앨범 n건 수정", description = "사진 여러 개를 다른 앨범으로 이동시킵니다.")
@PatchMapping("/bulk/album")
Flux<PhotoResponse> updatePhotoBulkAlbum(
@RequestMemberId
String memberId,

@Valid
@RequestBody
PhotoBulkUpdateAlbumIdRequest request
);

@Operation(summary = "사진 삭제", description = "사진을 삭제합니다.")
@DeleteMapping("{photoId}")
Mono<Void> deletePhoto(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import kr.mafoo.photo.api.PhotoApi;
import kr.mafoo.photo.controller.dto.request.PhotoCreateRequest;
import kr.mafoo.photo.controller.dto.request.PhotoBulkUpdateAlbumIdRequest;
import kr.mafoo.photo.controller.dto.request.PhotoUpdateAlbumIdRequest;
import kr.mafoo.photo.controller.dto.response.PhotoResponse;
import kr.mafoo.photo.service.PhotoService;
Expand All @@ -10,7 +11,6 @@
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import static kr.mafoo.photo.domain.BrandType.LIFE_FOUR_CUTS;

@RequiredArgsConstructor
@RestController
Expand Down Expand Up @@ -49,6 +49,17 @@ public Mono<PhotoResponse> updatePhotoAlbum(
.map(PhotoResponse::fromEntity);
}

@Override
public Flux<PhotoResponse> updatePhotoBulkAlbum(
String memberId,
PhotoBulkUpdateAlbumIdRequest request
){
return photoService
.updatePhotoBulkAlbumId(request.photoIds(), request.albumId(), memberId)
.map(PhotoResponse::fromEntity);
}


@Override
public Mono<Void> deletePhoto(
String memberId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package kr.mafoo.photo.controller.dto.request;

import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Schema;
import kr.mafoo.photo.annotation.ULID;

@Schema(description = "사진 앨범 수정 요청")
public record PhotoBulkUpdateAlbumIdRequest(

@ArraySchema(
schema = @Schema(description = "사진 ID 목록"),
arraySchema = @Schema(example = "[\"test_photo_id_1\", \"test_photo_id_2\", \"test_photo_id_3\"]")
)
String[] photoIds,

@ULID
@Schema(description = "앨범 ID", example = "test_album_id")
String albumId
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ public Mono<Void> deletePhotoById(String photoId, String requestMemberId) {
});
}

@Transactional
public Flux<PhotoEntity> updatePhotoBulkAlbumId(String[] photoIds, String albumId, String requestMemberId) {
return Flux.fromArray(photoIds)
.flatMap(photoId ->
this.updatePhotoAlbumId(photoId, albumId, requestMemberId)
);
}

@Transactional
public Mono<PhotoEntity> updatePhotoAlbumId(String photoId, String albumId, String requestMemberId) {
return photoRepository
Expand All @@ -80,21 +88,21 @@ public Mono<PhotoEntity> updatePhotoAlbumId(String photoId, String albumId, Stri
if (!photoEntity.getOwnerMemberId().equals(requestMemberId)) {
// 내 사진이 아니면 그냥 없는 사진 처리
return Mono.error(new PhotoNotFoundException());
} else {
return albumRepository
.findById(albumId)
.switchIfEmpty(Mono.error(new AlbumNotFoundException()))
.flatMap(albumEntity -> {
if (!albumEntity.getOwnerMemberId().equals(requestMemberId)) {
// 내 앨범이 아니면 그냥 없는 앨범 처리
return Mono.error(new AlbumNotFoundException());
} else {
return albumService.decreaseAlbumPhotoCount(photoEntity.getAlbumId(), requestMemberId)
.then(albumService.increaseAlbumPhotoCount(albumId, requestMemberId))
.then(photoRepository.save(photoEntity.updateAlbumId(albumId)));
}
});
}

return albumRepository
.findById(albumId)
.switchIfEmpty(Mono.error(new AlbumNotFoundException()))
.flatMap(albumEntity -> {
if (!albumEntity.getOwnerMemberId().equals(requestMemberId)) {
// 내 앨범이 아니면 그냥 없는 앨범 처리
return Mono.error(new AlbumNotFoundException());
}

return albumService.decreaseAlbumPhotoCount(photoEntity.getAlbumId(), requestMemberId)
.then(albumService.increaseAlbumPhotoCount(albumId, requestMemberId))
.then(photoRepository.save(photoEntity.updateAlbumId(albumId)));
});
});
}

Expand Down

0 comments on commit 84bef84

Please sign in to comment.