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

Added review&reservation consent field to profile #105

Merged
merged 5 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class SecurityConfig(
authorize("/api/oauth2/**", permitAll)
authorize(HttpMethod.GET, "/api/v1/rooms/main/**", permitAll)
authorize(HttpMethod.GET, "/api/v1/reservations/availability/**", permitAll)
authorize(HttpMethod.GET, "/api/v1/reservations/user/**", permitAll)
authorize(HttpMethod.GET, "/api/v1/reviews/**", permitAll)
authorize("/error", permitAll)
authorize("/redirect", permitAll)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,14 @@ class ProfileController(

data class UpdateProfileRequest(
val nickname: String,
val bio: String
val bio: String,
val showMyReviews: Boolean,
val showMyReservations: Boolean
)

data class CreateProfileRequest(
val nickname: String,
val bio: String
val bio: String,
val showMyReviews: Boolean,
val showMyReservations: Boolean
)
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,11 @@ class ProfileEntity(
var bio: String,

@Column(nullable = false)
var isSuperHost: Boolean = false
var isSuperHost: Boolean = false,

@Column(nullable = false)
var showMyReviews: Boolean = false,

@Column(nullable = false)
var showMyReservations: Boolean = false
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ProfileServiceImpl(
private val roomRepository: RoomRepository
) : ProfileService {

@Transactional
override fun getCurrentUserProfile(
user: UserEntity
): Profile {
Expand All @@ -30,10 +31,12 @@ class ProfileServiceImpl(
user: UserEntity,
request: UpdateProfileRequest
): Profile {
val profile = profileRepository.findByUser(user) ?: throw ProfileNotFoundException()
val profile = profileRepository.findByUser(user) ?: ProfileEntity(user = user, nickname = "", bio = "")

profile.nickname = request.nickname
profile.bio = request.bio
profile.showMyReviews = request.showMyReviews
profile.showMyReservations = request.showMyReservations
updateSuperHostStatus(profile)
profileRepository.save(profile)

Expand All @@ -50,7 +53,9 @@ class ProfileServiceImpl(
val profile = ProfileEntity(
user = user,
nickname = request.nickname,
bio = request.bio
bio = request.bio,
showMyReviews = request.showMyReviews,
showMyReservations = request.showMyReservations
)
updateSuperHostStatus(profile)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import com.example.toyTeam6Airbnb.user.controller.PrincipalDetails
import com.example.toyTeam6Airbnb.user.controller.User
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
Expand Down Expand Up @@ -91,9 +93,18 @@ class ReservationController(
@Operation(summary = "์œ ์ €๋ณ„ ์˜ˆ์•ฝ ์กฐํšŒ", description = "ํŠน์ • ์œ ์ €์˜ ๋ชจ๋“  ์˜ˆ์•ฝ ์ •๋ณด๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค")
fun getReservationsByUser(
@PathVariable userId: Long,
@RequestParam pageable: Pageable
): ResponseEntity<List<ReservationDTO>> {
val reservations = reservationService.getReservationsByUser(userId, pageable)
pageable: Pageable
): ResponseEntity<Page<ReservationDTO>> {
val viewerId =
try {
val principalDetails = SecurityContextHolder.getContext().authentication.principal as PrincipalDetails
principalDetails.getUser().id
// logic for when the user is logged in
} catch (e: ClassCastException) {
// logic for when the user is not logged in
null
}
val reservations = reservationService.getReservationsByUser(viewerId, userId, pageable)
return ResponseEntity.ok(reservations)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.example.toyTeam6Airbnb.reservation.persistence
import com.example.toyTeam6Airbnb.room.persistence.RoomEntity
import com.example.toyTeam6Airbnb.user.persistence.UserEntity
import jakarta.persistence.LockModeType
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Lock
import org.springframework.data.jpa.repository.Query
Expand All @@ -15,5 +17,5 @@ interface ReservationRepository : JpaRepository<ReservationEntity, Long> {

fun findAllByRoom(room: RoomEntity): List<ReservationEntity>

fun findAllByUser(user: UserEntity): List<ReservationEntity>
fun findAllByUser(user: UserEntity, pageable: Pageable): Page<ReservationEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.example.toyTeam6Airbnb.reservation.controller.Reservation
import com.example.toyTeam6Airbnb.reservation.controller.ReservationDTO
import com.example.toyTeam6Airbnb.reservation.controller.RoomAvailabilityResponse
import com.example.toyTeam6Airbnb.user.controller.User
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import java.time.LocalDate
import java.time.YearMonth
Expand Down Expand Up @@ -36,9 +37,10 @@ interface ReservationService {
): Reservation

fun getReservationsByUser(
viewerId: Long?,
userId: Long,
pageable: Pageable
): List<ReservationDTO>
): Page<ReservationDTO>

// fun getReservationsByRoom(
// roomId: Long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.example.toyTeam6Airbnb.user.controller.User
import com.example.toyTeam6Airbnb.user.persistence.UserRepository
import jakarta.persistence.EntityManager
import jakarta.persistence.LockModeType
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
Expand Down Expand Up @@ -132,10 +133,11 @@ class ReservationServiceImpl(
}

@Transactional
override fun getReservationsByUser(userId: Long, pageable: Pageable): List<ReservationDTO> {
override fun getReservationsByUser(viewerId: Long?, userId: Long, pageable: Pageable): Page<ReservationDTO> {
val userEntity = userRepository.findByIdOrNull(userId) ?: throw UserNotFoundException()
if (viewerId != userId && userEntity.profile?.showMyReservations != true) throw ReservationPermissionDenied()

return reservationRepository.findAllByUser(userEntity).map(ReservationDTO::fromEntity)
return reservationRepository.findAllByUser(userEntity, pageable).map(ReservationDTO::fromEntity)
}

// @Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import org.springframework.data.domain.Pageable
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.web.bind.annotation.DeleteMapping
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
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

@RestController
Expand Down Expand Up @@ -47,7 +47,7 @@ class ReviewController(
@Operation(summary = "ํŠน์ • ๋ฐฉ์˜ ๋ฆฌ๋ทฐ ์กฐํšŒ", description = "ํŠน์ • ๋ฐฉ์˜ ๋ชจ๋“  ๋ฆฌ๋ทฐ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค")
fun getReviewsByRoom(
@PathVariable roomId: Long,
@RequestParam pageable: Pageable
pageable: Pageable
): ResponseEntity<Page<ReviewDTO>> {
val reviews = reviewService.getReviewsByRoom(roomId, pageable)
return ResponseEntity.ok(reviews)
Expand All @@ -66,9 +66,18 @@ class ReviewController(
@Operation(summary = "ํŠน์ • ์œ ์ €์˜ ๋ฆฌ๋ทฐ ์กฐํšŒ", description = "ํŠน์ • ์œ ์ €์˜ ๋ชจ๋“  ๋ฆฌ๋ทฐ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค")
fun getReviewsByUser(
@PathVariable userId: Long,
@RequestParam pageable: Pageable
pageable: Pageable
): ResponseEntity<Page<ReviewDTO>> {
val reviews = reviewService.getReviewsByUser(userId, pageable)
val viewerId =
try {
val principalDetails = SecurityContextHolder.getContext().authentication.principal as PrincipalDetails
principalDetails.getUser().id
// logic for when the user is logged in
} catch (e: ClassCastException) {
// logic for when the user is not logged in
null
}
val reviews = reviewService.getReviewsByUser(viewerId, userId, pageable)
return ResponseEntity.ok(reviews)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface ReviewService {
): ReviewDTO

fun getReviewsByUser(
viewerId: Long?,
userId: Long,
pageable: Pageable
): Page<ReviewDTO>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ class ReviewServiceImpl(
}

@Transactional
override fun getReviewsByUser(userId: Long, pageable: Pageable): Page<ReviewDTO> {
userRepository.findByIdOrNull(userId) ?: throw UserNotFoundException()
override fun getReviewsByUser(viewerId: Long?, userId: Long, pageable: Pageable): Page<ReviewDTO> {
val userEntity = userRepository.findByIdOrNull(userId) ?: throw UserNotFoundException()
if (viewerId != userId && userEntity.profile?.showMyReviews != true) throw ReviewPermissionDeniedException()

val reviewEntities = reviewRepository.findAllByUserId(userId, validatePageable(pageable))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class UserController(
fun register(
@RequestBody request: RegisterRequest
): ResponseEntity<Unit> {
userService.register(username = request.username, password = request.password)
userService.register(request)
return ResponseEntity.ok().build()
}

Expand All @@ -47,5 +47,9 @@ class UserController(

data class RegisterRequest(
val username: String,
val password: String
val password: String,
val nickname: String,
val bio: String,
val showMyReviews: Boolean,
val showMyReservations: Boolean
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.example.toyTeam6Airbnb.user.service

import com.example.toyTeam6Airbnb.user.controller.RegisterRequest
import com.example.toyTeam6Airbnb.user.controller.User

interface UserService {
fun register(
username: String,
password: String
request: RegisterRequest
): User?
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.example.toyTeam6Airbnb.user.service

import com.example.toyTeam6Airbnb.profile.persistence.ProfileEntity
import com.example.toyTeam6Airbnb.profile.persistence.ProfileRepository
import com.example.toyTeam6Airbnb.user.SignUpBadUsernameException
import com.example.toyTeam6Airbnb.user.SignUpUsernameConflictException
import com.example.toyTeam6Airbnb.user.controller.RegisterRequest
import com.example.toyTeam6Airbnb.user.controller.User
import com.example.toyTeam6Airbnb.user.persistence.AuthProvider
import com.example.toyTeam6Airbnb.user.persistence.UserEntity
Expand All @@ -13,22 +16,29 @@ import org.springframework.stereotype.Service
@Service
class UserServiceImpl(
private val userRepository: UserRepository,
private val profileRepository: ProfileRepository,
private val passwordEncoder: PasswordEncoder
) : UserService {
@Transactional
override fun register(
username: String,
password: String
request: RegisterRequest
): User? {
if (username.startsWith("OAUTH")) throw SignUpBadUsernameException()
if (userRepository.existsByUsername(username)) throw SignUpUsernameConflictException()
if (request.username.startsWith("OAUTH")) throw SignUpBadUsernameException()
if (userRepository.existsByUsername(request.username)) throw SignUpUsernameConflictException()
val userEntity = UserEntity(
username = username,
password = passwordEncoder.encode(password),
username = request.username,
password = passwordEncoder.encode(request.password),
provider = AuthProvider.LOCAL
).let {
userRepository.save(it)
}
ProfileEntity(
user = userEntity,
nickname = request.nickname,
bio = request.bio,
showMyReviews = request.showMyReviews,
showMyReservations = request.showMyReservations
).let { profileRepository.save(it) }
return User.fromEntity(userEntity)
}
}
Loading
Loading