Skip to content

Commit 756451c

Browse files
committed
[DVK-74] fix: 카테고리 로직 수정
- 회원가입시 카테고리 생성 로직 제거 - 관심 카테고리 수정시 카테고리 생성 로직 제거 - 카테고리 기본값 입력 추가
1 parent 2c3da87 commit 756451c

32 files changed

+204
-328
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.devooks.backend.category.v1.error
2+
3+
import com.devooks.backend.common.exception.GeneralException
4+
import org.springframework.http.HttpStatus.BAD_REQUEST
5+
import org.springframework.http.HttpStatus.NOT_FOUND
6+
7+
enum class CategoryError(val exception: GeneralException) {
8+
// 400
9+
INVALID_CATEGORY_ID(GeneralException("CATEGORY-400-1", BAD_REQUEST, "잘못된 형식의 카테고리 식별자 입니다")),
10+
// 404
11+
NOT_FOUND_CATEGORY_BY_ID(GeneralException("CATEGORY-404-1", NOT_FOUND, "카테고리를 찾을 수 없습니다"))
12+
}

src/main/kotlin/com/devooks/backend/category/v1/service/CategoryService.kt

+11-10
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package com.devooks.backend.category.v1.service
33
import com.devooks.backend.category.v1.domain.Category
44
import com.devooks.backend.category.v1.domain.Category.Companion.toDomain
55
import com.devooks.backend.category.v1.dto.GetCategoriesRequest
6-
import com.devooks.backend.category.v1.entity.CategoryEntity
6+
import com.devooks.backend.category.v1.error.CategoryError
77
import com.devooks.backend.category.v1.repository.CategoryRepository
8-
import kotlinx.coroutines.flow.asFlow
8+
import java.util.*
9+
import kotlinx.coroutines.flow.count
910
import kotlinx.coroutines.flow.map
1011
import kotlinx.coroutines.flow.toList
1112
import org.springframework.stereotype.Service
@@ -21,15 +22,15 @@ class CategoryService(
2122
name = request.keyword,
2223
pageable = request.paging.value
2324
)
24-
.map { Category(it.id!!, it.name) }
25-
.toList()
26-
27-
suspend fun save(categoryNames: List<String>): List<Category> =
28-
categoryNames
29-
.asFlow()
30-
.map { name -> name to categoryRepository.findByNameIsIgnoreCase(name) }
31-
.map { (name, entity) -> entity ?: categoryRepository.save(CategoryEntity(name = name)) }
3225
.map { it.toDomain() }
3326
.toList()
3427

28+
suspend fun getAll(categoryIds: List<UUID>): List<Category> =
29+
categoryRepository
30+
.findAllById(categoryIds)
31+
.takeIf { it.count() == categoryIds.size }
32+
?.map { it.toDomain() }
33+
?.toList()
34+
?: throw CategoryError.NOT_FOUND_CATEGORY_BY_ID.exception
35+
3536
}

src/main/kotlin/com/devooks/backend/ebook/v1/controller/EbookController.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class EbookController(
6666
ebookImageService.save(listOf(command.mainImageId), ebook)
6767
val descriptionImageList: List<EbookImage> =
6868
ebookImageService.save(command.descriptionImageIdList, ebook)
69-
val categoryList: List<Category> = categoryService.save(command.relatedCategoryNameList)
69+
val categoryList: List<Category> = categoryService.getAll(command.relatedCategoryIdList)
7070
relatedCategoryService.save(categoryList, ebook)
7171
return CreateEbookResponse(EbookResponse(ebook, descriptionImageList, categoryList))
7272
}
@@ -153,4 +153,4 @@ class EbookController(
153153
return DeleteEbookResponse()
154154
}
155155

156-
}
156+
}

src/main/kotlin/com/devooks/backend/ebook/v1/dto/command/CreateEbookCommand.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import java.util.*
55
class CreateEbookCommand(
66
val pdfId: UUID,
77
val title: String,
8-
val relatedCategoryNameList: List<String>,
8+
val relatedCategoryIdList: List<UUID>,
99
val mainImageId: UUID,
1010
val descriptionImageIdList: List<UUID>,
1111
val price: Int,
1212
val introduction: String,
1313
val tableOfContents: String,
1414
val sellingMemberId: UUID
15-
)
15+
)

