Skip to content

Commit

Permalink
Merge pull request #76 from PSR-Co/feat/#60-patchWatchLists
Browse files Browse the repository at this point in the history
[feat] 관심 목록 변경 API
  • Loading branch information
chaerlo127 authored Aug 7, 2023
2 parents f27073c + 8d6a764 commit 7cf5949
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ProductService(
var interestCategoryList: MutableList<Category> = ArrayList()
if(category.isEmpty()) {
// 유저의 관심목록
val userInterestList = userInterestRepository.findByUser(user);
val userInterestList = userInterestRepository.findByUserAndStatus(user, ACTIVE_STATUS);
interestCategoryList = userInterestList.stream().map { ui -> ui.category }.toList()
} else {
interestCategoryList.add(Category.getCategoryByName(category))
Expand Down
10 changes: 10 additions & 0 deletions src/main/kotlin/com/psr/psr/user/controller/UserController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,15 @@ class UserController(
return BaseResponse(BaseResponseCode.SUCCESS)
}

/**
* 관심 목록 변경
*/
@PatchMapping("/watchlists")
@ResponseBody
fun patchWatchLists(@AuthenticationPrincipal userAccount: UserAccount, @RequestBody @Validated userInterestListReq: UserInterestListReq) : BaseResponse<Any>{
userService.patchWatchLists(userAccount.getUser(), userInterestListReq)
return BaseResponse(BaseResponseCode.SUCCESS)
}


}
5 changes: 5 additions & 0 deletions src/main/kotlin/com/psr/psr/user/dto/UserInterestListReq.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.psr.psr.user.dto

data class UserInterestListReq (
val interestList: List<UserInterestReq>
)
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@ class UserAssembler {
nickname = signUpReq.nickname)
}

fun toInterestEntity(user: User, signUpReq: SignUpReq): List<UserInterest> {
fun toInterestListEntity(user: User, signUpReq: SignUpReq): List<UserInterest> {
return signUpReq.interestList.stream()
.map { i ->
UserInterest(category = Category.getCategoryByName(i.category),
user = user)
}.collect(Collectors.toList())
}

fun toInterestEntity(user: User, category: Category): UserInterest {
return UserInterest(user = user,
category = category)
}

fun toBusinessEntity(user: User, signUpReq: SignUpReq): BusinessInfo {
val format = DateTimeFormatter.ofPattern("yyyyMMdd")
return BusinessInfo(user = user,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.psr.psr.user.repository

import com.psr.psr.user.entity.Category
import com.psr.psr.user.entity.User
import com.psr.psr.user.entity.UserInterest
import org.springframework.data.jpa.repository.JpaRepository
Expand All @@ -8,6 +9,7 @@ import org.springframework.stereotype.Repository
@Repository
interface UserInterestRepository: JpaRepository<UserInterest, Long> {

fun findByUser(user: User): List<UserInterest>
fun findByUserAndStatus(user: User, status: String): List<UserInterest>
fun findByUserAndCategory(user: User, category: Category): UserInterest ?

}
32 changes: 31 additions & 1 deletion src/main/kotlin/com/psr/psr/user/service/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.psr.psr.global.jwt.utils.JwtUtils
import com.psr.psr.user.dto.*
import com.psr.psr.user.dto.assembler.UserAssembler
import com.psr.psr.user.dto.eidReq.BusinessListRes
import com.psr.psr.user.entity.Category
import com.psr.psr.user.entity.Type
import com.psr.psr.user.entity.User
import com.psr.psr.user.repository.BusinessInfoRepository
Expand Down Expand Up @@ -67,7 +68,7 @@ class UserService(
signUpReq.password = encodedPassword
// user 저장
val user = userRepository.save(userAssembler.toEntity(signUpReq))
userInterestRepository.saveAll(userAssembler.toInterestEntity(user, signUpReq))
userInterestRepository.saveAll(userAssembler.toInterestListEntity(user, signUpReq))

// 사업자인경우
if (user.type == Type.ENTREPRENEUR){
Expand All @@ -80,6 +81,7 @@ class UserService(
}

// 로그인
@Transactional
fun login(loginReq: LoginReq) : TokenDto{
val user = userRepository.findByEmail(loginReq.email).orElseThrow{BaseException(NOT_EXIST_EMAIL)}
if(!passwordEncoder.matches(loginReq.password, user.password)) throw BaseException(INVALID_PASSWORD)
Expand Down Expand Up @@ -195,6 +197,7 @@ class UserService(
}

// 비밀번호 재설정
@Transactional
fun resetPassword(passwordReq: ResetPasswordReq) {
val user = userRepository.findByEmail(passwordReq.email).orElseThrow{BaseException(NOT_EXIST_EMAIL)}
if(user.phone != passwordReq.phone) throw BaseException(INVALID_PHONE)
Expand All @@ -204,4 +207,31 @@ class UserService(
user.password = passwordEncoder.encode(passwordReq.password)
userRepository.save(user)
}

// 관심 목록 변경
@Transactional
fun patchWatchLists(user: User, userInterestListReq: UserInterestListReq) {
val reqLists = userInterestListReq.interestList.map { i -> Category.getCategoryByName(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 }

// 사용자 관심 목록에 최근 추가한 리스트(REQUEST) 관심 목록이 없다면? => 저장
reqLists.stream()
.forEach { interest ->
if(!categoryLists.contains(interest)) {
// 과거에 삭제했던 경우 / 새롭게 생성하는 경우
val interestE = userInterestRepository.findByUserAndCategory(user, interest)?: userAssembler.toInterestEntity(user, interest)
interestE.status = ACTIVE_STATUS
userInterestRepository.save(interestE)
}
}

// 최근 추가한 리스트(REQUEST)에 사용자 관심 목록이 없다면? => 삭제
userWatchLists.stream()
.forEach { interest ->
// todo: cascade 적용 후 모두 삭제 되었는지 확인 필요
if(!reqLists.contains(interest.category)) userInterestRepository.delete(interest)
}
}
}

0 comments on commit 7cf5949

Please sign in to comment.