Skip to content

Commit

Permalink
MARA-67 : Figma 화면설계서 기반 기능 점검을 통한 리팩토링 작업 수행 (#28)
Browse files Browse the repository at this point in the history
* 냉장고 도메인 관련 코드 리팩토링

* 식자재 API documentation 추가

* 식자재 상세 API document 적용

* refrigerator QueryDSL 적용

* QueryDSL IngredientDetail 적용

* S3 API Documentation 추가

* Friendship API Documentation 추가

* FriendRefrigerator API Documentation 추가

* 불필요한 Pageable 사용 API 정리

* 친구 목록 조회 Pageable 추가

* S3 upload @transactional 적용

* kilintFormat 적용

* queryDSL 설정 변경

* IngredientDetail QueryDSL 재적용

* Refrigerator QueryDSL 적용

* friendship QueryDSL 적용

* orderBy 컬럼 추가

* 친구조회 등록일 순 정렬 추가

* ktlint 재적용

* query -> queryFactory 변경

---------

Co-authored-by: JUNGHO KANG <[email protected]>
  • Loading branch information
jhkang1517 and JUNGHO KANG authored Feb 20, 2024
1 parent 53edd17 commit 1bcd93f
Show file tree
Hide file tree
Showing 21 changed files with 269 additions and 122 deletions.
20 changes: 0 additions & 20 deletions src/main/kotlin/mara/server/config/pageable/PageableConfig.kt

This file was deleted.

17 changes: 17 additions & 0 deletions src/main/kotlin/mara/server/config/querydsl/QueryDslConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package mara.server.config.querydsl

import com.querydsl.jpa.impl.JPAQueryFactory
import jakarta.persistence.EntityManager
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class QueryDslConfig(
private val em: EntityManager
) {

@Bean
fun jpaQueryFactory(): JPAQueryFactory {
return JPAQueryFactory(em)
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
package mara.server.domain.friend

import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import mara.server.common.CommonResponse
import mara.server.common.success
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/friend-refrigs")
@Tag(name = "친구 냉장고", description = "친구 냉장고 API")
class FriendRefrigeratorController(
private val friendRefrigeratorService: FriendRefrigeratorService
) {

@GetMapping("/recent")
fun getRecentFriendRefrigeratorList(
@Qualifier("userPageable")
userPageable: Pageable,
@Qualifier("ingredientPageable")
ingredientPageable: Pageable,
): CommonResponse<Page<FriendRefrigeratorResponse>> {
return success(friendRefrigeratorService.getRecentFriendRefrigeratorList(userPageable, ingredientPageable))
@Operation(summary = "친구 냉장고 최신 근황 조회 API")
fun getRecentFriendRefrigeratorList(): CommonResponse<List<FriendRefrigeratorResponse>> {
return success(friendRefrigeratorService.getRecentFriendRefrigeratorList())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package mara.server.domain.friend
import mara.server.domain.ingredient.Ingredient
import mara.server.domain.refrigerator.Refrigerator
import mara.server.domain.user.User
import org.springframework.data.domain.Page

data class FriendRefrigeratorResponse(
val nickname: String,
val refrigeratorId: Long,
val friendRefrigeratorIngredientGroupList: Page<FriendRefrigeratorIngredient>
val friendRefrigeratorIngredientGroupList: List<FriendRefrigeratorIngredient>
) {
constructor(user: User, refrigerator: Refrigerator, ingredientList: Page<Ingredient>) : this(
constructor(user: User, refrigerator: Refrigerator, ingredientList: List<Ingredient>) : this(
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 @@ -3,8 +3,6 @@ package mara.server.domain.friend
import mara.server.domain.ingredient.IngredientDetailRepository
import mara.server.domain.refrigerator.RefrigeratorRepository
import mara.server.domain.user.UserService
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service

@Service
Expand All @@ -15,17 +13,17 @@ class FriendRefrigeratorService(
private val ingredientDetailRepository: IngredientDetailRepository
) {

fun getRecentFriendRefrigeratorList(userPageable: Pageable, ingredientPageable: Pageable): Page<FriendRefrigeratorResponse> {
fun getRecentFriendRefrigeratorList(): List<FriendRefrigeratorResponse> {
val currentLoginUser = userService.getCurrentLoginUser()
val friendshipList = friendshipRepository.findAllByFromUser(currentLoginUser)
val friendshipList = friendshipRepository.findByFromUser(currentLoginUser)
.orElseThrow { NoSuchElementException("친구 관계가 존재하지 않습니다.") }

val userList = friendshipList.map { it.toUser }
val refrigeratorList = refrigeratorRepository.findRefrigeratorByUserIn(userList, userPageable)
val refrigeratorList = refrigeratorRepository.findByUserList(userList, 5)

val friendRefrigeratorResponseList = refrigeratorList.map { refrig ->
val ingredientDetailList = ingredientDetailRepository
.findByRefrigeratorAndIsDeletedIsFalse(refrig, ingredientPageable)
val ingredientDetailList =
ingredientDetailRepository.findByRefrigerator(refrig, 4)
val ingredientList = ingredientDetailList.map { it.ingredient }
FriendRefrigeratorResponse(refrig.user, refrig, ingredientList)
}
Expand Down
19 changes: 17 additions & 2 deletions src/main/kotlin/mara/server/domain/friend/FriendshipController.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package mara.server.domain.friend

import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import mara.server.common.CommonResponse
import mara.server.common.success
import mara.server.domain.user.UserFriendResponse
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.web.PageableDefault
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
Expand All @@ -11,26 +16,36 @@ import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/friendship")
@Tag(name = "친구", description = "친구 API")
class FriendshipController(
private val friendshipService: FriendshipService
) {

@PostMapping
@Operation(summary = "친구 맺기 API")
fun createFriendship(@RequestBody friendshipRequest: FriendshipRequest): CommonResponse<String> {
return success(friendshipService.createFriendship(friendshipRequest))
}

@GetMapping
fun getFriendshipList(): CommonResponse<List<UserFriendResponse>> {
return success(friendshipService.getFriendshipList())
@Operation(summary = "친구 조회 API")
fun getFriendshipList(
@PageableDefault(
size = 10
)
pageable: Pageable
): CommonResponse<Page<UserFriendResponse>> {
return success(friendshipService.getFriendshipList(pageable))
}

@GetMapping("/count")
@Operation(summary = "친구 수 조회 API")
fun getFriendshipCount(): CommonResponse<Long> {
return success(friendshipService.getFriendshipCount())
}

@PostMapping("/delete")
@Operation(summary = "친구 삭제 API")
fun deleteFriendship(@RequestBody friendshipDeleteRequestList: List<FriendshipDeleteRequest>): CommonResponse<String> {
return success(friendshipService.deleteFriendship(friendshipDeleteRequestList))
}
Expand Down
50 changes: 37 additions & 13 deletions src/main/kotlin/mara/server/domain/friend/FriendshipRepository.kt
Original file line number Diff line number Diff line change
@@ -1,26 +1,50 @@
package mara.server.domain.friend

import com.querydsl.jpa.impl.JPAQueryFactory
import mara.server.domain.friend.QFriendship.friendship
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.jpa.repository.Query
import org.springframework.stereotype.Repository
import java.util.Optional

@Repository
interface FriendshipRepository : JpaRepository<Friendship, Long> {

@Query("select f from Friendship f where f.fromUser = ?1")
fun findAllByFromUser(
user: User
interface FriendshipRepository : JpaRepository<Friendship, Long>, CustomFriendshipRepository {
fun findByFromUser(
user: User,
): Optional<List<Friendship>>

fun countByFromUser(
user: User
): Long
}

@Query("select f from Friendship f where (f.fromUser = ?1 and f.toUser = ?2) or (f.fromUser = ?2 and f.toUser = ?1)")
fun findAllByFromUserAndToUser(
fromUser: User,
toUser: User
): Optional<List<Friendship>>
interface CustomFriendshipRepository {

fun findByFromUserPage(user: User, pageable: Pageable): Page<Friendship>

fun findByFromUserAndToUser(fromUser: User, toUser: User): List<Friendship>
}

class CustomFriendshipRepositoryImpl(
private val queryFactory: JPAQueryFactory
) : CustomFriendshipRepository {

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 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

return PageImpl(results, pageable, count)
}

override fun findByFromUserAndToUser(fromUser: User, toUser: User): List<Friendship> {
return queryFactory.selectFrom(friendship).where(
friendship.fromUser.eq(fromUser).and(friendship.toUser.eq(toUser))
.or(friendship.fromUser.eq(toUser).and(friendship.toUser.eq(fromUser)))
).fetch()
}
}
14 changes: 6 additions & 8 deletions src/main/kotlin/mara/server/domain/friend/FriendshipService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import mara.server.domain.user.User
import mara.server.domain.user.UserFriendResponse
import mara.server.domain.user.UserRepository
import mara.server.domain.user.UserService
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

Expand Down Expand Up @@ -31,12 +33,10 @@ class FriendshipService(
return ok
}

fun getFriendshipList(): List<UserFriendResponse> {
fun getFriendshipList(pageable: Pageable): Page<UserFriendResponse> {
val currentLoginUser = userService.getCurrentLoginUser()
val friendshipList = friendshipRepository.findAllByFromUser(currentLoginUser)
.orElseThrow { NoSuchElementException("친구 관계가 존재하지 않습니다.") }

val userFriendResponseList: List<UserFriendResponse> = friendshipList.map { friendship ->
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") }
Expand All @@ -59,9 +59,7 @@ class FriendshipService(
val targetUser = userRepository.findById(friendshipDeleteRequest.friendId)
.orElseThrow { NoSuchElementException("해당 유저가 존재하지 않습니다. ID: ${friendshipDeleteRequest.friendId}") }

val friendshipList = friendshipRepository.findAllByFromUserAndToUser(currentLoginUser, targetUser)
.orElseThrow { NoSuchElementException("친구 관계가 존재하지 않습니다.") }

val friendshipList = friendshipRepository.findByFromUserAndToUser(currentLoginUser, targetUser)
friendshipList.forEach { friendshipRepository.delete(it) }
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package mara.server.domain.ingredient

import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import mara.server.common.CommonResponse
import mara.server.common.success
import org.springframework.web.bind.annotation.DeleteMapping
Expand All @@ -13,31 +15,37 @@ import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/ingrs")
@Tag(name = "식자재", description = "식자재 API")
class IngredientController(
private val ingredientService: IngredientService
) {

@PostMapping
@Operation(summary = "식자재 생성 API")
fun createIngredient(@RequestBody ingredientRequest: IngredientRequest): CommonResponse<Long> {
return success(ingredientService.createIngredient(ingredientRequest))
}

@GetMapping("/{id}")
@Operation(summary = "식자재 조회 API")
fun getIngredient(@PathVariable(name = "id") id: Long): CommonResponse<IngredientResponse> {
return success(ingredientService.getIngredient(id))
}

@GetMapping
@Operation(summary = "식자재 리스트 조회 API")
fun getIngredientList(): CommonResponse<List<IngredientResponse>> {
return success(ingredientService.getIngredientList())
}

@GetMapping("/category")
@Operation(summary = "카테고리별 식자재 리스트 조회 API")
fun getIngredientGroupListByCategory(): CommonResponse<List<IngredientGroupResponse>> {
return success(ingredientService.getIngredientListByCategory())
}

@PutMapping("/{id}")
@Operation(summary = "식자재 업데이트 API")
fun updateIngredient(
@PathVariable(name = "id") id: Long,
@RequestBody ingredientRequest: IngredientRequest
Expand All @@ -46,6 +54,7 @@ class IngredientController(
}

@DeleteMapping("/{id}")
@Operation(summary = "식자재 삭제 조회 API")
fun deleteIngredient(@PathVariable(name = "id") id: Long): CommonResponse<String> {
return success(ingredientService.deleteIngredient(id))
}
Expand Down
Loading

0 comments on commit 1bcd93f

Please sign in to comment.