Skip to content

Commit

Permalink
MARA-75 : 화면설계서 기반 API 점검 및 리팩토링 2차 (#36)
Browse files Browse the repository at this point in the history
* 친구 목록 조회 응답 값 식자재 개수 추가

* 친구 리스트 조회 정렬 변경

* PageImpl -> PageableExecutionUtils 변경

* count -> count!! 처리

* count ?: 0 -> count!! 처리

* nickName -> nickname 일괄 변경

* NickName JPA method -> Nickname 변경
  • Loading branch information
jhkang1517 authored Feb 21, 2024
1 parent 1c3f582 commit cf5e4ba
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 31 deletions.
4 changes: 4 additions & 0 deletions src/main/kotlin/mara/server/domain/HealthController.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package mara.server.domain

import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@Tag(name = "서버 상태", description = "서버 상태 API")
class HealthController {

@GetMapping("/health-check")
@Operation(summary = "서버 상태 조회 API")
fun healthCheck(): HttpStatus {
return HttpStatus.OK
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ data class FriendRefrigeratorResponse(
val friendRefrigeratorIngredientGroupList: List<FriendRefrigeratorIngredient>
) {
constructor(user: User, refrigerator: Refrigerator, ingredientList: List<Ingredient>) : this(
nickname = user.nickName,
nickname = user.nickname,
refrigeratorId = refrigerator.refrigeratorId,
friendRefrigeratorIngredientGroupList = ingredientList.map { FriendRefrigeratorIngredient(it) }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class FriendshipController(
@Operation(summary = "친구 조회 API")
fun getFriendshipList(
@PageableDefault(
size = 10
size = 10,
sort = ["createdAt"]
)
pageable: Pageable
): CommonResponse<Page<UserFriendResponse>> {
Expand Down
33 changes: 26 additions & 7 deletions src/main/kotlin/mara/server/domain/friend/FriendshipRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package mara.server.domain.friend

import com.querydsl.jpa.impl.JPAQueryFactory
import mara.server.domain.friend.QFriendship.friendship
import mara.server.domain.user.QUser
import mara.server.domain.user.User
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageImpl
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.support.PageableExecutionUtils
import java.util.Optional

interface FriendshipRepository : JpaRepository<Friendship, Long>, CustomFriendshipRepository {
Expand All @@ -30,15 +31,33 @@ class CustomFriendshipRepositoryImpl(
private val queryFactory: JPAQueryFactory
) : CustomFriendshipRepository {

private val createdAt: String = "createdAt"
private val nickname: String = "nickname"

override fun findByFromUserPage(user: User, pageable: Pageable): Page<Friendship> {
val results = queryFactory.select(friendship).from(friendship).where(friendship.fromUser.eq(user))
.offset(pageable.offset).limit(pageable.pageSize.toLong()).fetch()
val appUser = QUser.user
val query = queryFactory.select(friendship).from(friendship).innerJoin(friendship.toUser, appUser).on(
friendship.toUser.userId.eq(appUser.userId)
).where(friendship.fromUser.eq(user))
.offset(pageable.offset).limit(pageable.pageSize.toLong())

pageable.sort.forEach { order ->
val path = when (order.property) {
createdAt -> friendship.createdAt.asc()
nickname -> appUser.nickname.asc()
else -> throw IllegalArgumentException("Unsupported sorting property: ${order.property}")
}
query.orderBy(path)
}

val results = query.fetch()

val count = queryFactory.select(friendship.count()).from(friendship)
.where(friendship.fromUser.eq(user))
.offset(pageable.offset).limit(pageable.pageSize.toLong()).orderBy(friendship.createdAt.asc()).fetchOne() ?: 0
val count = queryFactory.select(friendship.count()).from(friendship).innerJoin(friendship.toUser, appUser).on(
friendship.toUser.userId.eq(appUser.userId)
).where(friendship.fromUser.eq(user))
.offset(pageable.offset).limit(pageable.pageSize.toLong()).fetchOne()

return PageImpl(results, pageable, count)
return PageableExecutionUtils.getPage(results, pageable) { count!! }
}

override fun findByFromUserAndToUser(fromUser: User, toUser: User): List<Friendship> {
Expand Down
14 changes: 9 additions & 5 deletions src/main/kotlin/mara/server/domain/friend/FriendshipService.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package mara.server.domain.friend

import mara.server.domain.ingredient.IngredientDetailRepository
import mara.server.domain.refrigerator.RefrigeratorRepository
import mara.server.domain.user.User
import mara.server.domain.user.UserFriendResponse
import mara.server.domain.user.UserRepository
Expand All @@ -13,7 +15,9 @@ import org.springframework.transaction.annotation.Transactional
class FriendshipService(
private val friendshipRepository: FriendshipRepository,
private val userRepository: UserRepository,
private val userService: UserService
private val userService: UserService,
private val refrigeratorRepository: RefrigeratorRepository,
private val ingredientDetailRepository: IngredientDetailRepository
) {

private val ok = "ok"
Expand All @@ -37,10 +41,10 @@ class FriendshipService(
val currentLoginUser = userService.getCurrentLoginUser()
val friendshipList = friendshipRepository.findByFromUserPage(currentLoginUser, pageable)
val userFriendResponseList = friendshipList.map { friendship ->
val userId = friendship.toUser.userId
val user =
userRepository.findById(userId).orElseThrow { NoSuchElementException("해당 유저가 존재하지 않습니다. ID: $userId") }
UserFriendResponse(user)
val user = friendship.toUser
val refrigeratorList = refrigeratorRepository.findByUser(user)
val ingredientCount = ingredientDetailRepository.countByRefrigeratorList(refrigeratorList)
UserFriendResponse(user, ingredientCount)
}

return userFriendResponseList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import com.querydsl.jpa.impl.JPAQueryFactory
import mara.server.domain.ingredient.QIngredientDetail.ingredientDetail
import mara.server.domain.refrigerator.Refrigerator
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageImpl
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.support.PageableExecutionUtils
import java.time.LocalDateTime

interface IngredientDetailRepository : JpaRepository<IngredientDetail, Long>, CustomIngredientDetailRepository
Expand All @@ -28,6 +28,10 @@ interface CustomIngredientDetailRepository {
limit: Long
): List<IngredientDetail>

fun countByRefrigeratorList(
refrigeratorList: List<Refrigerator>,
): Long

fun countByRefrigeratorListAndExpirationDay(
refrigeratorList: List<Refrigerator>,
expirationDay: LocalDateTime
Expand All @@ -53,9 +57,9 @@ class CustomIngredientDetailRepositoryImpl(

val count = queryFactory.select(ingredientDetail.count()).from(ingredientDetail)
.where(ingredientDetail.refrigerator.eq(refrigerator).and(ingredientDetail.isDeleted.isFalse))
.offset(pageable.offset).limit(pageable.pageSize.toLong()).fetchOne() ?: 0
.offset(pageable.offset).limit(pageable.pageSize.toLong()).fetchOne()

return PageImpl(results, pageable, count)
return PageableExecutionUtils.getPage(results, pageable) { count!! }
}

override fun findByRefrigeratorList(refrigeratorList: List<Refrigerator>, limit: Long): List<IngredientDetail> {
Expand All @@ -64,6 +68,15 @@ class CustomIngredientDetailRepositoryImpl(
.orderBy(ingredientDetail.expirationDate.desc()).limit(limit).fetch()
}

override fun countByRefrigeratorList(refrigeratorList: List<Refrigerator>): Long {
val count = queryFactory.select(ingredientDetail.count()).from(ingredientDetail).where(
ingredientDetail.refrigerator.`in`(refrigeratorList)
.and(ingredientDetail.isDeleted.isFalse)
).fetchOne()

return count!!
}

override fun countByRefrigeratorListAndExpirationDay(
refrigeratorList: List<Refrigerator>,
expirationDay: LocalDateTime
Expand All @@ -75,6 +88,6 @@ class CustomIngredientDetailRepositoryImpl(
.and(ingredientDetail.isDeleted.isFalse)
).fetchOne()

return count ?: 0
return count!!
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/mara/server/domain/share/ShareService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ShareService(
fun getAllApplyUserList(shareId: Long): List<String>? {
val share = getShare(shareId)
val applyShareList = share.applyShareList
return applyShareList.map { it.user.nickName }.toList()
return applyShareList.map { it.user.nickname }.toList()
}

fun getAllMyAppliedShareList(pageable: Pageable, status: String): Page<ShareResponse>? {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/mara/server/domain/user/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ enum class Role { USER }
@Entity
@Table(name = "app_user")
class User(
val nickName: String,
val nickname: String,
val password: String,
val kakaoId: Long?,
val kakaoEmail: String?,
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/mara/server/domain/user/UserController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class UserController(

@GetMapping("/nickname/check")
@Operation(summary = "닉네임 중복 체크 API")
fun checkNickname(@RequestParam("nickname") nickname: String): CommonResponse<CheckDuplicateResponse> = success(userService.checkNickName(nickname))
fun checkNickname(@RequestParam("nickname") nickname: String): CommonResponse<CheckDuplicateResponse> = success(userService.checkNickname(nickname))

@GetMapping("/kakao-login")
@Operation(summary = "카카오 로그인 API")
Expand Down
14 changes: 8 additions & 6 deletions src/main/kotlin/mara/server/domain/user/UserDto.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package mara.server.domain.user

data class UserRequest(
val nickName: String,
val nickname: String,
val kakaoId: Long?,
val kakaoEmail: String?,
val googleEmail: String?,
Expand All @@ -12,14 +12,14 @@ data class CheckDuplicateResponse(val isDuplicated: Boolean)

data class RefreshAccessTokenRequest(val refreshToken: String)
class UserResponse(
val nickName: String?,
val nickname: String?,
val kakaoId: Long?,
val kakaoEmail: String?,
val googleEmail: String?,
val profileImage: String?,
) {
constructor(user: User) : this(
nickName = user.nickName,
nickname = user.nickname,
kakaoId = user.kakaoId,
googleEmail = user.googleEmail,
kakaoEmail = user.kakaoEmail,
Expand All @@ -37,10 +37,12 @@ class UserInviteCodeResponse(

class UserFriendResponse(
val userId: Long,
val nickName: String
val nickname: String,
val ingredientCount: Long
) {
constructor(user: User) : this(
constructor(user: User, ingredientCount: Long) : this(
userId = user.userId,
nickName = user.nickName
nickname = user.nickname,
ingredientCount = ingredientCount
)
}
2 changes: 1 addition & 1 deletion src/main/kotlin/mara/server/domain/user/UserRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface UserRepository : JpaRepository<User, Long>, CustomUserRepository {
fun findByKakaoId(id: Long): User?
fun findByGoogleEmail(email: String): User?
fun findByInviteCode(inviteCode: String): Optional<User>
fun existsByNickName(nickName: String): Boolean
fun existsByNickname(nickname: String): Boolean
}

interface CustomUserRepository {
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/mara/server/domain/user/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ class UserService(
}
private fun createUser(userRequest: UserRequest): User {
val user = User(
nickName = userRequest.nickName,
nickname = userRequest.nickname,
kakaoId = userRequest.kakaoId,
password = passwordEncoder.encode(userRequest.nickName),
password = passwordEncoder.encode(userRequest.nickname),
googleEmail = userRequest.googleEmail,
kakaoEmail = userRequest.kakaoEmail,
profileImage = ProfileImage.valueOf(userRequest.profileImage),
Expand All @@ -62,7 +62,7 @@ class UserService(
return userRepository.save(user)
}

fun checkNickName(nickName: String): CheckDuplicateResponse = CheckDuplicateResponse(userRepository.existsByNickName(nickName))
fun checkNickname(nickname: String): CheckDuplicateResponse = CheckDuplicateResponse(userRepository.existsByNickname(nickname))

fun kakaoLogin(authorizedCode: String): AuthDto {
// 리다이랙트 url 환경 따라 다르게 전달하기 위한 구분 값
Expand Down

0 comments on commit cf5e4ba

Please sign in to comment.