Skip to content

Commit

Permalink
feat: add admin apis
Browse files Browse the repository at this point in the history
  • Loading branch information
CChuYong committed Sep 8, 2024
1 parent 264cdf5 commit 07e8310
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
2 changes: 1 addition & 1 deletion admin-service/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ repositories {

dependencies {
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.springframework.boot:spring-boot-starter-security")
//implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package kr.mafoo.admin.config

import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.codec.ClientCodecConfigurer
import org.springframework.web.reactive.function.client.WebClient

@Configuration
class WebClientConfig {
class WebClientConfig(
@Value("\${app.url.user-service}") private val userServiceUrl: String,
@Value("\${app.url.photo-service}") private val photoServiceUrl: String,
) {
@Bean("userServiceWebClient")
fun userServiceWebClient(): WebClient {
return WebClient
.builder()
.baseUrl("http://user-serivce")
.baseUrl(userServiceUrl)
.codecs { clientCodecConfigurer: ClientCodecConfigurer ->
clientCodecConfigurer
.defaultCodecs()
Expand All @@ -24,7 +28,7 @@ class WebClientConfig {
fun photoServiceWebClient(): WebClient {
return WebClient
.builder()
.baseUrl("http://photo-serivce")
.baseUrl(photoServiceUrl)
.codecs { clientCodecConfigurer: ClientCodecConfigurer ->
clientCodecConfigurer
.defaultCodecs()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package kr.mafoo.admin.controller

import kotlinx.coroutines.reactor.awaitSingle
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.core.io.buffer.DataBuffer
import org.springframework.http.MediaType
import org.springframework.http.client.MultipartBodyBuilder
import org.springframework.http.codec.multipart.FilePart
import org.springframework.util.MultiValueMap
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestPart
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.reactive.function.BodyInserters
import org.springframework.web.reactive.function.client.WebClient
import reactor.core.publisher.Flux

Expand All @@ -15,18 +20,58 @@ import reactor.core.publisher.Flux
class AdminController(
@Qualifier("photoServiceWebClient") private val photoWebClient: WebClient,
) {
@PostMapping("/upload-image", consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
companion object {
const val MEMBER_ID_HEADER_KEY = "X-MEMBER-ID"
}

@PostMapping("/upload-image-with-new-album",
consumes = [MediaType.MULTIPART_FORM_DATA_VALUE],
produces = [MediaType.APPLICATION_JSON_VALUE]
)
suspend fun uploadImage(
memberId: String,
@RequestPart("files") files: Flux<FilePart>
) {
photoWebClient
@RequestPart("memberId") memberId: String,
@RequestPart("files") files: Flux<FilePart>,
@RequestPart("albumName") albumName: String,
): Flux<LinkedHashMap<*, *>> {
// #1. upload photos
val multipartBuilder = MultipartBodyBuilder()
files.collectList().awaitSingle().forEach {
multipartBuilder
.asyncPart("files", it.content(), DataBuffer::class.java)
.filename(it.filename())
}

val uploadedPhotos = photoWebClient
.post()
.uri("/v1/photos/files")
.header("X-MEMBER-ID", memberId)
.header(MEMBER_ID_HEADER_KEY, memberId)
.contentType(MediaType.MULTIPART_FORM_DATA)
.bodyValue(files)
.body(BodyInserters.fromMultipartData(multipartBuilder.build()))
.retrieve()
.bodyToFlux(LinkedHashMap::class.java)
.collectList()
.awaitSingle()

// #2. create album
val newAlbum = photoWebClient
.post()
.uri("/v1/albums")
.header(MEMBER_ID_HEADER_KEY, memberId)
.bodyValue(mapOf("name" to albumName, "type" to "HEART"))
.retrieve()
.bodyToMono(LinkedHashMap::class.java)
.awaitSingle()

val albumId = newAlbum["albumId"] ?: throw RuntimeException()

// #3. bulk update album ids
val photoIds = uploadedPhotos.map { it["photoId"] ?: throw RuntimeException() }
return photoWebClient
.patch()
.uri("/v1/photos/bulk/album")
.header(MEMBER_ID_HEADER_KEY, memberId)
.bodyValue(mapOf("albumId" to albumId, "photoIds" to photoIds))
.retrieve()
.bodyToFlux(LinkedHashMap::class.java)
}
}
5 changes: 5 additions & 0 deletions admin-service/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ management:
web:
exposure:
include: health,metrics,prometheus

app:
url:
photo-service: http://photo-service
user-service: http://user-service

0 comments on commit 07e8310

Please sign in to comment.