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 e2deab1 commit bc92f0d
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 33 deletions.
15 changes: 15 additions & 0 deletions src/main/kotlin/com/soon/common/application/ItemCommandService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.soon.common.application

import com.soon.common.application.model.ItemCreateCommand
import com.soon.common.domain.item.Item
import com.soon.common.domain.item.ItemRepository
import org.springframework.stereotype.Service

@Service
class ItemCommandService(
private val itemRepository: ItemRepository,
) {
suspend fun createItem(command: ItemCreateCommand) {
itemRepository.save(Item.create(command.toCreateModel()))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.soon.common.application.model

import com.soon.common.domain.item.model.ItemCreateModel

data class ItemCreateCommand(
val serviceNo: Int,
val title: String,
val description: String,
) {
fun toCreateModel() = ItemCreateModel(
serviceNo = serviceNo,
title = title,
description = description,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,4 @@ import kotlin.coroutines.CoroutineContext
val boundedElasticDispatcher= Schedulers.boundedElastic().asCoroutineDispatcher()

val boundedElasticScope:CoroutineScope
get()= CoroutineScope(boundedElasticDispatcher)

suspend fun <T> TransactionTemplate.executeWithContext(
context: CoroutineContext= boundedElasticDispatcher,
readOnly: Boolean=false,
block:CoroutineScope.() -> T,
): T= withContext(context){
this@executeWithContext.execute {
if(readOnly) TransactionSynchronizationManager.setCurrentTransactionReadOnly(true)
block()
} as T
}
get()= CoroutineScope(boundedElasticDispatcher)

This file was deleted.

34 changes: 34 additions & 0 deletions src/main/kotlin/com/soon/common/domain/item/Item.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.soon.common.domain.item

import com.soon.common.domain.item.model.ItemCreateModel
import jakarta.persistence.*
import java.time.LocalDateTime

@Entity
@Table(name = "item")
class Item(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "item_no")
val no: Int = 0,

@Column(name = "service_no")
val serviceNo: Int,

@Column(name = "title")
val title: String,

@Column(name = "description")
val description: String,
) {
@Column(name = "created_at")
val createdAt: LocalDateTime = LocalDateTime.now()

companion object {
fun create(createModel: ItemCreateModel) = Item(
serviceNo = createModel.serviceNo,
title = createModel.title,
description = createModel.description,
)
}
}
5 changes: 5 additions & 0 deletions src/main/kotlin/com/soon/common/domain/item/ItemRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.soon.common.domain.item

import org.springframework.data.jpa.repository.JpaRepository

interface ItemRepository : JpaRepository<Item, Int>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.soon.common.domain.item.model

data class ItemCreateModel(
val serviceNo: Int,
val title: String,
val description: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.soon.common.presentation.handler

import com.soon.common.application.ItemCommandService
import com.soon.common.application.util.coroutines.ApplicationDispatchers
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

@Service
class ItemHandler(
private val itemCommandService: ItemCommandService
) {
suspend fun createItem(request: ServerRequest): ServerResponse = withContext(ApplicationDispatchers.IO) {
val serviceHeader = request.extractServiceCodeHeader()
val command = request.awaitBodyOrNull<ItemCreateRequest>()?.toCommand(serviceHeader.no)
?: throw IllegalArgumentException()

itemCommandService.createItem(command)
ServerResponse.noContent().buildAndAwait()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.soon.common.presentation.handler.model

import com.soon.common.application.model.ItemCreateCommand

data class ItemCreateRequest(
val title: String,
val description: String,
) {
fun toCommand(serviceNo: Int) = ItemCreateCommand(
serviceNo = serviceNo,
title = title,
description = description,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.soon.common.presentation.router.item

import com.soon.common.presentation.handler.ItemHandler
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.MediaType
import org.springframework.web.reactive.function.server.RouterFunction
import org.springframework.web.reactive.function.server.ServerResponse
import org.springframework.web.reactive.function.server.coRouter

@Configuration
class ItemInternalRouter(private val itemHandler: ItemHandler) {
@Bean
fun itemBackOfficeRoute(): RouterFunction<ServerResponse> {
return coRouter {
(accept(MediaType.APPLICATION_JSON) and "/internal/common/item").nest {
POST("", itemHandler::createItem)
}
}
}
}
5 changes: 2 additions & 3 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

spring:
application:
name: common
Expand All @@ -16,13 +15,13 @@ spring:
read-timeout: 4000
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:13306/common-game?autoReconnect=true&serverTimezone=Asia/Seoul&allowPublicKeyRetrieval=true
url: jdbc:mysql://localhost:13306/common_game?autoReconnect=true&serverTimezone=Asia/Seoul&allowPublicKeyRetrieval=true
username: root
password: root
cache-prep-stmts: true
jpa:
hibernate:
ddl-auto: none
ddl-auto: create
show-sql: true
properties:
hibernate:
Expand Down
11 changes: 0 additions & 11 deletions src/test/http/backoffice.http

This file was deleted.

9 changes: 9 additions & 0 deletions src/test/http/common-game.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### 아이템 생성
POST localhost:8083/common/backoffice/item
Content-Type: application/json
Service-Code:eyJubyI6MSwidGl0bGUiOiJ0aXRsZSJ9

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

0 comments on commit bc92f0d

Please sign in to comment.