Skip to content

Commit

Permalink
ISSUE-42 : 사용자 식자재 수, 나눔 수, 친구 수 조회 API 추가 개발 (#46)
Browse files Browse the repository at this point in the history
* createIngredientDetail isDeleted false 고정값 설정

* expirationDate.desc() -> asc() 변경

* 사용자 식자재 수, 나눔 수, 친구 수 조회 API 구현
  • Loading branch information
jhkang1517 authored Feb 24, 2024
1 parent 1eeac6f commit fb718d1
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class CustomIngredientDetailRepositoryImpl(
override fun findByRefrigeratorList(refrigeratorList: List<Refrigerator>, limit: Long): List<IngredientDetail> {
return queryFactory.selectFrom(ingredientDetail)
.where(ingredientDetail.refrigerator.`in`(refrigeratorList).and(ingredientDetail.isDeleted.isFalse))
.orderBy(ingredientDetail.expirationDate.desc()).limit(limit).fetch()
.orderBy(ingredientDetail.expirationDate.asc()).limit(limit).fetch()
}

override fun countByRefrigeratorList(refrigeratorList: List<Refrigerator>): Long {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class IngredientDetailService(
memo = ingredientDetailRequest.memo,
addDate = ingredientDetailRequest.addDate,
expirationDate = ingredientDetailRequest.expirationDate,
isDeleted = ingredientDetailRequest.isDeleted
isDeleted = false
)

// 식재료 추가 일자 update
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ import org.springframework.data.jpa.repository.JpaRepository

interface ApplyShareRepository : JpaRepository<ApplyShare, Long> {
fun existsByUserAndShare(user: User, share: Share): Boolean

fun countByUser(user: User): Long
}
4 changes: 3 additions & 1 deletion src/main/kotlin/mara/server/domain/share/ShareRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.support.PageableExecutionUtils

interface ShareRepository : JpaRepository<Share, Long>, CustomShareRepository
interface ShareRepository : JpaRepository<Share, Long>, CustomShareRepository {
fun countByUser(user: User): Long
}

interface CustomShareRepository {
fun findAllMyFriendsShare(pageable: Pageable, status: ShareStatus, user: User): Page<Share>
Expand Down
6 changes: 6 additions & 0 deletions src/main/kotlin/mara/server/domain/user/UserController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,10 @@ class UserController(
fun getAllMyShareList(pageable: Pageable, @RequestParam("status") status: String): CommonResponse<Page<ShareResponse>> {
return success(shareService.getAllMyShareList(pageable, status))
}

@GetMapping("/me/statistics")
@Operation(summary = "유저의 식자재 수, 나눔 수, 친구 수 조회 API")
fun getCountMyStatistics(): CommonResponse<UserStatisticResponse> {
return success(userService.getCountMyStatistics())
}
}
6 changes: 6 additions & 0 deletions src/main/kotlin/mara/server/domain/user/UserDto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@ class UserFriendResponse(
ingredientCount = ingredientCount
)
}

class UserStatisticResponse(
val ingredientCount: Long,
val shareCount: Long,
val friendCount: Long
)
19 changes: 19 additions & 0 deletions src/main/kotlin/mara/server/domain/user/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import mara.server.auth.kakao.KakaoApiClient
import mara.server.auth.security.getCurrentLoginUserId
import mara.server.config.redis.RefreshToken
import mara.server.config.redis.RefreshTokenRepository
import mara.server.domain.friend.FriendshipRepository
import mara.server.domain.ingredient.IngredientDetailRepository
import mara.server.domain.refrigerator.RefrigeratorRepository
import mara.server.domain.share.ApplyShareRepository
import mara.server.domain.share.ShareRepository
import mara.server.util.StringUtil
import mara.server.util.logger
import org.springframework.beans.factory.annotation.Value
Expand All @@ -19,6 +24,11 @@ import java.util.UUID
@Service
class UserService(
private val userRepository: UserRepository,
private val refrigeratorRepository: RefrigeratorRepository,
private val ingredientDetailRepository: IngredientDetailRepository,
private val shareRepository: ShareRepository,
private val applyShareRepository: ApplyShareRepository,
private val friendshipRepository: FriendshipRepository,
private val jwtProvider: JwtProvider,
private val passwordEncoder: BCryptPasswordEncoder,
private val refreshTokenRepository: RefreshTokenRepository,
Expand Down Expand Up @@ -118,4 +128,13 @@ class UserService(
val refreshToken = refreshTokenRepository.save(RefreshToken(UUID.randomUUID().toString(), user.userId, refreshDurationMins))
return refreshToken.refreshToken
}

fun getCountMyStatistics(): UserStatisticResponse {
val user = getCurrentLoginUser()
val refrigeratorList = refrigeratorRepository.findByUser(user)
val ingredientCount = ingredientDetailRepository.countByRefrigeratorList(refrigeratorList)
val shareCount = shareRepository.countByUser(user) + applyShareRepository.countByUser(user)
val friendCount = friendshipRepository.countByFromUser(user)
return UserStatisticResponse(ingredientCount, shareCount, friendCount)
}
}

0 comments on commit fb718d1

Please sign in to comment.