Skip to content

Commit

Permalink
feat: #1 단건 조회 api
Browse files Browse the repository at this point in the history
  • Loading branch information
chs98412 committed Jun 15, 2024
1 parent bc92f0d commit d1a25c6
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 25 deletions.
17 changes: 17 additions & 0 deletions src/main/kotlin/com/soon/common/application/ItemQueryService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.soon.common.application

import com.soon.common.application.model.ItemGetSummary
import com.soon.common.domain.item.ItemRepository
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service

@Service
class ItemQueryService(
private val itemRepository: ItemRepository,
) {
fun getItem(itemNo: Int): ItemGetSummary {
return itemRepository.findByIdOrNull(itemNo)?.let {
ItemGetSummary.of(it)
} ?: throw IllegalArgumentException()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.soon.common.application.model

import com.soon.common.domain.item.Item

data class ItemGetSummary(
val serviceNo: Int,
val title: String,
val description: String,
) {
companion object {
fun of(item: Item) = ItemGetSummary(
serviceNo = item.serviceNo,
title = item.title,
description = item.description,
)
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.soon.member.presentation.extension
package com.soon.common.presentation.extension

import com.fasterxml.jackson.annotation.JsonProperty


data class MemberHeader
(
@JsonProperty("no")
val no: Int,
@JsonProperty("name")
val name: String,
@JsonProperty("account")
val account: String,
@JsonProperty("no")
val no: Int,
@JsonProperty("name")
val name: String,
@JsonProperty("account")
val account: String,
)
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package com.soon.member.presentation.extension
package com.soon.common.presentation.extension

import com.soon.common.domain.extension.decodeBase64ToDto
import com.soon.common.presentation.extension.ServiceHeader
import org.springframework.web.reactive.function.server.ServerRequest
import org.springframework.web.reactive.function.server.queryParamOrNull

fun ServerRequest.extractMemberCodeHeader() :MemberHeader{
fun ServerRequest.extractMemberCodeHeader(): MemberHeader {
return headers().header("Member-Code").firstOrNull()
?.let{
it.decodeBase64ToDto<MemberHeader>()
}?:throw IllegalArgumentException()
?.let {
it.decodeBase64ToDto<MemberHeader>()
} ?: throw IllegalArgumentException()
}

fun ServerRequest.extractServiceCodeHeader() :ServiceHeader{
fun ServerRequest.extractServiceCodeHeader(): ServiceHeader {
return headers().header("Service-Code").firstOrNull()
?.let{
it.decodeBase64ToDto<ServiceHeader>()
}?:throw IllegalArgumentException()
}
?.let {
it.decodeBase64ToDto<ServiceHeader>()
} ?: throw IllegalArgumentException()
}

fun ServerRequest.intQueryParam(parameter: String): Int {
return queryParamOrNull(parameter)?.toIntOrNull()
?: throw IllegalArgumentException("Invalid or missing 'itemNo' query parameter")
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package com.soon.common.presentation.handler

import com.soon.common.application.ItemCommandService
import com.soon.common.application.ItemQueryService
import com.soon.common.application.util.coroutines.ApplicationDispatchers
import com.soon.common.presentation.extension.extractServiceCodeHeader
import com.soon.common.presentation.extension.intQueryParam
import com.soon.common.presentation.handler.model.ItemCreateRequest
import com.soon.member.presentation.extension.extractServiceCodeHeader
import kotlinx.coroutines.withContext
import org.springframework.stereotype.Service
import org.springframework.web.reactive.function.server.ServerRequest
import org.springframework.web.reactive.function.server.ServerResponse
import org.springframework.web.reactive.function.server.awaitBodyOrNull
import org.springframework.web.reactive.function.server.buildAndAwait
import org.springframework.web.reactive.function.server.*

@Service
class ItemHandler(
private val itemCommandService: ItemCommandService
private val itemCommandService: ItemCommandService,
private val itemQueryService: ItemQueryService,
) {
suspend fun createItem(request: ServerRequest): ServerResponse = withContext(ApplicationDispatchers.IO) {
val serviceHeader = request.extractServiceCodeHeader()
Expand All @@ -24,4 +24,8 @@ class ItemHandler(
ServerResponse.noContent().buildAndAwait()
}

suspend fun getItem(request: ServerRequest): ServerResponse = withContext(ApplicationDispatchers.IO) {
val itemNo = request.intQueryParam("itemNo")
ServerResponse.ok().bodyValueAndAwait(itemQueryService.getItem(itemNo))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ItemInternalRouter(private val itemHandler: ItemHandler) {
return coRouter {
(accept(MediaType.APPLICATION_JSON) and "/internal/common/item").nest {
POST("", itemHandler::createItem)
GET("", itemHandler::getItem)
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/test/http/common-game.http
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
### 아이템 생성
POST localhost:8083/common/backoffice/item
POST localhost:8083/internal/common/item
Content-Type: application/json
Service-Code:eyJubyI6MSwidGl0bGUiOiJ0aXRsZSJ9

{
"title": "title",
"description": "description"
}

### 아이템 조회
GET localhost:8083/internal/common/item?itemNo=1
Content-Type: application/json
Service-Code:eyJubyI6MSwidGl0bGUiOiJ0aXRsZSJ9

0 comments on commit d1a25c6

Please sign in to comment.