Repository navigation
feat/BACKEND-312 어드민에서 카테고리별 검색 사전을 관리하고 버전을 S3 로 발행·롤백한다 #401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| package com.neki.admin.dictionary.api.controller | ||
|
|
||
| import com.neki.admin.dictionary.api.dto.DictionaryAdminConverter | ||
| import com.neki.admin.dictionary.api.dto.DictionaryAdminDto | ||
| import com.neki.admin.dictionary.application.AdminDictionaryFacade | ||
| import com.neki.core.api.dto.BaseResponse | ||
| import com.neki.domain.dictionary.dto.DictionaryCommand | ||
| import io.swagger.v3.oas.annotations.Operation | ||
| import jakarta.validation.Valid | ||
| import org.springframework.http.HttpStatus | ||
| import org.springframework.web.bind.annotation.DeleteMapping | ||
| import org.springframework.web.bind.annotation.GetMapping | ||
| import org.springframework.web.bind.annotation.PatchMapping | ||
| import org.springframework.web.bind.annotation.PathVariable | ||
| import org.springframework.web.bind.annotation.PostMapping | ||
| import org.springframework.web.bind.annotation.PutMapping | ||
| import org.springframework.web.bind.annotation.RequestBody | ||
| import org.springframework.web.bind.annotation.RequestMapping | ||
| import org.springframework.web.bind.annotation.ResponseStatus | ||
| import org.springframework.web.bind.annotation.RestController | ||
|
|
||
| /** | ||
| * 검색 사전 (카테고리 -> 키워드 -> 동의어·오타·축약어). 글자는 앞뒤 공백만 지우고 입력 그대로 저장한다. | ||
| * 저장한 내용은 버전을 만들어 발행해야 S3 에 반영된다 (PublicationAdminController). | ||
| */ | ||
| @RestController | ||
| @RequestMapping("/admin/dictionary") | ||
| class DictionaryAdminController(private val facade: AdminDictionaryFacade) { | ||
|
|
||
| @Operation(summary = "카테고리 목록", description = "id 오름차순. keywordCount 는 카테고리의 키워드 수다.") | ||
| @GetMapping("/categories") | ||
| fun getCategories(): BaseResponse<List<DictionaryAdminDto.Response.CategoryInfo>> = | ||
| BaseResponse(data = facade.getCategories().map { DictionaryAdminConverter.toResponse(it) }) | ||
|
|
||
| @Operation(summary = "카테고리 추가", description = "이름은 앞뒤 공백을 지운 뒤 1~50자. 이미 있는 이름이면 409 D-20 이다.") | ||
| @ResponseStatus(HttpStatus.CREATED) | ||
| @PostMapping("/categories") | ||
| fun addCategory( | ||
| @RequestBody request: DictionaryAdminDto.Request.SaveCategory, | ||
| ): BaseResponse<DictionaryAdminDto.Response.CategoryInfo> = BaseResponse( | ||
| data = DictionaryAdminConverter.toResponse( | ||
| facade.addCategory(DictionaryAdminConverter.toAddCommand(request)), | ||
| ), | ||
| ) | ||
|
|
||
| @Operation(summary = "카테고리 이름 변경", description = "이미 있는 이름이면 409 D-20, 없는 카테고리면 400 D-04 다.") | ||
| @PatchMapping("/categories/{categoryId}") | ||
| fun renameCategory( | ||
| @PathVariable categoryId: Long, | ||
| @RequestBody request: DictionaryAdminDto.Request.SaveCategory, | ||
| ): BaseResponse<DictionaryAdminDto.Response.CategoryInfo> = BaseResponse( | ||
| data = DictionaryAdminConverter.toResponse( | ||
| facade.renameCategory(DictionaryAdminConverter.toRenameCommand(categoryId, request)), | ||
| ), | ||
| ) | ||
|
|
||
| @Operation(summary = "카테고리 삭제", description = "키워드가 있으면 400 D-21 이다.") | ||
| @DeleteMapping("/categories/{categoryId}") | ||
| fun deleteCategory(@PathVariable categoryId: Long): BaseResponse<Any> { | ||
| facade.deleteCategory(DictionaryCommand.DeleteCategory(categoryId)) | ||
| return BaseResponse(data = null) | ||
| } | ||
|
|
||
| @Operation( | ||
| summary = "키워드 목록", | ||
| description = "키워드 오름차순. query 를 넘기면 키워드나 변형어 중 하나라도 대소문자 무시 부분일치하는 키워드만 남긴다.", | ||
| ) | ||
| @GetMapping("/categories/{categoryId}/keywords") | ||
| fun getKeywords( | ||
| @PathVariable categoryId: Long, | ||
| @Valid request: DictionaryAdminDto.Request.GetKeywords, | ||
| ): BaseResponse<DictionaryAdminDto.Response.GetKeywords> = BaseResponse( | ||
| data = DictionaryAdminConverter.toResponse( | ||
| facade.getKeywords(DictionaryAdminConverter.toQuery(categoryId, request)), | ||
| ), | ||
| ) | ||
|
|
||
| @Operation( | ||
| summary = "키워드 추가", | ||
| description = """ | ||
| 요청 안에서 같은 글자가 두 번 나오면 400 D-01, 같은 카테고리의 다른 키워드·변형어와 글자가 겹치면 409 D-19 다. | ||
| 대소문자가 다르면 다른 글자로 본다. 다른 카테고리와는 겹쳐도 된다. | ||
| """, | ||
| ) | ||
| @ResponseStatus(HttpStatus.CREATED) | ||
| @PostMapping("/categories/{categoryId}/keywords") | ||
| fun addKeyword( | ||
| @PathVariable categoryId: Long, | ||
| @RequestBody request: DictionaryAdminDto.Request.SaveKeyword, | ||
| ): BaseResponse<DictionaryAdminDto.Response.KeywordInfo> = BaseResponse( | ||
| data = DictionaryAdminConverter.toResponse( | ||
| facade.addKeyword(DictionaryAdminConverter.toAddCommand(categoryId, request)), | ||
| ), | ||
| ) | ||
|
|
||
| @Operation( | ||
| summary = "키워드 수정", | ||
| description = "키워드 글자와 세 목록을 통째로 바꾼다. 자기 키워드의 기존 글자는 그대로 두거나 자리를 바꿔도 된다.", | ||
| ) | ||
| @PutMapping("/keywords/{keywordId}") | ||
| fun updateKeyword( | ||
| @PathVariable keywordId: Long, | ||
| @RequestBody request: DictionaryAdminDto.Request.SaveKeyword, | ||
| ): BaseResponse<DictionaryAdminDto.Response.KeywordInfo> = BaseResponse( | ||
| data = DictionaryAdminConverter.toResponse( | ||
| facade.updateKeyword(DictionaryAdminConverter.toUpdateCommand(keywordId, request)), | ||
| ), | ||
| ) | ||
|
Comment on lines
+100
to
+108
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 키워드 동시 수정 시 변형어 목록이 합쳐질 수 있습니다.
변형어 삭제 전에 키워드 행을 잠그세요. 예를 들어 영향 범위는 관리자 기능에 한정됩니다. 같은 키워드를 동시에 수정할 때만 발생하며, 다음 수정으로 복구할 수 있습니다.
|
||
|
|
||
| @Operation(summary = "키워드 삭제", description = "변형어도 함께 지운다.") | ||
| @DeleteMapping("/keywords/{keywordId}") | ||
| fun deleteKeyword(@PathVariable keywordId: Long): BaseResponse<Any> { | ||
| facade.deleteKeyword(DictionaryCommand.DeleteKeyword(keywordId)) | ||
| return BaseResponse(data = null) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package com.neki.admin.dictionary.api.dto | ||
|
|
||
| import com.neki.admin.dictionary.application.dto.AdminDictionaryResult | ||
| import com.neki.core.domain.vo.Pagination | ||
| import com.neki.domain.dictionary.dto.DictionaryCommand | ||
| import com.neki.domain.dictionary.dto.DictionaryQuery | ||
|
|
||
| object DictionaryAdminConverter { | ||
| fun toAddCommand(request: DictionaryAdminDto.Request.SaveCategory): DictionaryCommand.AddCategory = | ||
| DictionaryCommand.AddCategory(request.name) | ||
|
|
||
| fun toRenameCommand( | ||
| categoryId: Long, | ||
| request: DictionaryAdminDto.Request.SaveCategory, | ||
| ): DictionaryCommand.RenameCategory = DictionaryCommand.RenameCategory(categoryId, request.name) | ||
|
|
||
| fun toQuery(categoryId: Long, request: DictionaryAdminDto.Request.GetKeywords): DictionaryQuery.GetKeywords = | ||
| DictionaryQuery.GetKeywords(categoryId, request.query, Pagination(request.page, request.size)) | ||
|
|
||
| fun toAddCommand(categoryId: Long, request: DictionaryAdminDto.Request.SaveKeyword): DictionaryCommand.AddKeyword = | ||
| DictionaryCommand.AddKeyword( | ||
| categoryId, | ||
| request.keyword, | ||
| terms(request.synonyms), | ||
| terms(request.typos), | ||
| terms(request.abbreviations), | ||
| ) | ||
|
|
||
| fun toUpdateCommand( | ||
| keywordId: Long, | ||
| request: DictionaryAdminDto.Request.SaveKeyword, | ||
| ): DictionaryCommand.UpdateKeyword = DictionaryCommand.UpdateKeyword( | ||
| keywordId, | ||
| request.keyword, | ||
| terms(request.synonyms), | ||
| terms(request.typos), | ||
| terms(request.abbreviations), | ||
| ) | ||
|
|
||
| fun toResponse(result: AdminDictionaryResult.CategoryInfo): DictionaryAdminDto.Response.CategoryInfo = | ||
| DictionaryAdminDto.Response.CategoryInfo(result.id, result.name, result.keywordCount) | ||
|
|
||
| fun toResponse(result: AdminDictionaryResult.GetKeywords): DictionaryAdminDto.Response.GetKeywords = | ||
| DictionaryAdminDto.Response.GetKeywords( | ||
| result.totalCount, | ||
| result.totalPages, | ||
| result.keywords.map { | ||
| toResponse(it) | ||
| }, | ||
| ) | ||
|
|
||
| fun toResponse(result: AdminDictionaryResult.KeywordInfo): DictionaryAdminDto.Response.KeywordInfo = | ||
| DictionaryAdminDto.Response.KeywordInfo( | ||
| id = result.id, | ||
| keyword = result.keyword, | ||
| synonyms = result.synonyms, | ||
| typos = result.typos, | ||
| abbreviations = result.abbreviations, | ||
| updatedAt = result.updatedAt, | ||
| ) | ||
|
|
||
| // null 원소는 빈 글자로 넘겨 도메인이 400 으로 거르게 한다 | ||
| private fun terms(values: List<String?>): List<String> = values.map { it ?: "" } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package com.neki.admin.dictionary.api.dto | ||
|
|
||
| import jakarta.validation.constraints.Max | ||
| import jakarta.validation.constraints.Min | ||
| import java.time.LocalDateTime | ||
|
|
||
| /** | ||
| * 글자 입력은 앞뒤 공백만 지우고 그대로 저장한다. 빈 글자·길이·개수·요청 안 중복은 도메인이 400 D-01 로 거른다. | ||
| */ | ||
| object DictionaryAdminDto { | ||
| class Request { | ||
| // 앞뒤 공백을 지운 뒤 1~50자 | ||
| data class SaveCategory(val name: String) | ||
|
|
||
| data class GetKeywords( | ||
| // 앞뒤 공백을 지우고 비면 거르지 않는다 | ||
| val query: String? = null, | ||
| @field:Min(value = 0, message = "page는 0 이상이어야 합니다.") | ||
| val page: Int = DEFAULT_PAGE, | ||
| @field:Min(value = 1, message = "size는 1 이상이어야 합니다.") | ||
| @field:Max(value = MAX_SIZE, message = "size는 {value} 이하여야 합니다.") | ||
| val size: Int = DEFAULT_SIZE, | ||
| ) | ||
|
|
||
| /** | ||
| * 각 글자는 앞뒤 공백을 지운 뒤 1~100자, 목록마다 50개 이하. 키워드와 세 목록을 통틀어 같은 글자가 두 번 나오면 400 이다. | ||
| * null 원소는 빈 글자로 보아 400 이다. | ||
| */ | ||
| data class SaveKeyword( | ||
| val keyword: String, | ||
| val synonyms: List<String?> = emptyList(), | ||
| val typos: List<String?> = emptyList(), | ||
| val abbreviations: List<String?> = emptyList(), | ||
| ) | ||
|
|
||
| companion object { | ||
| const val DEFAULT_PAGE = 0 | ||
| const val DEFAULT_SIZE = 20 | ||
| const val MAX_SIZE = 100L | ||
| } | ||
| } | ||
|
|
||
| class Response { | ||
| data class CategoryInfo(val id: Long, val name: String, val keywordCount: Long) | ||
|
|
||
| // totalCount 는 현재 페이지가 아니라 조건에 맞는 전체 키워드 수다 | ||
| data class GetKeywords(val totalCount: Long, val totalPages: Int, val keywords: List<KeywordInfo>) | ||
|
|
||
| // 변형어 목록은 등록순 | ||
| data class KeywordInfo( | ||
| val id: Long, | ||
| val keyword: String, | ||
| val synonyms: List<String>, | ||
| val typos: List<String>, | ||
| val abbreviations: List<String>, | ||
| val updatedAt: LocalDateTime, | ||
| ) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
동시 키워드 추가 중 카테고리 삭제가 D-21 대신 일반 오류를 반환할 수 있습니다.
DictionaryService.deleteCategory는existsTermInCategory로 키워드 유무를 먼저 확인한 뒤 삭제합니다. 두 단계 사이에 잠금이 없습니다. 같은 카테고리에 키워드를 추가하는 요청이 이 확인 직후 커밋되면, 삭제 시점에 외래 키 위반이 발생할 수 있습니다.DictionaryRepositoryAdapter.deleteCategory는categoryRepository.delete(category)만 호출합니다. 이 호출은 flush를 하지 않습니다. DELETE SQL은AdminDictionaryFacade.deleteCategory의 트랜잭션이 커밋될 때 실행됩니다. 이때 발생한DataIntegrityViolationException은conflictAs로 감싸져 있지 않으므로DICTIONARY_CATEGORY_NOT_EMPTY로 변환되지 않습니다. 호출자는 문서화된 D-21 대신 다른 오류 응답을 받습니다.삭제 직후 flush를 실행하고, 삭제 호출을
conflictAs로 감싸 주세요. 그러면 삭제 중 발생한 외래 키 위반이 D-21로 변환됩니다.제안 수정
domain/src/main/kotlin/com/neki/domain/dictionary/infra/persist/DictionaryRepositoryAdapter.ktdomain/src/main/kotlin/com/neki/domain/dictionary/service/DictionaryService.ktSource: Learnings