Add guest create endpoint
All checks were successful
build-and-deploy / build-deploy (push) Successful in 34s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 34s
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.android.trisolarisserver.controller
|
||||
|
||||
import com.android.trisolarisserver.component.PropertyAccess
|
||||
import com.android.trisolarisserver.controller.dto.GuestCreateRequest
|
||||
import com.android.trisolarisserver.controller.dto.GuestResponse
|
||||
import com.android.trisolarisserver.controller.dto.GuestVehicleRequest
|
||||
import com.android.trisolarisserver.models.booking.Guest
|
||||
@@ -15,6 +16,7 @@ import org.springframework.http.HttpStatus
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal
|
||||
import org.springframework.web.bind.annotation.*
|
||||
import org.springframework.web.server.ResponseStatusException
|
||||
import java.time.OffsetDateTime
|
||||
import java.util.UUID
|
||||
|
||||
@RestController
|
||||
@@ -28,6 +30,38 @@ class Guests(
|
||||
private val guestRatingRepo: GuestRatingRepo
|
||||
) {
|
||||
|
||||
@PostMapping
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
fun createGuest(
|
||||
@PathVariable propertyId: UUID,
|
||||
@AuthenticationPrincipal principal: MyPrincipal?,
|
||||
@RequestBody request: GuestCreateRequest
|
||||
): GuestResponse {
|
||||
requireMember(propertyAccess, propertyId, principal)
|
||||
val property = requireProperty(propertyRepo, propertyId)
|
||||
|
||||
val phone = request.phoneE164.trim()
|
||||
if (phone.isBlank()) {
|
||||
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "phoneE164 required")
|
||||
}
|
||||
val existing = guestRepo.findByPropertyIdAndPhoneE164(propertyId, phone)
|
||||
if (existing != null) {
|
||||
throw ResponseStatusException(HttpStatus.CONFLICT, "Guest already exists")
|
||||
}
|
||||
|
||||
val now = OffsetDateTime.now()
|
||||
val guest = Guest(
|
||||
property = property,
|
||||
phoneE164 = phone,
|
||||
name = request.name?.trim()?.ifBlank { null },
|
||||
nationality = request.nationality?.trim()?.ifBlank { null },
|
||||
addressText = request.addressText?.trim()?.ifBlank { null },
|
||||
updatedAt = now
|
||||
)
|
||||
guestRepo.save(guest)
|
||||
return setOf(guest).toResponse(guestVehicleRepo, guestRatingRepo).first()
|
||||
}
|
||||
|
||||
@GetMapping("/search")
|
||||
fun search(
|
||||
@PathVariable propertyId: UUID,
|
||||
|
||||
@@ -49,6 +49,13 @@ data class GuestResponse(
|
||||
val averageScore: Double?
|
||||
)
|
||||
|
||||
data class GuestCreateRequest(
|
||||
val phoneE164: String,
|
||||
val name: String? = null,
|
||||
val nationality: String? = null,
|
||||
val addressText: String? = null
|
||||
)
|
||||
|
||||
data class GuestVehicleRequest(
|
||||
val vehicleNumber: String,
|
||||
val bookingId: UUID
|
||||
|
||||
Reference in New Issue
Block a user