Skip to content

Commit

Permalink
MARA-87 : 최종 배포 전 코드 리팩토링 (#48)
Browse files Browse the repository at this point in the history
* Optional<> 제거

* dev 환경 log level info -> warn 변경

* S3 Service 적재 예외 경우 추가
  • Loading branch information
jhkang1517 authored Feb 26, 2024
1 parent abe0ebb commit aadf7d5
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class FriendRefrigeratorService(

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

val userList = friendshipList.map { it.toUser }
val refrigeratorList = refrigeratorRepository.findByUserList(userList, 5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ class FriendshipController(
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> {
Expand Down
16 changes: 0 additions & 16 deletions src/main/kotlin/mara/server/domain/friend/FriendshipDto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,3 @@ data class FriendshipRequest(
data class FriendshipDeleteRequest(
val friendId: Long
)

data class FriendshipResponse(
val friendshipId: Long,
val fromFriend: Long,
val toFriend: Long
) {
constructor(friendship: Friendship) : this(
friendshipId = friendship.friendshipId,
fromFriend = friendship.fromUser.userId,
toFriend = friendship.toUser.userId
)
}

fun List<Friendship>.toFriendshipResponseList(): List<FriendshipResponse> {
return this.map { FriendshipResponse(it) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import org.springframework.data.domain.Page
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 {
fun findByFromUser(
user: User,
): Optional<List<Friendship>>
): List<Friendship>?

fun countByFromUser(
user: User
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class FriendshipService(
val fromUser = userRepository.findById(currentUserId)
.orElseThrow { NoSuchElementException("해당 유저가 존재하지 않습니다. ID: $currentUserId") }
val toUser = userRepository.findByInviteCode(friendshipRequest.inviteCode)
.orElseThrow { NoSuchElementException("해당 초대코드를 가진 사용자가 존재하지 않습니다.") }
?: throw NoSuchElementException("해당 초대코드를 가진 사용자가 존재하지 않습니다. 초대코드: ${friendshipRequest.inviteCode}")

addFriendship(fromUser, toUser)
addFriendship(toUser, fromUser)
Expand All @@ -50,11 +50,6 @@ class FriendshipService(
return userFriendResponseList
}

fun getFriendshipCount(): Long {
val currentLoginUser = userService.getCurrentLoginUser()
return friendshipRepository.countByFromUser(currentLoginUser)
}

@Transactional
fun deleteFriendship(friendshipDeleteRequestList: List<FriendshipDeleteRequest>): String {
val currentLoginUser = userService.getCurrentLoginUser()
Expand Down
9 changes: 6 additions & 3 deletions src/main/kotlin/mara/server/domain/s3/S3Service.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ class S3Service(
private val dir: String
) {
@Transactional
fun upload(file: MultipartFile, customDir: String? = dir): String {
fun upload(file: MultipartFile, customDir: String?): String {
val resultDir = customDir ?: dir
if (resultDir.startsWith("/")) throw IllegalArgumentException("올바르지 않은 경로입니다. $resultDir")

val fileName = UUID.randomUUID().toString() + "-" + file.originalFilename
val objMeta = ObjectMetadata()

Expand All @@ -31,10 +34,10 @@ class S3Service(
val byteArrayIs = ByteArrayInputStream(bytes)

s3Client.putObject(
PutObjectRequest(bucket, dir + fileName, byteArrayIs, objMeta)
PutObjectRequest(bucket, resultDir + fileName, byteArrayIs, objMeta)
.withCannedAcl(CannedAccessControlList.PublicRead)
)

return s3Client.getUrl(bucket, dir + fileName).toString()
return s3Client.getUrl(bucket, resultDir + fileName).toString()
}
}
3 changes: 1 addition & 2 deletions src/main/kotlin/mara/server/domain/user/UserRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package mara.server.domain.user
import com.querydsl.jpa.impl.JPAQueryFactory
import mara.server.domain.user.QUser.user
import org.springframework.data.jpa.repository.JpaRepository
import java.util.Optional

interface UserRepository : JpaRepository<User, Long>, CustomUserRepository {

fun findByKakaoId(id: Long): User?
fun findByGoogleEmail(email: String): User?
fun findByInviteCode(inviteCode: String): Optional<User>
fun findByInviteCode(inviteCode: String): User?
fun existsByNickname(nickname: String): Boolean
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ jwt:

logging:
level:
root: info
root: warn
mara.server: TRACE

0 comments on commit aadf7d5

Please sign in to comment.