Skip to content

Commit

Permalink
Merge pull request #136 from PSR-Co/refactor/#111-enumValid
Browse files Browse the repository at this point in the history
[refactor] enumValid 어노테이션 User + CS 도메인 적용
  • Loading branch information
chaerlo127 authored Aug 19, 2023
2 parents 3e45ac4 + 7db61d7 commit 15c69e4
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 33 deletions.
8 changes: 4 additions & 4 deletions src/main/kotlin/com/psr/psr/cs/controller/CsController.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.psr.psr.cs.controller

import com.psr.psr.cs.dto.FaqListRes
import com.psr.psr.cs.dto.FaqRes
import com.psr.psr.cs.dto.NoticeListRes
import com.psr.psr.cs.dto.NoticeRes
import com.psr.psr.cs.dto.response.FaqListRes
import com.psr.psr.cs.dto.response.FaqRes
import com.psr.psr.cs.dto.response.NoticeListRes
import com.psr.psr.cs.dto.response.NoticeRes
import com.psr.psr.cs.service.CsService
import com.psr.psr.global.dto.BaseResponse
import org.springframework.web.bind.annotation.*
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/com/psr/psr/cs/dto/assembler/CsAssembler.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.psr.psr.cs.dto.assembler

import com.psr.psr.cs.dto.FaqListRes
import com.psr.psr.cs.dto.FaqRes
import com.psr.psr.cs.dto.NoticeListRes
import com.psr.psr.cs.dto.NoticeRes
import com.psr.psr.cs.dto.response.FaqListRes
import com.psr.psr.cs.dto.response.FaqRes
import com.psr.psr.cs.dto.response.NoticeListRes
import com.psr.psr.cs.dto.response.NoticeRes
import com.psr.psr.cs.entity.Faq
import com.psr.psr.cs.entity.Notice
import org.springframework.stereotype.Component
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.psr.psr.cs.dto
package com.psr.psr.cs.dto.response