src/main/kotlin/com/devooks/backend/ebook/v1/dto/command/ModifyEbookCommand.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import java.util.*
55
class ModifyEbookCommand(
66
val ebookId: UUID,
77
val title: String?,
8-
val relatedCategoryNameList: List<String>?,
8+
val relatedCategoryIdList: List<UUID>?,
99
val mainImageId: UUID?,
1010
val descriptionImageIdList: List<UUID>?,
1111
val introduction: String?,
@@ -16,5 +16,5 @@ class ModifyEbookCommand(
1616
val isChangedEbook: Boolean =
1717
title != null || mainImageId != null || introduction != null || tableOfContents != null || price != null
1818
val isChangedDescriptionImageList: Boolean = descriptionImageIdList != null
19-
val isChangedRelatedCategoryNameList: Boolean = relatedCategoryNameList != null
19+
val isChangedRelatedCategoryIdList: Boolean = relatedCategoryIdList != null
2020
}

src/main/kotlin/com/devooks/backend/ebook/v1/dto/request/CreateEbookRequest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import java.util.*
1414
data class CreateEbookRequest(
1515
val pdfId: String?,
1616
val title: String?,
17-
val relatedCategoryNameList: List<String>?,
17+
val relatedCategoryIdList: List<String>?,
1818
val mainImageId: String?,
1919
val descriptionImageIdList: List<String>?,
2020
val price: Int?,
@@ -25,7 +25,7 @@ data class CreateEbookRequest(
2525
CreateEbookCommand(
2626
pdfId = pdfId.validatePdfId(),
2727
title = title.validateEbookTitle(),
28-
relatedCategoryNameList = relatedCategoryNameList.validateRelatedCategoryList(),
28+
relatedCategoryIdList = relatedCategoryIdList.validateRelatedCategoryList(),
2929
mainImageId = mainImageId.validateMainImageId(),
3030
descriptionImageIdList = descriptionImageIdList.validateDescriptionImageIdList(),
3131
price = price.validateEbookPrice(),

src/main/kotlin/com/devooks/backend/ebook/v1/dto/request/ModifyEbookRequest.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ data class ModifyEbookRequest(
1818
) {
1919
data class Ebook(
2020
val title: String? = null,
21-
val relatedCategoryNameList: List<String>? = null,
21+
val relatedCategoryIdList: List<String>? = null,
2222
val mainImageId: String? = null,
2323
val descriptionImageIdList: List<String>? = null,
2424
val introduction: String? = null,
@@ -28,7 +28,7 @@ data class ModifyEbookRequest(
2828

2929
data class IsChanged(
3030
val title: Boolean? = false,
31-
val relatedCategoryNameList: Boolean? = false,
31+
val relatedCategoryIdList: Boolean? = false,
3232
val mainImage: Boolean? = false,
3333
val descriptionImageList: Boolean? = false,
3434
val introduction: Boolean? = false,
@@ -42,7 +42,7 @@ data class ModifyEbookRequest(
4242
ModifyEbookCommand(
4343
ebookId.validateEbookId(),
4444
if (isChanged.title == true) ebook.title.validateEbookTitle() else null,
45-
if (isChanged.relatedCategoryNameList == true) ebook.relatedCategoryNameList.validateRelatedCategoryList() else null,
45+
if (isChanged.relatedCategoryIdList == true) ebook.relatedCategoryIdList.validateRelatedCategoryList() else null,
4646
if (isChanged.mainImage == true) ebook.mainImageId.validateMainImageId() else null,
4747
if (isChanged.descriptionImageList == true) ebook.descriptionImageIdList.validateDescriptionImageIdList() else null,
4848
if (isChanged.introduction == true) ebook.introduction.validateEbookIntroduction() else null,

src/main/kotlin/com/devooks/backend/ebook/v1/dto/response/EbookResponse.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ data class EbookResponse(
1414
val id: UUID,
1515
val pdfId: UUID,
1616
val mainImageId: UUID,
17-
val relatedCategoryNameList: List<CategoryDto>,
17+
val relatedCategoryList: List<CategoryDto>,
1818
val title: String,
1919
val price: Int,
2020
val tableOfContents: String,
@@ -33,7 +33,7 @@ data class EbookResponse(
3333
id = ebook.id,
3434
pdfId = ebook.pdfId,
3535
mainImageId = ebook.mainImageId,
36-
relatedCategoryNameList = categoryList.map { it.toDto() },
36+
relatedCategoryList = categoryList.map { it.toDto() },
3737
title = ebook.title,
3838
price = ebook.price,
3939
tableOfContents = ebook.tableOfContents,
@@ -44,4 +44,4 @@ data class EbookResponse(
4444
deletedDate = ebook.deletedDate,
4545
descriptionImageList = descriptionImageList.map { it.toDto() }.sortedBy { it.order }
4646
)
47-
}
47+
}

src/main/kotlin/com/devooks/backend/ebook/v1/error/EbookValidation.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.devooks.backend.ebook.v1.error
22

3+
import com.devooks.backend.category.v1.error.CategoryError
34
import com.devooks.backend.common.error.validateNotBlank
45
import com.devooks.backend.common.error.validateNotEmpty
56
import com.devooks.backend.common.error.validateNotNull
@@ -13,8 +14,9 @@ fun String?.validatePdfId(): UUID =
1314
fun String?.validateEbookTitle(): String =
1415
validateNotBlank(EbookError.REQUIRED_TITLE.exception)
1516

16-
fun List<String>?.validateRelatedCategoryList(): List<String> =
17+
fun List<String>?.validateRelatedCategoryList(): List<UUID> =
1718
validateNotEmpty(EbookError.REQUIRED_RELATED_CATEGORY_LIST.exception)
19+
.map { it.validateUUID(CategoryError.INVALID_CATEGORY_ID.exception) }
1820

1921
fun Int?.validateEbookPrice(): Int =
2022
takeIf { it != null && it in 0..9_999_999 }
@@ -49,4 +51,4 @@ fun String?.validateMainImageId(): UUID =
4951

5052
fun List<String>?.validateDescriptionImageIdList(): List<UUID> =
5153
validateNotNull(EbookError.REQUIRED_DESCRIPTION_IMAGE_ID.exception)
52-
.map { it.validateUUID(EbookError.INVALID_DESCRIPTION_IMAGE_ID.exception) }
54+
.map { it.validateUUID(EbookError.INVALID_DESCRIPTION_IMAGE_ID.exception) }

src/main/kotlin/com/devooks/backend/ebook/v1/service/RelatedCategoryService.kt

+4-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.devooks.backend.ebook.v1.service
22

33
import com.devooks.backend.category.v1.domain.Category
44
import com.devooks.backend.category.v1.domain.Category.Companion.toDomain
5-
import com.devooks.backend.category.v1.entity.CategoryEntity
65
import com.devooks.backend.category.v1.repository.CategoryRepository
76
import com.devooks.backend.ebook.v1.domain.Ebook
87
import com.devooks.backend.ebook.v1.dto.command.ModifyEbookCommand
@@ -24,13 +23,9 @@ class RelatedCategoryService(
2423
.toList()
2524

2625
suspend fun modify(command: ModifyEbookCommand, ebook: Ebook): List<Category> {
27-
return if (command.isChangedRelatedCategoryNameList) {
28-
val relatedCategoryNameList = command.relatedCategoryNameList!!
29-
val categoryList = relatedCategoryNameList
30-
.map { name -> name to categoryRepository.findByNameIsIgnoreCase(name) }
31-
.map { (name, entity) -> entity ?: categoryRepository.save(CategoryEntity(name = name)) }
32-
.map { it.toDomain() }
33-
.toList()
26+
return if (command.isChangedRelatedCategoryIdList) {
27+
val categoryList =
28+
categoryRepository.findAllById(command.relatedCategoryIdList!!).map { it.toDomain() }.toList()
3429
relatedCategoryRepository.deleteAllByEbookId(ebook.id)
3530
save(categoryList, ebook)
3631
categoryList
@@ -43,4 +38,4 @@ class RelatedCategoryService(
4338
.toList()
4439
}
4540
}
46-
}
41+
}

src/main/kotlin/com/devooks/backend/member/v1/controller/MemberController.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class MemberController(
9292
val command = request.toCommand()
9393
val member = memberService.signUp(command)
9494
memberInfoService.create(member)
95-
val categories = categoryService.save(command.favoriteCategoryNames)
95+
val categories = categoryService.getAll(command.favoriteCategoryIdList)
9696
favoriteCategoryService.save(categories, member.id)
9797
val tokenGroup = tokenService.createTokenGroup(member)
9898
return SignUpResponse(
@@ -154,7 +154,7 @@ class MemberController(
154154
val requesterId: UUID = tokenService.getMemberId(Authorization(authorization))
155155
val command: ModifyProfileCommand = request.toCommand()
156156
val memberInfo: MemberInfo = memberInfoService.updateProfile(command, requesterId)
157-
val categories: List<Category> = categoryService.save(command.favoriteCategoryNames)
157+
val categories: List<Category> = categoryService.getAll(command.favoriteCategoryIdList)
158158
favoriteCategoryService.deleteByMemberId(requesterId)
159159
favoriteCategoryService.save(categories, requesterId)
160160
return ModifyProfileResponse(memberInfo, categories)
@@ -184,4 +184,4 @@ class MemberController(
184184
memberService.withdraw(command, requesterId)
185185
return WithdrawMemberResponse()
186186
}
187-
}
187+
}

src/main/kotlin/com/devooks/backend/member/v1/dto/GetProfileResponse.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.devooks.backend.member.v1.dto
22

33
import com.devooks.backend.category.v1.domain.Category
4+
import com.devooks.backend.category.v1.dto.CategoryDto
5+
import com.devooks.backend.category.v1.dto.CategoryDto.Companion.toDto
46
import com.devooks.backend.member.v1.domain.Member
57
import com.devooks.backend.member.v1.domain.MemberInfo
68
import java.util.*
@@ -10,7 +12,7 @@ data class GetProfileResponse(
1012
val nickname: String,
1113
val profileImagePath: String,
1214
val profile: Profile,
13-
val favoriteCategories: List<String>,
15+
val favoriteCategories: List<CategoryDto>,
1416
) {
1517

1618
constructor(
@@ -27,7 +29,7 @@ data class GetProfileResponse(
2729
youtubeLink = memberInfo.youtubeLink,
2830
introduction = memberInfo.introduction
2931
),
30-
favoriteCategories = categories.map { it.name }
32+
favoriteCategories = categories.map { it.toDto() }
3133
)
3234

3335
data class Profile(
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.devooks.backend.member.v1.dto
22

3+
import java.util.UUID
4+
35
class ModifyProfileCommand(
46
val phoneNumber: String,
57
val blogLink: String,
68
val instagramLink: String,
79
val youtubeLink: String,
810
val introduction: String,
9-
val favoriteCategoryNames: List<String>,
11+
val favoriteCategoryIdList: List<UUID>,
1012
)

src/main/kotlin/com/devooks/backend/member/v1/dto/ModifyProfileRequest.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.devooks.backend.member.v1.dto
22

33
import com.devooks.backend.member.v1.error.validateBlogLink
4-
import com.devooks.backend.member.v1.error.validateFavoriteCategoryNames
4+
import com.devooks.backend.member.v1.error.validateFavoriteCategoryIdList
55
import com.devooks.backend.member.v1.error.validateInstagramLink
66
import com.devooks.backend.member.v1.error.validateIntroduction
77
import com.devooks.backend.member.v1.error.validatePhoneNumber
@@ -13,7 +13,7 @@ data class ModifyProfileRequest(
1313
val instagramLink: String?,
1414
val youtubeLink: String?,
1515
val introduction: String?,
16-
val favoriteCategoryNames: List<String>?,
16+
val favoriteCategoryIdList: List<String>?,
1717
) {
1818
fun toCommand(): ModifyProfileCommand =
1919
ModifyProfileCommand(
@@ -22,6 +22,6 @@ data class ModifyProfileRequest(
2222
instagramLink = instagramLink.validateInstagramLink(),
2323
youtubeLink = youtubeLink.validateYoutubeLink(),
2424
introduction = introduction.validateIntroduction(),
25-
favoriteCategoryNames = favoriteCategoryNames.validateFavoriteCategoryNames()
25+
favoriteCategoryIdList = favoriteCategoryIdList.validateFavoriteCategoryIdList()
2626
)
2727
}

src/main/kotlin/com/devooks/backend/member/v1/dto/SignUpCommand.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package com.devooks.backend.member.v1.dto
22

33
import com.devooks.backend.auth.v1.domain.OauthId
44
import com.devooks.backend.auth.v1.domain.OauthType
5+
import java.util.*
56

67
class SignUpCommand(
78
val oauthId: OauthId,
89
val oauthType: OauthType,
910
val nickname: String,
10-
val favoriteCategoryNames: List<String>,
11-
)
11+
val favoriteCategoryIdList: List<UUID>,
12+
)

src/main/kotlin/com/devooks/backend/member/v1/dto/SignUpRequest.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.devooks.backend.member.v1.dto
22

33
import com.devooks.backend.auth.v1.error.validateOauthId
44
import com.devooks.backend.auth.v1.error.validateOauthType
5-
import com.devooks.backend.member.v1.error.validateFavoriteCategories
5+
import com.devooks.backend.member.v1.error.validateFavoriteCategoryIdList
66
import com.devooks.backend.member.v1.error.validateNickname
77
import io.swagger.v3.oas.annotations.media.Schema
88

@@ -13,16 +13,16 @@ data class SignUpRequest(
1313
val oauthType: String?,
1414
@Schema(description = "닉네임", required = true, nullable = false)
1515
val nickname: String?,
16-
@Schema(description = "관심 카테고리 목록", required = true, nullable = false)
17-
val favoriteCategories: List<String>?,
16+
@Schema(description = "관심 카테고리 식별자 목록", required = true, nullable = false)
17+
val favoriteCategoryIdList: List<String>?,
1818
) {
1919

2020
fun toCommand(): SignUpCommand =
2121
SignUpCommand(
2222
oauthId = oauthId.validateOauthId(),
2323
oauthType = oauthType.validateOauthType(),
2424
nickname = nickname.validateNickname(),
25-
favoriteCategoryNames = favoriteCategories.validateFavoriteCategories()
25+
favoriteCategoryIdList = favoriteCategoryIdList.validateFavoriteCategoryIdList()
2626
)
2727

28-
}
28+
}

src/main/kotlin/com/devooks/backend/member/v1/error/MemberError.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ enum class MemberError(val exception: GeneralException) {
2020
REQUIRED_INSTAGRAM_LINK(GeneralException("MEMBER-400-10", BAD_REQUEST, "인스타그램 링크가 반드시 필요합니다.")),
2121
REQUIRED_YOUTUBE_LINK(GeneralException("MEMBER-400-11", BAD_REQUEST, "유튜브 링크가 반드시 필요합니다.")),
2222
REQUIRED_INTRODUCTION_LINK(GeneralException("MEMBER-400-12", BAD_REQUEST, "소개글이 반드시 필요합니다.")),
23-
INVALID_FAVORITE_CATEGORIES(GeneralException("MEMBER-400-13", BAD_REQUEST, "카테고리는 20자 이하만 가능합니다.")),
23+
INVALID_FAVORITE_CATEGORIES(GeneralException("MEMBER-400-13", BAD_REQUEST, "잘못된 형식의 카테고리 식별자 입니다.")),
2424
REQUIRED_WITHDRAWAL_REASON(GeneralException("MEMBER-400-14", BAD_REQUEST, "탈퇴 이유가 반드시 필요합니다.")),
2525
INVALID_MEMBER_ID(GeneralException("MEMBER-400-15", BAD_REQUEST, "잘못된 형식의 회원 식별자 입니다.")),
2626

@@ -40,4 +40,4 @@ enum class MemberError(val exception: GeneralException) {
4040
override fun toString(): String {
4141
return "MemberError(exception=$exception)"
4242
}
43-
}
43+
}

0 commit comments

Comments
 (0)