Skip to content

Commit

Permalink
Merge pull request #109 from wafflestudio/dev
Browse files Browse the repository at this point in the history
dev 배포
  • Loading branch information
gs18113 authored Jan 16, 2025
2 parents 0f3f8e8 + c268a53 commit 8ec8a34
Show file tree
Hide file tree
Showing 23 changed files with 291 additions and 200 deletions.
24 changes: 24 additions & 0 deletions src/main/kotlin/com/example/toyTeam6Airbnb/ValidatePageable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,33 @@ package com.example.toyTeam6Airbnb

import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Pageable
import org.springframework.http.HttpStatus
import org.springframework.http.HttpStatusCode

fun validatePageable(pageable: Pageable): Pageable {
return PageRequest.of(pageable.pageNumber, pageable.pageSize)
}

fun validateSortedPageable(pageable: Pageable): Pageable {
val allowedSortProperties = listOf("createdAt", "rating")
pageable.sort.forEach { order ->
if (order.property !in allowedSortProperties) {
throw WrongSortingException()
}
}
return PageRequest.of(pageable.pageNumber, pageable.pageSize, pageable.sort)
}

sealed class PageableException(
errorCode: Int,
httpStatusCode: HttpStatusCode,
msg: String,
cause: Throwable? = null
) : DomainException(errorCode, httpStatusCode, msg, cause)

class WrongSortingException : PageableException(
errorCode = 6001,
httpStatusCode = HttpStatus.BAD_REQUEST,
msg = "Wrong form of sorting"
)
// 이후에 rating, review, price 등의 정렬 기준이 추가될 경우 함수를 추가하여 처리할 수 있습니다.
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ package com.example.toyTeam6Airbnb.profile.controller
import com.example.toyTeam6Airbnb.profile.persistence.ProfileEntity

