-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: log dependenct μΆκ° * feat: μμΈ ν΄λμ€ μμ * feat: spring-data-commons λνλμ μΆκ° * feat: μν°ν° μμ * feat: μΉ΄ν κ³ λ¦¬ CRUD ꡬν
- Loading branch information
Showing
27 changed files
with
510 additions
and
33 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
adapters/in-web/src/main/kotlin/com/pokit/category/CategoryController.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,97 @@ | ||
package com.pokit.category | ||
|
||
import com.pokit.auth.config.ErrorOperation | ||
import com.pokit.auth.model.PrincipalUser | ||
import com.pokit.category.dto.request.CreateCategoryRequest | ||
import com.pokit.category.dto.request.toDto | ||
import com.pokit.category.dto.response.CategoryCountResponse | ||
import com.pokit.category.dto.response.CategoryResponse | ||
import com.pokit.category.dto.response.toResponse | ||
import com.pokit.category.exception.CategoryErrorCode | ||
import com.pokit.category.model.Category | ||
import com.pokit.category.model.CategoryImage | ||
import com.pokit.category.port.`in`.CategoryUseCase | ||
import com.pokit.common.dto.SliceResponseDto | ||
import com.pokit.common.wrapper.ResponseWrapper.wrapOk | ||
import com.pokit.common.wrapper.ResponseWrapper.wrapSlice | ||
import com.pokit.common.wrapper.ResponseWrapper.wrapUnit | ||
import io.swagger.v3.oas.annotations.Operation | ||
import jakarta.validation.Valid | ||
import org.springframework.data.domain.Pageable | ||
import org.springframework.data.domain.Sort | ||
import org.springframework.data.web.PageableDefault | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal | ||
import org.springframework.web.bind.annotation.* | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/category") | ||
class CategoryController( | ||
private val categoryUseCase: CategoryUseCase | ||
) { | ||
@Operation(summary = "ν¬ν· μμ± API") | ||
@ErrorOperation(CategoryErrorCode::class) | ||
@PostMapping | ||
fun createCategory( | ||
@AuthenticationPrincipal user: PrincipalUser, | ||
@Valid @RequestBody request: CreateCategoryRequest | ||
): ResponseEntity<CategoryResponse> = | ||
categoryUseCase.create(request.toDto(), user.id) | ||
.toResponse() | ||
.wrapOk() | ||
|
||
@Operation(summary = "ν¬ν· λͺ©λ‘ μ‘°ν API") | ||
@GetMapping | ||
fun getCategories( | ||
@AuthenticationPrincipal user: PrincipalUser, | ||
@PageableDefault( | ||
page = 0, | ||
size = 10, | ||
sort = ["createdAt"], | ||
direction = Sort.Direction.DESC | ||
) pageable: Pageable, | ||
@RequestParam(defaultValue = "true") filterUncategorized: Boolean | ||
): ResponseEntity<SliceResponseDto<Category>> = | ||
categoryUseCase.getCategories(user.id, pageable, filterUncategorized) | ||
.wrapSlice() | ||
.wrapOk() | ||
|
||
@Operation(summary = "ν¬ν· μμ API") | ||
@ErrorOperation(CategoryErrorCode::class) | ||
@PatchMapping("/{categoryId}") | ||
fun updateCategory( | ||
@AuthenticationPrincipal user: PrincipalUser, | ||
@Valid @RequestBody request: CreateCategoryRequest, | ||
@PathVariable categoryId: Long, | ||
): ResponseEntity<CategoryResponse> = | ||
categoryUseCase.update(request.toDto(), user.id, categoryId) | ||
.toResponse() | ||
.wrapOk() | ||
|
||
@Operation(summary = "ν¬ν· μμ API") | ||
@ErrorOperation(CategoryErrorCode::class) | ||
@PutMapping("/{categoryId}") | ||
fun deleteCategory( | ||
@AuthenticationPrincipal user: PrincipalUser, | ||
@PathVariable categoryId: Long, | ||
): ResponseEntity<Unit> = | ||
categoryUseCase.delete(categoryId, user.id) | ||
.wrapUnit() | ||
|
||
@Operation(summary = "μ μ μ ν¬ν· κ°μ μ‘°ν API") | ||
@GetMapping("/count") | ||
fun getTotalCount( | ||
@AuthenticationPrincipal user: PrincipalUser, | ||
): ResponseEntity<CategoryCountResponse> { | ||
val count = categoryUseCase.getTotalCount(user.id) | ||
return CategoryCountResponse(count) | ||
.wrapOk() | ||
} | ||
|
||
@Operation(summary = "ν¬ν· νλ‘ν λͺ©λ‘ μ‘°ν API") | ||
@GetMapping("/images") | ||
fun getCategoryImages(): ResponseEntity<List<CategoryImage>> = | ||
categoryUseCase.getAllCategoryImages() | ||
.wrapOk() | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
adapters/in-web/src/main/kotlin/com/pokit/category/dto/request/CreateCategoryRequest.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,17 @@ | ||
package com.pokit.category.dto.request | ||
|
||
import com.pokit.category.dto.CategoryCommand | ||
import jakarta.validation.constraints.NotBlank | ||
import jakarta.validation.constraints.Size | ||
|
||
data class CreateCategoryRequest( | ||
@field:NotBlank(message = "ν¬ν· λͺ μ νμ κ°μ λλ€.") | ||
@field:Size(min = 1, max = 10, message = "μ΅λ 10μκΉμ§ μ λ ₯ κ°λ₯ν©λλ€.") | ||
val categoryName: String, | ||
val categoryImageId: Int, | ||
) | ||
|
||
internal fun CreateCategoryRequest.toDto() = CategoryCommand( | ||
categoryName = this.categoryName, | ||
categoryImageId = this.categoryImageId | ||
) |
17 changes: 17 additions & 0 deletions
17
adapters/in-web/src/main/kotlin/com/pokit/category/dto/request/UpdateCategoryRequest.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,17 @@ | ||
package com.pokit.category.dto.request | ||
|
||
import com.pokit.category.dto.CategoryCommand | ||
import jakarta.validation.constraints.NotBlank | ||
import jakarta.validation.constraints.Size | ||
|
||
data class UpdateCategoryRequest ( | ||
@field:NotBlank(message = "ν¬ν· λͺ μ νμ κ°μ λλ€.") | ||
@field:Size(min = 1, max = 10, message = "μ΅λ 10μκΉμ§ μ λ ₯ κ°λ₯ν©λλ€.") | ||
val categoryName: String, | ||
val categoryImageId: Int, | ||
) | ||
|
||
internal fun UpdateCategoryRequest.toDto() = CategoryCommand( | ||
categoryName = this.categoryName, | ||
categoryImageId = this.categoryImageId | ||
) |
20 changes: 20 additions & 0 deletions
20
adapters/in-web/src/main/kotlin/com/pokit/category/dto/response/CategoryResponse.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.pokit.category.dto.response | ||
|
||
import com.pokit.category.model.Category | ||
import com.pokit.category.model.CategoryImage | ||
|
||
data class CategoryResponse( | ||
val categoryId: Long, | ||
var categoryName: String, | ||
var categoryImage: CategoryImage, | ||
) | ||
|
||
data class CategoryCountResponse( | ||
val categoryTotalCount: Int, | ||
) | ||
|
||
fun Category.toResponse(): CategoryResponse = CategoryResponse( | ||
categoryId = this.categoryId, | ||
categoryName = this.categoryName, | ||
categoryImage = this.categoryImage, | ||
) |
20 changes: 20 additions & 0 deletions
20
adapters/in-web/src/main/kotlin/com/pokit/common/dto/SliceResponseDto.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.pokit.common.dto | ||
|
||
import org.springframework.data.domain.Slice | ||
import org.springframework.data.domain.Sort | ||
|
||
data class SliceResponseDto<T>( | ||
val data: List<T>, | ||
val page: Int?, | ||
val size: Int?, | ||
val sort: Sort, | ||
val hasNext: Boolean, | ||
) { | ||
constructor(slice: Slice<T>) : this( | ||
data = slice.content, | ||
page = slice.pageable.pageNumber, | ||
size = slice.size, | ||
sort = slice.sort, | ||
hasNext = slice.hasNext() | ||
) | ||
} |
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
13 changes: 13 additions & 0 deletions
13
adapters/in-web/src/main/kotlin/com/pokit/common/wrapper/ResponseWrapper.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,13 @@ | ||
package com.pokit.common.wrapper | ||
|
||
import com.pokit.common.dto.SliceResponseDto | ||
import org.springframework.data.domain.Slice | ||
import org.springframework.http.ResponseEntity | ||
|
||
object ResponseWrapper { | ||
fun <T> T.wrapOk(): ResponseEntity<T> = ResponseEntity.ok(this) | ||
|
||
fun <T> Slice<T>.wrapSlice() = SliceResponseDto(this) | ||
|
||
fun Unit.wrapUnit(): ResponseEntity<Unit> = ResponseEntity.noContent().build() | ||
} |
39 changes: 39 additions & 0 deletions
39
...ut-persistence/src/main/kotlin/com/pokit/out/persistence/category/impl/CategoryAdapter.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,39 @@ | ||
package com.pokit.out.persistence.category.impl | ||
|
||
import com.pokit.category.model.Category | ||
import com.pokit.category.port.out.CategoryPort | ||
import com.pokit.out.persistence.category.persist.CategoryEntity | ||
import com.pokit.out.persistence.category.persist.CategoryRepository | ||
import com.pokit.out.persistence.category.persist.toDomain | ||
import org.springframework.data.domain.Pageable | ||
import org.springframework.data.domain.Slice | ||
import org.springframework.data.repository.findByIdOrNull | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class CategoryAdapter( | ||
private val categoryRepository: CategoryRepository | ||
) : CategoryPort { | ||
override fun loadAllByUserId(userId: Long, pageable: Pageable): Slice<Category> = | ||
categoryRepository.findByUserIdAndDeleted(userId, false, pageable) | ||
.map { it.toDomain() } | ||
|
||
override fun loadByIdAndUserId(id: Long, userId: Long): Category? = | ||
categoryRepository.findByIdAndUserIdAndDeleted(id, userId, false)?.toDomain() | ||
|
||
override fun existsByNameAndUserId(name: String, userId: Long): Boolean = | ||
categoryRepository.existsByNameAndUserIdAndDeleted(name, userId, false) | ||
|
||
override fun persist(category: Category): Category { | ||
val categoryEntity = CategoryEntity.of(category) | ||
return categoryRepository.save(categoryEntity).toDomain() | ||
} | ||
|
||
override fun delete(category: Category) { | ||
categoryRepository.findByIdOrNull(category.categoryId) | ||
?.delete() | ||
} | ||
|
||
override fun countByUserId(userId: Long): Int = | ||
categoryRepository.countByUserIdAndDeleted(userId, false) | ||
} |
20 changes: 20 additions & 0 deletions
20
...rsistence/src/main/kotlin/com/pokit/out/persistence/category/impl/CategoryImageAdapter.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.pokit.out.persistence.category.impl | ||
|
||
import com.pokit.category.model.CategoryImage | ||
import com.pokit.category.port.out.CategoryImagePort | ||
import com.pokit.out.persistence.category.persist.CategoryImageRepository | ||
import com.pokit.out.persistence.category.persist.toDomain | ||
import org.springframework.data.repository.findByIdOrNull | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class CategoryImageAdapter( | ||
private val categoryImageRepository: CategoryImageRepository | ||
) : CategoryImagePort { | ||
override fun loadById(id: Int): CategoryImage? = | ||
categoryImageRepository.findByIdOrNull(id)?.toDomain() | ||
|
||
override fun loadAll(): List<CategoryImage> = | ||
categoryImageRepository.findAll() | ||
.map { it.toDomain() } | ||
} |
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
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
6 changes: 6 additions & 0 deletions
6
...nce/src/main/kotlin/com/pokit/out/persistence/category/persist/CategoryImageRepository.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,6 @@ | ||
package com.pokit.out.persistence.category.persist | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository | ||
|
||
interface CategoryImageRepository : JpaRepository<CategoryImageEntity, Int> { | ||
} |
12 changes: 12 additions & 0 deletions
12
...sistence/src/main/kotlin/com/pokit/out/persistence/category/persist/CategoryRepository.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,12 @@ | ||
package com.pokit.out.persistence.category.persist | ||
|
||
import org.springframework.data.domain.Pageable | ||
import org.springframework.data.domain.Slice | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
|
||
interface CategoryRepository : JpaRepository<CategoryEntity, Long> { | ||
fun existsByNameAndUserIdAndDeleted(name: String, userId: Long, deleted: Boolean): Boolean | ||
fun findByUserIdAndDeleted(userId: Long, deleted: Boolean, pageable: Pageable): Slice<CategoryEntity> | ||
fun findByIdAndUserIdAndDeleted(id: Long, userId: Long, deleted: Boolean): CategoryEntity? | ||
fun countByUserIdAndDeleted(userId: Long, deleted: Boolean): Int | ||
} |
Oops, something went wrong.