Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

홈베이스 예약 비관적 락 적용 #240

Merged
merged 15 commits into from
Mar 28, 2024
4 changes: 3 additions & 1 deletion buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ object Dependencies {

// mockito
const val MOCKITO = "org.mockito.kotlin:mockito-kotlin:${DependencyVersions.MOCKITO_VERSION}"

// jackson
const val JACKSON_MODULE_KOTLIN = "com.fasterxml.jackson.module:jackson-module-kotlin"

Expand All @@ -41,6 +42,7 @@ object Dependencies {
const val COMMONS_IO = "commons-io:commons-io:${DependencyVersions.COMMONS_IO_VERSION}"
const val APACHE_TIKA = "org.apache.tika:tika-core:${DependencyVersions.APACHE_TIKA_VERSION}"

//gauth
// gauth
const val GAUTH = "com.github.GSM-MSG:GAuth-SDK-Java:${DependencyVersions.GAUTH_VERSION}"

}
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/DependencyVersions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ object DependencyVersions {

// test
const val MOCKITO_VERSION = "4.0.0"

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package team.msg.hiv2.aspect
package team.msg.hiv2.aspect.log

import org.aspectj.lang.JoinPoint
import org.aspectj.lang.ProceedingJoinPoint
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package team.msg.hiv2.aspect
package team.msg.hiv2.aspect.notice

import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Before
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package team.msg.hiv2.aspect
package team.msg.hiv2.aspect.reservation

import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Before
Expand All @@ -20,9 +20,7 @@ class HomeBaseReservationInfoAspect(
"&& args(floor, period, request) && within(team.msg.hiv2.domain.homebase.application.usecase.ReserveHomeBaseUseCase)")
private fun reserveHomeBaseUseCasePointcut(floor: Int, period: Int, request: ReservationHomeBaseRequest) {}

@Before(
"reserveHomeBaseUseCasePointcut(floor, period, request)"
)
@Before("reserveHomeBaseUseCasePointcut(floor, period, request)")
private fun loggingReservationInfo(floor: Int, period: Int, request: ReservationHomeBaseRequest) {
val currentUser = userService.queryCurrentUser()
log.info("reserve homebase by = {}, floor = {}, period = {}, users = {}",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package team.msg.hiv2.aspect
package team.msg.hiv2.aspect.reservation

import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ReserveHomeBaseUseCase(

val homeBase = homeBaseService.queryHomeBaseByFloorAndPeriodAndHomeBaseNumber(floor, period, homeBaseNumber)

if(reservationService.existsByHomeBase(homeBase))
if(reservationService.existsByHomeBaseId(homeBase.id))
throw AlreadyExistReservationException()

if (request.users.size > homeBase.maxCapacity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ class HomeBaseJpaEntity(

override val id: Long,

@Column(name = "floor", nullable = false)
@Column(columnDefinition = "TINYINT UNSIGNED", nullable = false)
val floor: Int,

@Column(name = "period", nullable = false)
@Column(columnDefinition = "TINYINT UNSIGNED", nullable = false)
val period: Int,

@Column(name = "home_base_number", nullable = false)
@Column(columnDefinition = "TINYINT UNSIGNED", nullable = false)
val homeBaseNumber: Int,

@Column(name = "max_capacity", nullable = false)
@Column(columnDefinition = "TINYINT UNSIGNED", nullable = false)
val maxCapacity: Int

) : BaseIdEntity(id)
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import java.util.*
class NoticeJpaEntity(
override val id: UUID,

@Column(nullable = false)
@Column(columnDefinition = "VARCHAR(30)", nullable = false)
val title: String,

@Column(nullable = false)
@Column(columnDefinition = "VARCHAR(300)", nullable = false)
val content: String,

@ManyToOne(fetch = FetchType.LAZY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ interface QueryReservationService {
fun queryAllReservationByHomeBaseIn(homeBases: List<HomeBase>): List<Reservation>
fun queryAllReservation(): List<Reservation>
fun countReservationByHomeBase(homeBase: HomeBase): Int
fun existsByHomeBase(homeBase: HomeBase): Boolean
fun existsByHomeBaseId(homeBaseId: Long): Boolean
fun queryAllReservationByUserIdInOrderByReservationId(userId: UUID): List<Reservation>
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class QueryReservationServiceImpl(
override fun countReservationByHomeBase(homeBase: HomeBase): Int =
queryReservationPort.countReservationByHomeBase(homeBase)

override fun existsByHomeBase(homeBase: HomeBase): Boolean =
queryReservationPort.existsByHomeBase(homeBase)
override fun existsByHomeBaseId(homeBaseId: Long): Boolean =
queryReservationPort.existsByHomeBaseId(homeBaseId)

override fun queryAllReservationByUserIdInOrderByReservationId(userId: UUID): List<Reservation> =
queryReservationPort.queryAllReservationByUserIdInOrderByHomeBaseId(listOf(userId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ interface QueryReservationPort {
fun queryAllReservationByHomeBaseIn(homeBases: List<HomeBase>): List<Reservation>
fun queryAllReservations(): List<Reservation>
fun countReservationByHomeBase(homeBase: HomeBase): Int
fun existsByHomeBase(homeBase: HomeBase): Boolean
fun existsByHomeBaseId(homeBaseId: Long): Boolean
fun queryAllReservationByUserIdInOrderByHomeBaseId(userId: List<UUID>): List<Reservation>
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ReservationPersistenceAdapter(
) : ReservationPort {

override fun save(reservation: Reservation): Reservation =
reservationMapper.toDomain(reservationRepository.save(reservationMapper.toEntity(reservation)))!!
reservationMapper.toDomain(reservationRepository.saveAndFlush(reservationMapper.toEntity(reservation)))!!

override fun delete(reservation: Reservation) {
reservationRepository.deleteById(reservation.id)
Expand Down Expand Up @@ -48,8 +48,8 @@ class ReservationPersistenceAdapter(
override fun countReservationByHomeBase(homeBase: HomeBase): Int =
reservationRepository.countByHomeBase(homeBaseMapper.toEntity(homeBase))

override fun existsByHomeBase(homeBase: HomeBase): Boolean =
reservationRepository.existsByHomeBase(homeBaseMapper.toEntity(homeBase))
override fun existsByHomeBaseId(homeBaseId: Long): Boolean =
reservationRepository.existsByHomeBaseId(homeBaseId)

override fun queryAllReservationByUserIdInOrderByHomeBaseId(userId: List<UUID>): List<Reservation> =
reservationRepository.findAllByUserIdsInOrderByHomeBaseId(userId).map { reservationMapper.toDomain(it)!! }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ class ReservationJpaEntity(

override val id: UUID,

@ManyToOne(fetch = FetchType.LAZY)
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "home_base_id")
val homeBase: HomeBaseJpaEntity,

@Column(nullable = false)
@Column(columnDefinition = "VARCHAR(600)", nullable = false)
val reason: String,

@Column(name = "check_status", nullable = false)
var checkStatus: Boolean = false,

@JoinColumn(name = "reservation_id")
@OnDelete(action = OnDeleteAction.CASCADE)
@Column(name = "user_id", columnDefinition = "BINARY(16)")
@Column(columnDefinition = "BINARY(16)", name = "user_id")
@ElementCollection
@CollectionTable(name = "users", joinColumns = [JoinColumn(name = "reservation_id")])
val userIds: MutableList<UUID>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package team.msg.hiv2.domain.reservation.persistence.repository

import org.springframework.data.jpa.repository.EntityGraph
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.jpa.repository.*
import team.msg.hiv2.domain.homebase.persistence.entity.HomeBaseJpaEntity
import team.msg.hiv2.domain.reservation.persistence.entity.ReservationJpaEntity
import java.util.*
import javax.persistence.LockModeType.*

interface ReservationRepository : JpaRepository<ReservationJpaEntity, UUID> {

Expand All @@ -18,7 +16,8 @@ interface ReservationRepository : JpaRepository<ReservationJpaEntity, UUID> {
@Query("DELETE FROM ReservationJpaEntity r WHERE r IN :reservations")
fun deleteAllInBatch(reservations: List<ReservationJpaEntity>)
fun countByHomeBase(homeBase: HomeBaseJpaEntity): Int
fun existsByHomeBase(homeBase: HomeBaseJpaEntity): Boolean
@Lock(PESSIMISTIC_WRITE)
fun existsByHomeBaseId(homeBaseId: Long): Boolean
@EntityGraph(attributePaths = ["homeBase"])
fun findAllByUserIdsInOrderByHomeBaseId(userIds: List<UUID>): List<ReservationJpaEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@ class UserJpaEntity(
@Column(columnDefinition = "VARCHAR(10)", nullable = false)
val name: String,

@Column(nullable = true)
@Column(columnDefinition = "TINYINT UNSIGNED", nullable = true)
val grade: Int?,

@Column(nullable = true)
@Column(columnDefinition = "TINYINT UNSIGNED", nullable = true)
val classNum: Int?,

@Column(nullable = true)
@Column(columnDefinition = "TINYINT UNSIGNED", nullable = true)
val number: Int?,

@Column(columnDefinition = "TEXT", nullable = false)
var profileImageUrl: String = "",

@Enumerated(EnumType.STRING)
@Column(name = "role", nullable = false)
@Column(columnDefinition = "VARCHAR(30)", name = "role", nullable = false)
var role: UserRole,

@Enumerated(EnumType.STRING)
@Column(name = "use_status", nullable = false)
@Column(columnDefinition = "VARCHAR(30)", name = "use_status", nullable = false)
val useStatus: UseStatus

) : BaseUuidEntity(id)
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package team.msg.hiv2.global.annotation

@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.CLASS)
import kotlin.annotation.AnnotationRetention.*
import kotlin.annotation.AnnotationTarget.*

@Retention(RUNTIME)
@Target(CLASS)
annotation class Aggregate
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package team.msg.hiv2.global.annotation.service

@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.CLASS)
import kotlin.annotation.AnnotationRetention.*
import kotlin.annotation.AnnotationTarget.*

@Retention(RUNTIME)
@Target(CLASS)
annotation class DomainService
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package team.msg.hiv2.global.annotation.usecase

import org.springframework.transaction.annotation.Transactional
import kotlin.annotation.AnnotationRetention.*
import kotlin.annotation.AnnotationTarget.*

@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.CLASS)
@Retention(RUNTIME)
@Target(CLASS)
@Transactional(readOnly = true)
annotation class ReadOnlyUseCase
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package team.msg.hiv2.global.annotation.usecase

import org.springframework.transaction.annotation.Transactional
import kotlin.annotation.AnnotationRetention.*
import kotlin.annotation.AnnotationTarget.*

@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.CLASS)
@Retention(RUNTIME)
@Target(CLASS)
@Transactional
annotation class UseCase
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ internal class ReserveHomeBaseUseCaseTest {
private val homeBaseNumber = 1
private val maxCapacity = 4

private val homeBaseId = 1L
private val userId1 = UUID.randomUUID()
private val userId2 = UUID.randomUUID()

private val homeBaseStub by lazy {
HomeBase(
id = 1,
id = homeBaseId,
period = period,
floor = floor,
homeBaseNumber = homeBaseNumber,
Expand Down Expand Up @@ -113,7 +114,7 @@ internal class ReserveHomeBaseUseCaseTest {
given(homeBaseService.queryHomeBaseByFloorAndPeriodAndHomeBaseNumber(floor, period, homeBaseNumber))
.willReturn(homeBaseStub)

given(reservationService.existsByHomeBase(homeBaseStub))
given(reservationService.existsByHomeBaseId(homeBaseId))
.willReturn(false)

given(userService.queryAllUserById(requestStub.users))
Expand Down
Loading