Skip to content

Commit

Permalink
[feat #198] 포킷 생성/수정 api Ver2 (#199)
Browse files Browse the repository at this point in the history
* refactor : 버전 별 파일 분리

* feat : 포킷 생성/수정 api Ver2

* feat : Category 공개여부, 포킷 키워드 필드 추가

* feat : 필드 추가에 따른 기존 로직 수정
  • Loading branch information
dlswns2480 authored Dec 15, 2024
1 parent 6b0b1da commit 317a3e4
Show file tree
Hide file tree
Showing 18 changed files with 136 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.pokit.category
package com.pokit.category.v1

import com.pokit.auth.config.ErrorOperation
import com.pokit.auth.model.PrincipalUser
import com.pokit.category.dto.CategoriesResponse
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.v1.dto.request.CreateCategoryRequest
import com.pokit.category.v1.dto.request.toDto
import com.pokit.category.v1.dto.response.CategoryCountResponse
import com.pokit.category.v1.dto.response.CategoryResponse
import com.pokit.category.v1.dto.response.toResponse
import com.pokit.category.exception.CategoryErrorCode
import com.pokit.category.model.CategoryImage
import com.pokit.category.port.`in`.CategoryUseCase
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.pokit.category
package com.pokit.category.v1

import com.pokit.auth.aop.KakaoAuth
import com.pokit.auth.model.PrincipalUser
import com.pokit.category.dto.request.DuplicateCategoryRequest
import com.pokit.category.dto.response.SharedContentsResponse
import com.pokit.category.v1.dto.request.DuplicateCategoryRequest
import com.pokit.category.v1.dto.response.SharedContentsResponse
import com.pokit.category.port.`in`.CategoryUseCase
import com.pokit.common.wrapper.ResponseWrapper.wrapOk
import com.pokit.common.wrapper.ResponseWrapper.wrapUnit
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pokit.category.dto.request
package com.pokit.category.v1.dto.request

import com.pokit.category.dto.CategoryCommand
import jakarta.validation.constraints.NotBlank
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pokit.category.dto.request
package com.pokit.category.v1.dto.request

import jakarta.validation.constraints.Size

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pokit.category.dto.request
package com.pokit.category.v1.dto.request

import com.pokit.category.dto.CategoryCommand
import jakarta.validation.constraints.NotBlank
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pokit.category.dto.response
package com.pokit.category.v1.dto.response

import com.pokit.category.model.Category
import com.pokit.category.model.CategoryImage
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pokit.category.dto.response
package com.pokit.category.v1.dto.response

import com.pokit.category.model.Category
import com.pokit.common.dto.DateFormatters
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.pokit.category.v2

import com.pokit.auth.config.ErrorOperation
import com.pokit.auth.model.PrincipalUser
import com.pokit.category.exception.CategoryErrorCode
import com.pokit.category.port.`in`.CategoryUseCase
import com.pokit.category.v1.dto.response.CategoryResponse
import com.pokit.category.v1.dto.response.toResponse
import com.pokit.category.v2.dto.request.CreateCategoryRequestV2
import com.pokit.category.v2.dto.request.toDto
import com.pokit.common.wrapper.ResponseWrapper.wrapOk
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import jakarta.validation.Valid
import org.springframework.http.ResponseEntity
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.*

@Tag(name = "Category API Ver2")
@RestController
@RequestMapping("/api/v2/category")
class CategoryControllerV2(
private val categoryUseCase: CategoryUseCase
) {
@Operation(summary = "포킷 생성 API Ver2")
@ErrorOperation(CategoryErrorCode::class)
@PostMapping
fun createCategory(
@AuthenticationPrincipal user: PrincipalUser,
@Valid @RequestBody request: CreateCategoryRequestV2
): ResponseEntity<CategoryResponse> {
return categoryUseCase.create(request.toDto(), user.id)
.toResponse()
.wrapOk()
}

@Operation(summary = "포킷 추가 API Ver2")
@ErrorOperation(CategoryErrorCode::class)
@PatchMapping("/{categoryId}")
fun updateCategory(
@AuthenticationPrincipal user: PrincipalUser,
@Valid @RequestBody request: CreateCategoryRequestV2,
@PathVariable categoryId: Long,
): ResponseEntity<CategoryResponse> {
return categoryUseCase.update(request.toDto(), user.id, categoryId)
.toResponse()
.wrapOk()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.pokit.category.v2.dto.request

import com.pokit.category.dto.CategoryCommand
import com.pokit.category.model.OpenType
import com.pokit.user.model.InterestType
import jakarta.validation.constraints.NotBlank
import jakarta.validation.constraints.Size

data class CreateCategoryRequestV2(
@field:NotBlank(message = "포킷 명은 필수 값입니다.")
@field:Size(min = 1, max = 10, message = "최대 10자까지 입력 가능합니다.")
val categoryName: String,
val categoryImageId: Int,
val openType: String,
val keywordType: String,
)

internal fun CreateCategoryRequestV2.toDto() = CategoryCommand(
categoryName = this.categoryName,
categoryImageId = this.categoryImageId,
openType = OpenType.of(this.openType),
keywordType = InterestType.of(this.keywordType)
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.pokit.out.persistence.category.persist
import com.pokit.category.model.Category
import com.pokit.category.model.OpenType
import com.pokit.out.persistence.BaseEntity
import com.pokit.user.model.InterestType
import jakarta.persistence.*

// 포킷 엔티티
Expand Down Expand Up @@ -32,7 +33,11 @@ class CategoryEntity(
var userCount: Int = 0,

@Column(name = "is_shared")
var isShared: Boolean = false
var isShared: Boolean = false,

@Column(name = "keyword")
@Enumerated(EnumType.STRING)
var keyword: InterestType = InterestType.DEFAULT,
) : BaseEntity() {

@Column(name = "is_deleted")
Expand All @@ -51,7 +56,8 @@ class CategoryEntity(
image = CategoryImageEntity.of(category.categoryImage),
openType = category.openType,
userCount = category.userCount,
isShared = category.isShared
isShared = category.isShared,
keyword = category.keyword
)
}
}
Expand All @@ -64,5 +70,6 @@ fun CategoryEntity.toDomain() = Category(
createdAt = this.createdAt,
openType = this.openType,
userCount = this.userCount,
isShared = this.isShared
isShared = this.isShared,
keyword = this.keyword
)
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class CategoryService(
categoryName = command.categoryName,
categoryImage = categoryImage,
userId = userId,
openType = OpenType.PRIVATE,
openType = command.openType,
keyword = command.keywordType
)
)
}
Expand All @@ -67,7 +68,7 @@ class CategoryService(
val categoryImage = categoryImagePort.loadById(categoryCommand.categoryImageId)
?: throw NotFoundCustomException(CategoryErrorCode.NOT_FOUND_CATEGORY_IMAGE)

category.update(categoryCommand.categoryName, categoryImage)
category.update(categoryCommand, categoryImage)
return categoryPort.persist(category)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.pokit.user.dto.request.UserCommand
import com.pokit.user.exception.UserErrorCode
import com.pokit.user.model.FcmToken
import com.pokit.user.model.Interest
import com.pokit.user.model.InterestType
import com.pokit.user.model.User
import com.pokit.user.port.`in`.UserUseCase
import com.pokit.user.port.out.FcmTokenPort
Expand Down Expand Up @@ -58,6 +59,7 @@ class UserService(
categoryName = UNCATEGORIZED.displayName,
categoryImage = image,
openType = OpenType.PRIVATE,
keyword = InterestType.DEFAULT
)
categoryPort.persist(category)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ data class CategoriesResponse(
var categoryName: String,
var categoryImage: CategoryImage,
var contentCount: Int,
val createdAt: String
val createdAt: String,
val openType: String,
val keywordType: String,
)

fun Category.toCategoriesResponse(): CategoriesResponse {
Expand All @@ -22,6 +24,8 @@ fun Category.toCategoriesResponse(): CategoriesResponse {
categoryName = this.categoryName,
categoryImage = this.categoryImage,
contentCount = this.contentCount,
createdAt = this.createdAt.format(formatter)
createdAt = this.createdAt.format(formatter),
openType = this.openType.toString(),
keywordType = this.keyword.kor
)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.pokit.category.dto

data class CategoryCommand (
import com.pokit.category.model.OpenType
import com.pokit.user.model.InterestType

data class CategoryCommand(
val categoryName: String,
val categoryImageId: Int,
val openType: OpenType = OpenType.PRIVATE,
val keywordType: InterestType = InterestType.DEFAULT,
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ enum class CategoryErrorCode(
NOT_FOUND_UNCATEGORIZED("사용자가 미분류 카테고리가 없습니다.(서버 에러)", "CA_008"),
ALREADY_ACCEPTED("이미 초대를 수락한 포킷입니다.", "CA_009"),
NEVER_ACCPTED("해당 유저가 포킷에 초대된 이력이 없습니다.", "CA_0010"),
INVALID_OPENTYPE("OpenType은 PUBLIC, PRIVATE중 하나여야 합니다.", "CA_0011"),

}
9 changes: 7 additions & 2 deletions domain/src/main/kotlin/com/pokit/category/model/Category.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.pokit.category.model

import com.pokit.category.dto.CategoryCommand
import com.pokit.user.model.InterestType
import java.time.LocalDateTime

data class Category(
Expand All @@ -12,10 +14,13 @@ data class Category(
var openType: OpenType,
var userCount: Int = 0,
var isShared: Boolean = false,
var keyword: InterestType,
) {
fun update(categoryName: String, categoryImage: CategoryImage) {
this.categoryName = categoryName
fun update(command: CategoryCommand, categoryImage: CategoryImage) {
this.categoryName = command.categoryName
this.categoryImage = categoryImage
this.openType = command.openType
this.keyword = command.keywordType
}

fun completeShare(): Category {
Expand Down
14 changes: 13 additions & 1 deletion domain/src/main/kotlin/com/pokit/category/model/OpenType.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
package com.pokit.category.model

import com.pokit.category.exception.CategoryErrorCode
import com.pokit.common.exception.ClientValidationException

enum class OpenType {
PUBLIC,
PRIVATE,
PRIVATE
;

companion object {
fun of(input: String): OpenType {
return OpenType.entries
.firstOrNull { it.toString() == input }
?: throw ClientValidationException(CategoryErrorCode.INVALID_OPENTYPE)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.pokit.user.exception.UserErrorCode
enum class InterestType(
val kor: String,
) {
DEFAULT("default"),
SPORTS("스포츠/레저"),
OFFICE("문구/오피스"),
FASHION("패션"),
Expand Down

0 comments on commit 317a3e4

Please sign in to comment.