Skip to content

Commit

Permalink
feat: Admin 컨트롤러 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
belljun3395 committed Jul 1, 2024
1 parent fa127f3 commit a4857fa
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.few.api.web.controller.admin

import com.few.api.domain.admin.document.dto.*
import com.few.api.domain.admin.document.usecase.AddArticleUseCase
import com.few.api.domain.admin.document.usecase.AddWorkbookUseCase
import com.few.api.domain.admin.document.usecase.ConvertContentUseCase
import com.few.api.domain.admin.document.usecase.MapArticleUseCase
import com.few.api.web.controller.admin.request.AddArticleRequest
import com.few.api.web.controller.admin.request.AddWorkbookRequest
import com.few.api.web.controller.admin.request.ConvertContentRequest
import com.few.api.web.controller.admin.request.MapArticleRequest
import com.few.api.web.controller.admin.response.AddArticleResponse
import com.few.api.web.controller.admin.response.AddWorkbookResponse
import com.few.api.web.controller.admin.response.ConvertContentResponse
import com.few.api.web.support.ApiResponse
import com.few.api.web.support.ApiResponseGenerator
import com.few.api.web.support.MessageCode
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
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/admin"])
class AdminController(
private val addArticleUseCase: AddArticleUseCase,
private val addWorkbookUseCase: AddWorkbookUseCase,
private val mapArticleUseCase: MapArticleUseCase,
private val convertContentUseCase: ConvertContentUseCase
) {
@PostMapping("/workbooks")
fun addWorkbook(@RequestBody request: AddWorkbookRequest): ApiResponse<ApiResponse.SuccessBody<AddWorkbookResponse>> {
val useCaseOut = AddWorkbookUseCaseIn(
title = request.title,
mainImageUrl = request.mainImageUrl,
category = request.category,
description = request.description
).let {
addWorkbookUseCase.execute(it)
}

return AddWorkbookResponse(useCaseOut.workbookId).let {
ApiResponseGenerator.success(it, HttpStatus.OK)
}
}

@PostMapping("/articles")
fun addArticle(
@RequestBody request: AddArticleRequest
): ApiResponse<ApiResponse.SuccessBody<AddArticleResponse>> {
val useCaseOut = AddArticleUseCaseIn(
writerEmail = request.writerEmail,
articleImageUrl = request.articleImageUrl,
title = request.title,
category = request.category,
contentSource = request.contentSource,
problemData = ProblemDetail(
title = request.problemData.title,
contents = request.problemData.contents.map {
ProblemContentDetail(
number = it.number,
content = it.content
)
},
answer = request.problemData.answer,
explanation = request.problemData.explanation
)
).let { useCaseIn ->
addArticleUseCase.execute(useCaseIn)
}

return AddArticleResponse(useCaseOut).let {
ApiResponseGenerator.success(it, HttpStatus.OK)
}
}

@PostMapping("/relations/articles")
fun mapArticle(@RequestBody request: MapArticleRequest): ApiResponse<ApiResponse.Success> {
MapArticleUseCaseIn(
workbookId = request.workbookId,
articleId = request.articleId,
dayCol = request.dayCol
).let {
mapArticleUseCase.execute(it)
}

return ApiResponseGenerator.success(HttpStatus.OK, MessageCode.RESOURCE_CREATED)
}

@PostMapping("/utilities/conversion/content", consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
fun convertContent(
request: ConvertContentRequest
): ApiResponse<ApiResponse.SuccessBody<ConvertContentResponse>> {
val useCaseOut = ConvertContentUseCaseIn(request.content).let {
convertContentUseCase.execute(it)
}

ConvertContentResponse(useCaseOut.content, useCaseOut.originDownLoadUrl).let {
return ApiResponseGenerator.success(it, HttpStatus.OK)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.few.api.web.controller.admin.request

import jakarta.validation.constraints.NotBlank
import java.net.URL

data class AddArticleRequest(
/** Article MST */
@field:NotBlank(message = "{email.notblank}")
val writerEmail: String,
@field:NotBlank(message = "{image.url.notblank}")
val articleImageUrl: URL,
@field:NotBlank(message = "{title.notblank}")
val title: String,
@field:NotBlank(message = "{category.notblank}")
val category: String,
/** Article IFO */
@field:NotBlank(message = "{content.source.notblank}")
val contentSource: String,
val problemData: ProblemDto
)

data class ProblemDto(
@field:NotBlank(message = "{problem.title.notblank}")
val title: String,
val contents: List<ProblemContentDto>,
@field:NotBlank(message = "{problem.answer.notblank}")
val answer: String,
@field:NotBlank(message = "{problem.explanation.notblank}")
val explanation: String
)
data class ProblemContentDto(
val number: Long,
val content: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.few.api.web.controller.admin.request

import jakarta.validation.constraints.NotBlank
import java.net.URL

data class AddWorkbookRequest(
@field:NotBlank(message = "{workbook.title.notblank}")
val title: String,
@field:NotBlank(message = "{image.url.notblank}")
val mainImageUrl: URL,
@field:NotBlank(message = "{category.notblank}")
val category: String,
@field:NotBlank(message = "{workbook.description.notblank}")
val description: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.few.api.web.controller.admin.request

import org.springframework.web.multipart.MultipartFile

data class ConvertContentRequest(
val content: MultipartFile
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.few.api.web.controller.admin.request

import jakarta.validation.constraints.Max
import jakarta.validation.constraints.Min
import jakarta.validation.constraints.NotBlank

data class MapArticleRequest(
@field:NotBlank(message = "{min.id}")
val workbookId: Long,
@field:NotBlank(message = "{min.id}")
val articleId: Long,
@field:Min(value = 1, message = "{min.id}")
@field:Max(value = 7, message = "{max.day}")
val dayCol: Int
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.few.api.web.controller.admin.response

import com.few.api.domain.admin.document.dto.AddArticleUseCaseOut

data class AddArticleResponse(
val articleId: Long
) {
constructor(useCaseOut: AddArticleUseCaseOut) : this(
articleId = useCaseOut.articleId
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.few.api.web.controller.admin.response

data class AddWorkbookResponse(
val workbookId: Long
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.few.api.web.controller.admin.response

import java.net.URL

data class ConvertContentResponse(
val content: String,
val originDownLoadUrl: URL
)

0 comments on commit a4857fa

Please sign in to comment.