diff --git a/src/main/kotlin/com/example/toyTeam6Airbnb/room/controller/Room.kt b/src/main/kotlin/com/example/toyTeam6Airbnb/room/controller/Room.kt index ee1420c..63941a1 100644 --- a/src/main/kotlin/com/example/toyTeam6Airbnb/room/controller/Room.kt +++ b/src/main/kotlin/com/example/toyTeam6Airbnb/room/controller/Room.kt @@ -1,6 +1,7 @@ package com.example.toyTeam6Airbnb.room.controller import com.example.toyTeam6Airbnb.room.persistence.Address +import com.example.toyTeam6Airbnb.room.persistence.Price import com.example.toyTeam6Airbnb.room.persistence.RoomDetails import com.example.toyTeam6Airbnb.room.persistence.RoomType import java.time.Instant @@ -13,9 +14,10 @@ data class Room( val type: RoomType, val address: Address, val roomDetails: RoomDetails, - val price: Double, + val price: Price, val maxOccupancy: Int, val rating: Double, + val reviewCount: Int, val isSuperhost: Boolean, val createdAt: Instant, val updatedAt: Instant @@ -36,6 +38,7 @@ data class Room( price = entity.price, maxOccupancy = entity.maxOccupancy, rating = averageRating, + reviewCount = entity.reviews.size, isSuperhost = entity.host.isSuperhost(), createdAt = entity.createdAt, updatedAt = entity.updatedAt @@ -69,6 +72,7 @@ data class Room( price = this.price, maxOccupancy = this.maxOccupancy, rating = this.rating, + reviewCount = this.reviewCount, isSuperhost = this.isSuperhost, createdAt = this.createdAt, updatedAt = this.updatedAt @@ -83,7 +87,7 @@ data class RoomDTO( val description: String, val type: RoomType, val address: Address, - val price: Double, + val price: Price, val maxOccupancy: Int, val rating: Double ) @@ -96,9 +100,10 @@ data class RoomDetailsDTO( val type: RoomType, val address: Address, val roomDetails: RoomDetails, - val price: Double, + val price: Price, val maxOccupancy: Int, val rating: Double, + val reviewCount: Int, val isSuperhost: Boolean, val createdAt: Instant, val updatedAt: Instant diff --git a/src/main/kotlin/com/example/toyTeam6Airbnb/room/controller/RoomController.kt b/src/main/kotlin/com/example/toyTeam6Airbnb/room/controller/RoomController.kt index 657df73..2bd5b88 100644 --- a/src/main/kotlin/com/example/toyTeam6Airbnb/room/controller/RoomController.kt +++ b/src/main/kotlin/com/example/toyTeam6Airbnb/room/controller/RoomController.kt @@ -1,6 +1,7 @@ package com.example.toyTeam6Airbnb.room.controller import com.example.toyTeam6Airbnb.room.persistence.Address +import com.example.toyTeam6Airbnb.room.persistence.Price import com.example.toyTeam6Airbnb.room.persistence.RoomDetails import com.example.toyTeam6Airbnb.room.persistence.RoomType import com.example.toyTeam6Airbnb.room.service.RoomService @@ -137,7 +138,7 @@ data class CreateRoomRequest( val type: RoomType, val address: Address, val roomDetails: RoomDetails, - val price: Double, + val price: Price, val maxOccupancy: Int ) @@ -147,6 +148,6 @@ data class UpdateRoomRequest( val type: RoomType, val address: Address, val roomDetails: RoomDetails, - val price: Double, + val price: Price, val maxOccupancy: Int ) diff --git a/src/main/kotlin/com/example/toyTeam6Airbnb/room/service/RoomService.kt b/src/main/kotlin/com/example/toyTeam6Airbnb/room/service/RoomService.kt index 0cadf31..eaa189a 100644 --- a/src/main/kotlin/com/example/toyTeam6Airbnb/room/service/RoomService.kt +++ b/src/main/kotlin/com/example/toyTeam6Airbnb/room/service/RoomService.kt @@ -3,6 +3,7 @@ package com.example.toyTeam6Airbnb.room.service import com.example.toyTeam6Airbnb.room.controller.AddressSearchDTO import com.example.toyTeam6Airbnb.room.controller.Room import com.example.toyTeam6Airbnb.room.persistence.Address +import com.example.toyTeam6Airbnb.room.persistence.Price import com.example.toyTeam6Airbnb.room.persistence.RoomDetails import com.example.toyTeam6Airbnb.room.persistence.RoomType import org.springframework.data.domain.Page @@ -17,7 +18,7 @@ interface RoomService { type: RoomType, address: Address, roomDetails: RoomDetails, - price: Double, + price: Price, maxOccupancy: Int ): Room @@ -33,7 +34,7 @@ interface RoomService { type: RoomType, address: Address, roomDetails: RoomDetails, - price: Double, + price: Price, maxOccupancy: Int ): Room diff --git a/src/main/kotlin/com/example/toyTeam6Airbnb/room/service/RoomServiceImpl.kt b/src/main/kotlin/com/example/toyTeam6Airbnb/room/service/RoomServiceImpl.kt index ecdb0ec..2a9e9de 100644 --- a/src/main/kotlin/com/example/toyTeam6Airbnb/room/service/RoomServiceImpl.kt +++ b/src/main/kotlin/com/example/toyTeam6Airbnb/room/service/RoomServiceImpl.kt @@ -16,6 +16,7 @@ import com.example.toyTeam6Airbnb.room.persistence.RoomDetails import com.example.toyTeam6Airbnb.room.persistence.RoomEntity import com.example.toyTeam6Airbnb.room.persistence.RoomRepository import com.example.toyTeam6Airbnb.room.persistence.RoomType +import com.example.toyTeam6Airbnb.room.persistence.Price import com.example.toyTeam6Airbnb.user.AuthenticateException import com.example.toyTeam6Airbnb.user.persistence.UserRepository import org.springframework.dao.DataIntegrityViolationException @@ -40,16 +41,14 @@ class RoomServiceImpl( type: RoomType, address: Address, roomDetails: RoomDetails, - price: Double, + price: Price, maxOccupancy: Int ): Room { val hostEntity = userRepository.findByIdOrNull(hostId) ?: throw AuthenticateException() validateRoomInfo(name, description, type, address, price, maxOccupancy) - if (roomRepository.existsByNameAndTypeAndAddress(name, type, address)) { - throw DuplicateRoomException() - } + if (roomRepository.existsByAddress(address)) throw DuplicateRoomException() try { val roomEntity = RoomEntity( @@ -93,20 +92,18 @@ class RoomServiceImpl( type: RoomType, address: Address, roomDetails: RoomDetails, - price: Double, + price: Price, maxOccupancy: Int ): Room { val hostEntity = userRepository.findByIdOrNull(hostId) ?: throw AuthenticateException() val roomEntity = roomRepository.findByIdOrNullForUpdate(roomId) ?: throw RoomNotFoundException() - if (roomEntity.host.id != hostEntity.id) { - throw RoomPermissionDeniedException() - } + if (roomEntity.host.id != hostEntity.id) throw RoomPermissionDeniedException() validateRoomInfo(name, description, type, address, price, maxOccupancy) - if (roomRepository.existsByNameAndTypeAndAddress(name, type, address) && - (roomEntity.name != name || roomEntity.type != type || roomEntity.address != address) + if (roomRepository.existsByAddress(address) && + (roomEntity.address != address) ) { throw DuplicateRoomException() } @@ -171,12 +168,12 @@ class RoomServiceImpl( description: String, type: RoomType, address: Address, - price: Double, + price: Price, maxOccupancy: Int ) { if (name.isBlank()) throw InvalidNameException() if (description.isBlank()) throw InvalidDescriptionException() - if (price <= 0) throw InvalidPriceException() + validatePrice(price) if (maxOccupancy <= 0) throw InvalidMaxOccupancyException() try { @@ -188,6 +185,16 @@ class RoomServiceImpl( validateAddress(address) } + private fun validatePrice(price: Price){ + if (price.perNight <= 0 || + price.cleaningFee < 0 || + price.charge < 0 || + price.updateTotal() <= 0 + ){ + throw InvalidPriceException() + } + } + private fun validateAddress(address: Address) { if (address.sido.isBlank() || address.sigungu.isBlank() ||