Skip to content

Commit

Permalink
Merge pull request #106 from wafflestudio/dev
Browse files Browse the repository at this point in the history
main merge
  • Loading branch information
JunBye authored Jan 15, 2025
2 parents 0c1809e + f088a25 commit 0f3f8e8
Show file tree
Hide file tree
Showing 19 changed files with 319 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.toyTeam6Airbnb.config

import com.example.toyTeam6Airbnb.user.JwtTokenProvider
import com.example.toyTeam6Airbnb.user.controller.PrincipalDetails
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import org.springframework.beans.factory.annotation.Value
Expand All @@ -22,6 +23,6 @@ class CustomAuthenticationSuccessHandler(
) {
val token = jwtTokenProvider.generateToken(authentication.name)

response.sendRedirect("/redirect?token=$token")
response.sendRedirect("/redirect?token=$token&userid=${(authentication.principal as PrincipalDetails).getId()}")
}
}
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 @@ -40,12 +40,21 @@ class UserController(
// just return the token parameter in body
@Operation(summary = "Redirect", description = "Redirect to the token", hidden = true)
@GetMapping("/redirect")
fun redirect(@RequestParam token: String): ResponseEntity<String> {
return ResponseEntity.ok(token)
fun redirect(@RequestParam token: String, @RequestParam userid: Long): ResponseEntity<RedirectResponse> {
return ResponseEntity.ok(RedirectResponse(token, userid))
}
}

data class RegisterRequest(
val username: String,
val password: String
val password: String,
val nickname: String,
val bio: String,
val showMyReviews: Boolean,
val showMyReservations: Boolean
)

data class RedirectResponse(
val token: String,
val userId: Long
)
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)
}
}
3 changes: 3 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ spring:
client-id: ASDF
client-secret: ASDF
redirect-uri: '{baseUrl}/api/oauth2/callback/{registrationId}'
scope:
- email
- profile
naver:
client-id: ASDF
client-secret: ASDF
Expand Down
Loading

0 comments on commit 0f3f8e8

Please sign in to comment.