Skip to content

Commit

Permalink
[DPMBE-106] user profile 수정 시 사용하지 않는 이미지는 삭제한다 (#161)
Browse files Browse the repository at this point in the history
* feat: user profile 수정 시 사용하지 않는 이미지는 삭제한다

* remove: imageFileExtension 사용하지 않아서 제거

* refactor: UserProfileImageUpdatedEvent 발행 Domain으로 이동하고 Kotlin takeIf 문법 사용

* style: spotless

* style: 주석해제 & s3Service로 이름 변경
  • Loading branch information
kdomo committed Jul 7, 2023
1 parent af55f68 commit fb07499
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import com.depromeet.whatnow.config.security.SecurityUtils

@UseCase
class GetPresignedUrlUseCase(
val presignedUrlService: S3Service,
val s3Service: S3Service,
) {
fun forPromise(promiseId: Long, fileExtension: ImageFileExtension): ImageUrlResponse {
return ImageUrlResponse.from(presignedUrlService.getPresignedUrlForPromise(promiseId, fileExtension))
return ImageUrlResponse.from(s3Service.getPresignedUrlForPromise(promiseId, fileExtension))
}

fun forUser(fileExtension: ImageFileExtension): ImageUrlResponse {
val currentUserId = SecurityUtils.currentUserId
return ImageUrlResponse.from(presignedUrlService.getPresignedUrlForUser(currentUserId, fileExtension))
return ImageUrlResponse.from(s3Service.getPresignedUrlForUser(currentUserId, fileExtension))
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.depromeet.whatnow.api.user.dto.request

data class UpdateProfileRequest(
val profileImage: String,
val username: String,
val profileImage: String,
val isDefaultImg: Boolean,
val imageKey: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ class UpdateUserUseCase(

fun updateProfile(updateProfileRequest: UpdateProfileRequest): UserDetailVo {
val currentUserId: Long = SecurityUtils.currentUserId
return userDomainService.updateProfile(currentUserId, updateProfileRequest.profileImage, updateProfileRequest.username)
return userDomainService.updateProfile(
currentUserId,
updateProfileRequest.profileImage,
updateProfileRequest.username,
updateProfileRequest.isDefaultImg,
updateProfileRequest.imageKey,
)
.toUserDetailVo()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const val RADIUS_WAIT_CONFIRM = 200

const val IMAGE_DOMAIN = "https://image.whatnow.kr"
const val ASSERT_IMAGE_DOMAIN = "https://image.whatnow.kr/assert"
const val USER_DEFAULT_PROFILE_IMAGE = "https://image.whatnow.kr/assert/users/default.svg"

const val REDIS_EXPIRE_EVENT_PATTERN = "__keyevent@*__:expired"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class UserImageAdapter(
return userImageRepository.save(userImage)
}

fun findAllByUserId(userId: Long): List<UserImage> {
return userImageRepository.findAllByUserId(userId)
}

fun findByUserIdAndImageKey(userId: Long, imageKey: String): UserImage {
return userImageRepository.findByUserIdAndImageKey(userId, imageKey) ?: run { throw UserImageNotFoundException.EXCEPTION }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.springframework.stereotype.Repository

@Repository
interface UserImageRepository : JpaRepository<UserImage, Long> {
fun findAllByUserId(userId: Long): List<UserImage>
fun findByUserIdAndImageKey(userId: Long, imageKey: String): UserImage?
fun deleteByImageKeyAndUserId(imageKey: String, userId: Long)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import com.depromeet.whatnow.common.BaseTimeEntity
import com.depromeet.whatnow.common.aop.event.Events
import com.depromeet.whatnow.common.vo.UserDetailVo
import com.depromeet.whatnow.common.vo.UserInfoVo
import com.depromeet.whatnow.consts.USER_DEFAULT_PROFILE_IMAGE
import com.depromeet.whatnow.domains.user.exception.AlreadyDeletedUserException
import com.depromeet.whatnow.domains.user.exception.ForbiddenUserException
import com.depromeet.whatnow.events.domainEvent.UserProfileImageUpdatedEvent
import com.depromeet.whatnow.events.domainEvent.UserSignUpEvent
import java.time.LocalDateTime
import javax.persistence.Column
Expand Down Expand Up @@ -91,12 +93,14 @@ class User(
fcmNotification = FcmNotificationVo.updateToken(fcmNotification, fcmToken)
}

fun updateProfile(profileImage: String, username: String) {
if (profileImage != profileImg) {
isDefaultImg = false
fun updateProfile(profileImage: String, username: String, isDefaultImage: Boolean, imageKey: String) {
this.nickname = username
this.isDefaultImg = isDefaultImage
this.profileImg = profileImage.takeIf { !isDefaultImage } ?: USER_DEFAULT_PROFILE_IMAGE

if (!isDefaultImage) {
Events.raise(UserProfileImageUpdatedEvent(this.id!!, imageKey))
}
profileImg = profileImage
nickname = username
}

fun toUserInfoVo(): UserInfoVo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ class UserDomainService(
}

@Transactional
fun updateProfile(currentUserId: Long, profileImage: String, username: String): User {
fun updateProfile(currentUserId: Long, profileImage: String, username: String, isDefaultImage: Boolean, imageKey: String): User {
val user = userAdapter.queryUser(currentUserId)
user.updateProfile(profileImage, username)
user.updateProfile(profileImage, username, isDefaultImage, imageKey)
return user
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.depromeet.whatnow.events.domainEvent

import com.depromeet.whatnow.common.aop.event.DomainEvent

class UserProfileImageUpdatedEvent(
val userId: Long,
val imageKey: String,
) : DomainEvent()
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.depromeet.whatnow.events.handler

import com.depromeet.whatnow.annotation.Handler
import com.depromeet.whatnow.config.s3.S3Service
import com.depromeet.whatnow.domains.image.adapter.UserImageAdapter
import com.depromeet.whatnow.events.domainEvent.UserProfileImageUpdatedEvent
import org.springframework.scheduling.annotation.Async
import org.springframework.transaction.event.TransactionPhase
import org.springframework.transaction.event.TransactionalEventListener

@Handler
class UserProfileImageUpdatedEventHandler(
val s3Service: S3Service,
val userImageAdapter: UserImageAdapter,
) {
@Async
@TransactionalEventListener(classes = [UserProfileImageUpdatedEvent::class], phase = TransactionPhase.AFTER_COMMIT)
fun handleRegisterUserEvent(userProfileImageUpdatedEvent: UserProfileImageUpdatedEvent) {
val userId = userProfileImageUpdatedEvent.userId
val imageKey = userProfileImageUpdatedEvent.imageKey

userImageAdapter.findAllByUserId(userId)
.filter { it.imageKey != imageKey }
.map {
s3Service.deleteForUser(userId, it.imageKey, it.fileExtension)
}
}
}

0 comments on commit fb07499

Please sign in to comment.