-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
234 additions
and
1 deletion.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
photo-service/src/main/java/kr/mafoo/photo/api/AdminApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} |
34 changes: 34 additions & 0 deletions
34
photo-service/src/main/java/kr/mafoo/photo/controller/AdminController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
|
||
} |
41 changes: 41 additions & 0 deletions
41
photo-service/src/main/java/kr/mafoo/photo/controller/dto/response/AlbumRawResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
photo-service/src/main/java/kr/mafoo/photo/controller/dto/response/PageResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
photo-service/src/main/java/kr/mafoo/photo/service/AlbumAdminService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
photo-service/src/main/java/kr/mafoo/photo/service/PhotoAdminService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |