Skip to content

Commit

Permalink
[Feat/#149] 로깅 API 구현 (#160)
Browse files Browse the repository at this point in the history
* feat: log ifo 테이블 추가

* feat: insertLogIfo 메서드 구현

* feat: AddApiLogUseCase 구현

* feat: addApiLogUseCase 컨트롤러 적용
  • Loading branch information
belljun3395 authored Jul 8, 2024
1 parent 72bda63 commit ba2498f
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 0 deletions.
20 changes: 20 additions & 0 deletions api-repo/src/main/kotlin/com/few/api/repo/dao/log/LogIfoDao.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.few.api.repo.dao.log

import com.few.api.repo.dao.log.command.InsertLogCommand
import jooq.jooq_dsl.tables.LogIfo
import org.jooq.DSLContext
import org.jooq.JSON
import org.springframework.stereotype.Repository

@Repository
class LogIfoDao(
private val dslContext: DSLContext
) {

fun insertLogIfo(command: InsertLogCommand): Long? {
return dslContext.insertInto(LogIfo.LOG_IFO)
.set(LogIfo.LOG_IFO.HISTORY, JSON.valueOf(command.history))
.returning(LogIfo.LOG_IFO.ID)
.fetchOne()?.id
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.few.api.repo.dao.log.command

data class InsertLogCommand(
val history: String
)
19 changes: 19 additions & 0 deletions api/src/main/kotlin/com/few/api/domain/log/AddApiLogUseCase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.few.api.domain.log

import com.few.api.domain.log.dto.AddApiLogUseCaseIn
import com.few.api.repo.dao.log.LogIfoDao
import com.few.api.repo.dao.log.command.InsertLogCommand
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional

@Component
class AddApiLogUseCase(
private val logIfoDao: LogIfoDao
) {
@Transactional
fun execute(useCaseIn: AddApiLogUseCaseIn) {
InsertLogCommand(useCaseIn.history).let {
logIfoDao.insertLogIfo(it)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.few.api.domain.log.dto

data class AddApiLogUseCaseIn(
val history: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.few.api.web.controller.admin

import com.few.api.domain.admin.document.dto.*
import com.few.api.domain.admin.document.usecase.*
import com.few.api.domain.log.AddApiLogUseCase
import com.few.api.domain.log.dto.AddApiLogUseCaseIn
import com.few.api.web.controller.admin.request.*
import com.few.api.web.support.ApiResponse
import com.few.api.web.support.ApiResponseGenerator
import org.springframework.http.HttpStatus
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@Validated
@RestController
@RequestMapping(value = ["/api/v1/logs"])
class ApiLogController(
private val addApiLogUseCase: AddApiLogUseCase
) {
@PostMapping
fun addApiLog(@RequestBody request: ApiLogRequest): ApiResponse<ApiResponse.Success> {
AddApiLogUseCaseIn(request.history).let {
addApiLogUseCase.execute(it)
}

return ApiResponseGenerator.success(HttpStatus.OK)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.few.api.web.controller.admin.request

data class ApiLogRequest(
val history: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.few.api.web.controller.admin

import com.epages.restdocs.apispec.ResourceDocumentation.resource
import com.epages.restdocs.apispec.ResourceSnippetParameters
import com.epages.restdocs.apispec.Schema
import com.fasterxml.jackson.databind.ObjectMapper
import com.few.api.domain.log.AddApiLogUseCase
import com.few.api.domain.log.dto.AddApiLogUseCaseIn
import com.few.api.web.controller.ControllerTestSpec
import com.few.api.web.controller.admin.request.ApiLogRequest
import com.few.api.web.controller.description.Description
import com.few.api.web.controller.helper.*
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post

import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.mockito.Mockito
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.http.MediaType
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status

class ApiLogControllerTest : ControllerTestSpec() {
@Autowired
private lateinit var objectMapper: ObjectMapper

@Autowired
private lateinit var mockMvc: MockMvc

@MockBean
private lateinit var addApiLogUseCase: AddApiLogUseCase

companion object {
private val BASE_URL = "/api/v1/logs"
private val TAG = "ApiLogControllerTest"
}

@Test
@DisplayName("[POST] /api/v1/logs")
fun addApiLog() {
// Given
val api = "addApiLog"
val history =
objectMapper.writeValueAsString(mapOf("from" to "email", "to" to "readArticle"))
val request = ApiLogRequest(history)
val content = objectMapper.writeValueAsString(request)
val useCaseIn = AddApiLogUseCaseIn(history)
Mockito.doNothing().`when`(addApiLogUseCase).execute(useCaseIn)

// When
mockMvc.perform(
post(BASE_URL)
.content(content)
.contentType(MediaType.APPLICATION_JSON)
).andExpect(
status().is2xxSuccessful
).andDo(
document(
api.toIdentifier(),
resource(
ResourceSnippetParameters.builder()
.summary(api.toIdentifier())
.description("API 로그를 기록")
.tag(TAG)
.requestSchema(Schema.schema(api.toRequestSchema()))
.responseSchema(Schema.schema(api.toResponseSchema())).responseFields(
*Description.describe()
).build()
)
)
)
}
}
7 changes: 7 additions & 0 deletions data/db/migration/entity/V1.00.0.6__log_ifo_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- 로그 기록 테이블
CREATE TABLE LOG_IFO
(
id BIGINT PRIMARY KEY AUTO_INCREMENT,
history JSON NOT NULL DEFAULT (JSON_OBJECT()),
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

0 comments on commit ba2498f

Please sign in to comment.