Skip to content

Commit

Permalink
feat: add photo & admin apis
Browse files Browse the repository at this point in the history
  • Loading branch information
CChuYong committed Sep 8, 2024
1 parent 42d6a50 commit f4af14a
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 1 deletion.
45 changes: 45 additions & 0 deletions photo-service/src/main/java/kr/mafoo/photo/api/AdminApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package kr.mafoo.photo.api;

import kr.mafoo.photo.controller.dto.response.AlbumRawResponse;
import kr.mafoo.photo.controller.dto.response.PageResponse;
import kr.mafoo.photo.controller.dto.response.PhotoResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import reactor.core.publisher.Mono;

@RequestMapping("/v1/admin")
public interface AdminApi {
@GetMapping("/albums")
Mono<PageResponse<AlbumRawResponse>> queryAlbums(
@RequestParam(defaultValue = "1")
int page,

@RequestParam(defaultValue = "10")
int size,

@RequestParam(required = false)
String name,

@RequestParam(required = false)
String type,

@RequestParam(required = false)
String ownerMemberId
);

@GetMapping("/photos")
Mono<PageResponse<PhotoResponse>> getPhotos(
@RequestParam(defaultValue = "1")
int page,

@RequestParam(defaultValue = "10")
int size,

@RequestParam(required = false)
String type,

@RequestParam(required = false)
String ownerMemberId
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package kr.mafoo.photo.controller;

import kr.mafoo.photo.api.AdminApi;
import kr.mafoo.photo.controller.dto.response.AlbumRawResponse;
import kr.mafoo.photo.controller.dto.response.PageResponse;
import kr.mafoo.photo.controller.dto.response.PhotoResponse;
import kr.mafoo.photo.service.AlbumAdminService;
import kr.mafoo.photo.service.PhotoAdminService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@RequiredArgsConstructor
@RestController
public class AdminController implements AdminApi {
private final AlbumAdminService albumAdminService;
private final PhotoAdminService photoAdminService;

@Override
public Mono<PageResponse<AlbumRawResponse>> queryAlbums(int page, int size, String name, String type, String ownerMemberId) {
return albumAdminService
.queryAlbums(page, size, name, type, ownerMemberId)
.map(pageResponse -> pageResponse.map(AlbumRawResponse::fromEntity));
}

@Override
public Mono<PageResponse<PhotoResponse>> getPhotos(int page, int size, String type, String ownerMemberId) {
return photoAdminService
.queryPhotos(page, size, type, ownerMemberId)
.map(pageResponse -> pageResponse.map(PhotoResponse::fromEntity));
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package kr.mafoo.photo.controller.dto.response;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import kr.mafoo.photo.domain.AlbumEntity;
import kr.mafoo.photo.domain.AlbumType;

import java.time.LocalDateTime;

@Schema(description = "앨범 응답")
public record AlbumRawResponse(
@Schema(description = "앨범 ID", example = "test_album_id")
String albumId,

@Schema(description = "앨범 이름", example = "야뿌들")
String name,

@Schema(description = "앨범 종류", example = "TYPE_B")
AlbumType type,

@Schema(description = "앨범 내 사진 수", example = "6")
String photoCount,

@Schema(description = "앨범 주인 ID", example = "6")
String ownerMemberId,

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Seoul")
@Schema(description = "생성 시간", example = "2021-08-01 00:00:00")
LocalDateTime createdAt
) {
public static AlbumRawResponse fromEntity(AlbumEntity entity) {
return new AlbumRawResponse(
entity.getAlbumId(),
entity.getName(),
entity.getType(),
entity.getPhotoCount().toString(),
entity.getOwnerMemberId(),
entity.getCreatedAt()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package kr.mafoo.photo.controller.dto.response;

import java.util.Collection;
import java.util.function.Function;

public record PageResponse<T>(
Collection<T> results,
Integer page,
Integer size,
Integer totalElement
) {
public <R> PageResponse<R> map(Function<T, R> mapper) {
return new PageResponse<>(
results.stream().map(mapper).toList(),
page,
size,
totalElement
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package kr.mafoo.photo.repository;

import kr.mafoo.photo.domain.AlbumEntity;
import kr.mafoo.photo.domain.AlbumType;
import org.springframework.data.domain.Pageable;
import org.springframework.data.r2dbc.repository.Modifying;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.r2dbc.repository.R2dbcRepository;
Expand All @@ -23,4 +25,11 @@ public interface AlbumRepository extends R2dbcRepository<AlbumEntity, String> {
@Query("UPDATE album SET display_index = display_index -1 WHERE owner_member_id = :ownerMemberId " +
"AND display_index BETWEEN :startIndex AND :lastIndex")
Mono<Void> popDisplayIndexBetween(String ownerMemberId, Integer startIndex, Integer lastIndex);

Flux<AlbumEntity> findAllByOrderByAlbumIdDesc(Pageable pageable);

Flux<AlbumEntity> findAllByNameOrderByAlbumIdDesc(String name, Pageable pageable);
Flux<AlbumEntity> findAllByTypeOrderByAlbumIdDesc(AlbumType type, Pageable pageable);

Flux<AlbumEntity> findAllByOwnerMemberIdOrderByAlbumIdDesc(String ownerMemberId, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package kr.mafoo.photo.repository;

import kr.mafoo.photo.domain.BrandType;
import kr.mafoo.photo.domain.PhotoEntity;
import org.springframework.data.domain.Pageable;
import org.springframework.data.r2dbc.repository.Modifying;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.r2dbc.repository.R2dbcRepository;
Expand All @@ -21,4 +23,8 @@ public interface PhotoRepository extends R2dbcRepository<PhotoEntity, String> {
@Modifying
@Query("UPDATE photo SET display_index = display_index + 1 WHERE album_id = :albumId AND display_index BETWEEN :startIndex AND :endIndex")
Mono<Void> pushDisplayIndexBetween(String albumId, int startIndex, int endIndex);
}

Flux<PhotoEntity> findAllByOrderByPhotoIdDesc(Pageable pageable);
Flux<PhotoEntity> findAllByBrandOrderByPhotoIdDesc(BrandType brandType, Pageable pageable);
Flux<PhotoEntity> findAllByOwnerMemberIdOrderByPhotoIdDesc(String ownerMemberId, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package kr.mafoo.photo.service;

import kr.mafoo.photo.controller.dto.response.PageResponse;
import kr.mafoo.photo.domain.AlbumEntity;
import kr.mafoo.photo.domain.AlbumType;
import kr.mafoo.photo.repository.AlbumRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RequiredArgsConstructor
@Service
public class AlbumAdminService {
private final AlbumRepository albumRepository;

public Mono<PageResponse<AlbumEntity>> queryAlbums(int page, int size, String name, String type, String ownerMemberId) {
Flux<AlbumEntity> publisher;
if(name != null) {
publisher = albumRepository.findAllByNameOrderByAlbumIdDesc(name, PageRequest.of(page, size));
} else if(type != null) {
AlbumType albumType;
try {
albumType = AlbumType.valueOf(type);
publisher = albumRepository.findAllByTypeOrderByAlbumIdDesc(albumType, PageRequest.of(page, size));
} catch (IllegalArgumentException e) {
publisher = albumRepository.findAllByOrderByAlbumIdDesc(PageRequest.of(page, size));
}
} else if(ownerMemberId != null) {
publisher = albumRepository.findAllByOwnerMemberIdOrderByAlbumIdDesc(ownerMemberId, PageRequest.of(page, size));
} else {
publisher = albumRepository.findAllByOrderByAlbumIdDesc(PageRequest.of(page, size));
}
return publisher
.collectList()
.zipWith(albumRepository.count())
.map(tuple -> new PageResponse<>(tuple.getT1(), page, size, tuple.getT2().intValue()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package kr.mafoo.photo.service;

import kr.mafoo.photo.controller.dto.response.PageResponse;
import kr.mafoo.photo.domain.BrandType;
import kr.mafoo.photo.domain.PhotoEntity;
import kr.mafoo.photo.repository.PhotoRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RequiredArgsConstructor
@Service
public class PhotoAdminService {
private final PhotoRepository photoRepository;

public Mono<PageResponse<PhotoEntity>> queryPhotos(int page, int size, String type, String ownerMemberId) {
Flux<PhotoEntity> publisher;
if(type != null) {
BrandType brandType;
try {
brandType = BrandType.valueOf(type);
publisher = photoRepository.findAllByBrandOrderByPhotoIdDesc(brandType, PageRequest.of(page, size));
} catch (IllegalArgumentException e) {
publisher = photoRepository.findAllByOrderByPhotoIdDesc(PageRequest.of(page, size));
}
} else if(ownerMemberId != null) {
publisher = photoRepository.findAllByOwnerMemberIdOrderByPhotoIdDesc(ownerMemberId, PageRequest.of(page, size));
} else {
publisher = photoRepository.findAllByOrderByPhotoIdDesc(PageRequest.of(page, size));
}
return publisher
.collectList()
.zipWith(photoRepository.count())
.map(tuple -> new PageResponse<>(tuple.getT1(), page, size, tuple.getT2().intValue()));
}
}

0 comments on commit f4af14a

Please sign in to comment.