data class Profile(
val id: Long,
val userId: Long,
val nickname: String,
val bio: String,
val isSuperHost: Boolean
val isSuperHost: Boolean,
val showMyReviews: Boolean,
val showMyReservations: Boolean
) {
companion object {
fun fromEntity(profileEntity: ProfileEntity): Profile {
return Profile(
id = profileEntity.id!!,
userId = profileEntity.user.id!!,
nickname = profileEntity.nickname,
bio = profileEntity.bio,
isSuperHost = profileEntity.isSuperHost
isSuperHost = profileEntity.isSuperHost,
showMyReviews = profileEntity.showMyReviews,
showMyReservations = profileEntity.showMyReservations
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
Expand All @@ -30,24 +31,33 @@ class ProfileController(
return ResponseEntity.ok(profile)
}

@GetMapping("/{userId}")
@Operation(summary = "특정 유저 프로필 가져오기", description = "특정 user의 프로필을 가져옵니다.")
fun getProfileByUserId(
@PathVariable userId: Long
): ResponseEntity<Profile> {
val profile = profileService.getProfileByUserId(userId)
return ResponseEntity.ok(profile)
}

@PutMapping
@Operation(summary = "유저 프로필 업데이트하기", description = "현재 로그인 되어 있는 user의 프로필을 업데이트합니다.")
fun updateCurrentUserProfile(
@AuthenticationPrincipal principalDetails: PrincipalDetails,
@RequestBody request: UpdateProfileRequest
): ResponseEntity<Profile> {
val updatedProfile = profileService.updateCurrentUserProfile(principalDetails.getUser(), request)
return ResponseEntity.ok(updatedProfile)
): ResponseEntity<Unit> {
profileService.updateCurrentUserProfile(principalDetails.getUser(), request)
return ResponseEntity.ok().build()
}

@PostMapping
@Operation(summary = "유저 프로필 추가", description = "현재 로그인 되어 있는 user에게 프로필을 추가합니다. (소셜 로그인 전용)")
fun addProfileToCurrentUser(
@AuthenticationPrincipal principalDetails: PrincipalDetails,
@RequestBody request: CreateProfileRequest
): ResponseEntity<Profile> {
val profile = profileService.addProfileToCurrentUser(principalDetails.getUser(), request)
return ResponseEntity.status(HttpStatus.CREATED).body(profile)
): ResponseEntity<Unit> {
profileService.addProfileToCurrentUser(principalDetails.getUser(), request)
return ResponseEntity.status(HttpStatus.CREATED).build()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ import org.springframework.data.jpa.repository.JpaRepository
interface ProfileRepository : JpaRepository<ProfileEntity, Long> {
fun findByUser(user: UserEntity): ProfileEntity?

fun findByUserId(userId: Long): ProfileEntity?

fun existsByUser(user: UserEntity): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ interface ProfileService {
user: UserEntity
): Profile

fun getProfileByUserId(
userId: Long
): Profile

fun updateCurrentUserProfile(
user: UserEntity,
request: UpdateProfileRequest
): Profile
)

fun addProfileToCurrentUser(
user: UserEntity,
request: CreateProfileRequest
): Profile
)

fun updateSuperHostStatus(
profile: ProfileEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@ class ProfileServiceImpl(
return Profile.fromEntity(profile)
}

override fun getProfileByUserId(
userId: Long
): Profile {
val profile = profileRepository.findByUserId(userId) ?: throw ProfileNotFoundException()
return Profile.fromEntity(profile)
}

@Transactional
override fun updateCurrentUserProfile(
user: UserEntity,
request: UpdateProfileRequest
): Profile {
) {
val profile = profileRepository.findByUser(user) ?: ProfileEntity(user = user, nickname = "", bio = "")

profile.nickname = request.nickname
Expand All @@ -39,15 +46,13 @@ class ProfileServiceImpl(
profile.showMyReservations = request.showMyReservations
updateSuperHostStatus(profile)
profileRepository.save(profile)

return Profile.fromEntity(profile)
}

@Transactional
override fun addProfileToCurrentUser(
user: UserEntity,
request: CreateProfileRequest
): Profile {
) {
if (profileRepository.existsByUser(user)) throw ProfileAlreadyExistException()

val profile = ProfileEntity(
Expand All @@ -58,8 +63,6 @@ class ProfileServiceImpl(
showMyReservations = request.showMyReservations
)
updateSuperHostStatus(profile)

return Profile.fromEntity(profileRepository.save(profile))
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -1,58 +1,59 @@
package com.example.toyTeam6Airbnb.reservation.controller

import com.example.toyTeam6Airbnb.reservation.persistence.ReservationEntity
import java.time.Instant
import java.time.LocalDate

data class Reservation(
val id: Long,
val userId: Long,
val roomId: Long,
val reviewId: Long?,
val startDate: LocalDate,
val endDate: LocalDate,
val totalPrice: Double,
val numberOfGuests: Int,
val createdAt: Instant,
val updatedAt: Instant
val reservationId: Long
) {
companion object {
fun fromEntity(entity: ReservationEntity): Reservation {
return Reservation(
id = entity.id!!,
userId = entity.user.id!!,
roomId = entity.room.id!!,
reviewId = entity.review?.id,
startDate = entity.startDate,
endDate = entity.endDate,
totalPrice = entity.totalPrice,
createdAt = entity.createdAt,
updatedAt = entity.updatedAt,
numberOfGuests = entity.numberOfGuests
reservationId = entity.id!!
)
}
}
}

data class ReservationDTO(
val id: Long,
val userId: Long,
data class ReservationDetails(
val reservationId: Long,
val roomId: Long,
val startDate: LocalDate,
val endDate: LocalDate,
val place: String,
val numberOfGuests: Int
// val imageUrl: String,
) {
companion object {
fun fromEntity(entity: ReservationEntity): ReservationDTO {
return ReservationDTO(
id = entity.id!!,
userId = entity.user.id!!,
fun fromEntity(entity: ReservationEntity): ReservationDetails {
return ReservationDetails(
reservationId = entity.id!!,
roomId = entity.room.id!!,
startDate = entity.startDate,
endDate = entity.endDate,
place = entity.room.address.sido,
numberOfGuests = entity.numberOfGuests
// imageUrl = entity.room.imageUrl
)
}
}
}

data class ReservationDTO(
val reservationId: Long,
val place: String,
val startDate: LocalDate,
val endDate: LocalDate
// val imageUrl: String,
) {
companion object {
fun fromEntity(entity: ReservationEntity): ReservationDTO {
return ReservationDTO(
reservationId = entity.id!!,
place = entity.room.address.sido,
startDate = entity.startDate,
endDate = entity.endDate
// imageUrl = entity.room.imageUrl
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ class ReservationController(
@GetMapping("/{reservationId}")
@Operation(summary = "예약 상세 조회", description = "예약 상세 정보를 조회합니다")
fun getReservation(
@AuthenticationPrincipal principalDetails: PrincipalDetails,
@PathVariable reservationId: Long
): ResponseEntity<Reservation> {
val reservation = reservationService.getReservation(reservationId)
): ResponseEntity<ReservationDetails> {
val reservation = reservationService.getReservation(principalDetails.getUser().id!!, reservationId)

return ResponseEntity.ok(reservation)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.example.toyTeam6Airbnb.reservation.service

import com.example.toyTeam6Airbnb.reservation.controller.Reservation
import com.example.toyTeam6Airbnb.reservation.controller.ReservationDTO
import com.example.toyTeam6Airbnb.reservation.controller.ReservationDetails
import com.example.toyTeam6Airbnb.reservation.controller.RoomAvailabilityResponse
import com.example.toyTeam6Airbnb.user.controller.User
import org.springframework.data.domain.Page
Expand Down Expand Up @@ -33,8 +34,9 @@ interface ReservationService {
): Reservation

fun getReservation(
userId: Long,
reservationId: Long
): Reservation
): ReservationDetails

fun getReservationsByUser(
viewerId: Long?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.example.toyTeam6Airbnb.reservation.ReservationUnavailable
import com.example.toyTeam6Airbnb.reservation.ZeroGuests
import com.example.toyTeam6Airbnb.reservation.controller.Reservation
import com.example.toyTeam6Airbnb.reservation.controller.ReservationDTO
import com.example.toyTeam6Airbnb.reservation.controller.ReservationDetails
import com.example.toyTeam6Airbnb.reservation.controller.RoomAvailabilityResponse
import com.example.toyTeam6Airbnb.reservation.persistence.ReservationEntity
import com.example.toyTeam6Airbnb.reservation.persistence.ReservationRepository
Expand Down Expand Up @@ -126,10 +127,14 @@ class ReservationServiceImpl(
}

@Transactional
override fun getReservation(reservationId: Long): Reservation {
override fun getReservation(
userId: Long,
reservationId: Long
): ReservationDetails {
val reservationEntity = reservationRepository.findByIdOrNull(reservationId) ?: throw ReservationNotFound()
if (reservationEntity.user.id != userId) throw ReservationPermissionDenied()

return Reservation.fromEntity(reservationEntity)
return ReservationDetails.fromEntity(reservationEntity)
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,45 @@ import com.example.toyTeam6Airbnb.review.persistence.ReviewEntity
import java.time.Instant
import java.time.LocalDate

data class Review(
val id: Long,
data class ReviewByRoomDTO(
val userId: Long,
val reservationId: Long,
val roomId: Long,
val nickname: String,
val profileImage: String,
val content: String,
val rating: Int,
val createdAt: Instant,
val updatedAt: Instant
val startDate: LocalDate,
val endDate: LocalDate
) {
companion object {
fun fromEntity(entity: ReviewEntity): Review {
return Review(
id = entity.id!!,
fun fromEntity(entity: ReviewEntity): ReviewByRoomDTO {
return ReviewByRoomDTO(
userId = entity.user.id!!,
reservationId = entity.reservation.id!!,
roomId = entity.room.id!!,
nickname = entity.user.profile?.nickname ?: entity.user.username,
profileImage = "",
content = entity.content,
rating = entity.rating,
createdAt = entity.createdAt,
updatedAt = entity.updatedAt
startDate = entity.reservation.startDate,
endDate = entity.reservation.endDate
)
}
}
}

data class ReviewByUserDTO(
val content: String,
val rating: Int,
val place: String,
val startDate: LocalDate,
val endDate: LocalDate
) {
companion object {
fun fromEntity(entity: ReviewEntity): ReviewByUserDTO {
return ReviewByUserDTO(
content = entity.content,
rating = entity.rating,
place = entity.room.address.sido,
startDate = entity.reservation.startDate,
endDate = entity.reservation.endDate
)
}
}
Expand Down
Loading

0 comments on commit 8ec8a34

Please sign in to comment.