-
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.
* feat: log ifo 테이블 추가 * feat: insertLogIfo 메서드 구현 * feat: AddApiLogUseCase 구현 * feat: addApiLogUseCase 컨트롤러 적용
- Loading branch information
1 parent
72bda63
commit ba2498f
Showing
8 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
api-repo/src/main/kotlin/com/few/api/repo/dao/log/LogIfoDao.kt
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 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 | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
api-repo/src/main/kotlin/com/few/api/repo/dao/log/command/InsertLogCommand.kt
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,5 @@ | ||
package com.few.api.repo.dao.log.command | ||
|
||
data class InsertLogCommand( | ||
val history: String | ||
) |
19 changes: 19 additions & 0 deletions
19
api/src/main/kotlin/com/few/api/domain/log/AddApiLogUseCase.kt
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,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) | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
api/src/main/kotlin/com/few/api/domain/log/dto/AddApiLogUseCaseIn.kt
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,5 @@ | ||
package com.few.api.domain.log.dto | ||
|
||
data class AddApiLogUseCaseIn( | ||
val history: String | ||
) |
31 changes: 31 additions & 0 deletions
31
api/src/main/kotlin/com/few/api/web/controller/admin/ApiLogController.kt
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,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) | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
api/src/main/kotlin/com/few/api/web/controller/admin/request/ApiLogRequest.kt
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,5 @@ | ||
package com.few.api.web.controller.admin.request | ||
|
||
data class ApiLogRequest( | ||
val history: String | ||
) |
75 changes: 75 additions & 0 deletions
75
api/src/test/kotlin/com/few/api/web/controller/admin/ApiLogControllerTest.kt
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,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() | ||
) | ||
) | ||
) | ||
} | ||
} |
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,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 | ||
); |