data class FaqListRes (
val faqLists: List<FaqRes>?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.psr.psr.cs.dto
package com.psr.psr.cs.dto.response

import com.fasterxml.jackson.annotation.JsonInclude

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.psr.psr.cs.dto
package com.psr.psr.cs.dto.response

data class NoticeListRes (
val noticeLists: List<NoticeRes>?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.psr.psr.cs.dto
package com.psr.psr.cs.dto.response

import com.fasterxml.jackson.annotation.JsonFormat
import com.fasterxml.jackson.annotation.JsonInclude
Expand Down
12 changes: 6 additions & 6 deletions src/main/kotlin/com/psr/psr/cs/service/CsService.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.psr.psr.cs.service

import com.psr.psr.cs.dto.FaqListRes
import com.psr.psr.cs.dto.FaqRes
import com.psr.psr.cs.dto.NoticeListRes
import com.psr.psr.cs.dto.NoticeRes
import com.psr.psr.cs.dto.response.FaqListRes
import com.psr.psr.cs.dto.response.FaqRes
import com.psr.psr.cs.dto.response.NoticeListRes
import com.psr.psr.cs.dto.response.NoticeRes
import com.psr.psr.cs.dto.assembler.CsAssembler
import com.psr.psr.cs.entity.FaqType
import com.psr.psr.cs.repository.FaqRepository
Expand All @@ -21,12 +21,12 @@ class CsService(

) {
// 공지사항 메인
fun getNotices() : NoticeListRes{
fun getNotices() : NoticeListRes {
return csAssembler.toNoticeListRes(noticeRepository.findByStatusOrderByCreatedAtDesc(ACTIVE_STATUS))
}

// 공지사항 상세
fun getNotice(noticeId: Long) : NoticeRes{
fun getNotice(noticeId: Long) : NoticeRes {
val notice = noticeRepository.findByIdAndStatus(noticeId, ACTIVE_STATUS) ?: throw BaseException(BaseResponseCode.NOT_FOUND_NOTICE)
return csAssembler.toNoticeRes(notice)
}
Expand Down
8 changes: 3 additions & 5 deletions src/main/kotlin/com/psr/psr/user/dto/UserInterestDto.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.psr.psr.user.dto

import com.psr.psr.global.resolver.EnumValid
import com.psr.psr.user.entity.Category

data class UserInterestDto (
@EnumValid(enumClass = Category::class, message = "올바르지 않은 상품 카테고리입니다.")
val category: String
){
fun checkInterestCategory() : Category{
return Category.getCategoryByName(category)
}
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class UserAssembler {
fun toEntity(signUpReq: SignUpReq): User {
return User(email = signUpReq.email,
password = signUpReq.password,
type = Type.getTypeByName(signUpReq.type),
type = Type.getTypeByValue(signUpReq.type),
phone = signUpReq.phone,
imgUrl = signUpReq.imgUrl,
provider = Provider.LOCAL,
Expand All @@ -43,7 +43,7 @@ class UserAssembler {
fun toInterestListEntity(user: User, signUpReq: SignUpReq): List<UserInterest> {
return signUpReq.interestList.stream()
.map { i ->
UserInterest(category = Category.getCategoryByName(i.category),
UserInterest(category = Category.getCategoryByValue(i.category),
user = user)
}.collect(Collectors.toList())
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/com/psr/psr/user/dto/request/SignUpReq.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.psr.psr.user.dto.request

import com.psr.psr.global.resolver.EnumValid
import com.psr.psr.user.dto.UserInterestDto
import com.psr.psr.user.entity.Category
import com.psr.psr.user.entity.Type
import jakarta.annotation.Nullable
import jakarta.validation.constraints.Email
import jakarta.validation.constraints.NotBlank
Expand All @@ -20,6 +23,7 @@ data class SignUpReq (
)
var password: String,
@field:NotBlank
@EnumValid(enumClass = Type::class, message = "올바르지 않은 사용자 역할니다.")
val type: String,
@field:Pattern(
regexp = "^01([0|1|6|7|8|9])-?([0-9]{3,4})-?([0-9]{4})\$",
Expand Down
10 changes: 4 additions & 6 deletions src/main/kotlin/com/psr/psr/user/entity/Type.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package com.psr.psr.user.entity

import com.psr.psr.global.exception.BaseException
import com.psr.psr.global.exception.BaseResponseCode
import com.psr.psr.global.resolver.EnumType


enum class Type(val value: String) {
enum class Type(override val value: String) : EnumType {
GENERAL("일반"),
ENTREPRENEUR("사업자"),
SHOW_HOST("쇼호스트"),
MANAGER("관리자");

companion object {
fun getTypeByName(name: String): Type {
return enumValues<Type>().find { it.value == name }
?: throw BaseException(BaseResponseCode.INVALID_USER_TYPE_NAME)
fun getTypeByValue(value: String): Type {
return enumValues<Type>().find { it.value == value }!!
}
}
}
5 changes: 3 additions & 2 deletions src/main/kotlin/com/psr/psr/user/service/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ class UserService(
// 회원가입
@Transactional
fun signUp(signUpReq: SignUpReq): TokenDto {
val categoryCheck = signUpReq.interestList.stream().map { i -> i.checkInterestCategory() }.collect(Collectors.toList()).groupingBy { it }.eachCount().any { it.value > 1 }
// 중복 값 확인
val categoryCheck = signUpReq.interestList.groupingBy { it }.eachCount().any { it.value > 1 }
if(categoryCheck) throw BaseException(INVALID_USER_INTEREST_COUNT)
// category 의 사이즈 확인
val listSize = signUpReq.interestList.size
Expand Down Expand Up @@ -242,7 +243,7 @@ class UserService(
// 관심 목록 변경
@Transactional
fun patchWatchLists(user: User, userInterestListReq: UserInterestListDto) {
val reqLists = userInterestListReq.interestList!!.map { i -> Category.getCategoryByName(i.category) }
val reqLists = userInterestListReq.interestList!!.map { i -> Category.getCategoryByValue(i.category) }
if(reqLists.isEmpty() || reqLists.size > 3) throw BaseException(INVALID_USER_INTEREST_COUNT)
val userWatchLists = userInterestRepository.findByUserAndStatus(user, ACTIVE_STATUS)
val categoryLists = userWatchLists.map { c -> c.category }
Expand Down

0 comments on commit 15c69e4

Please sign in to comment.