add customer controller with find by guid method

This commit is contained in:
Denis Savosin
2024-10-14 12:25:30 +07:00
parent fecbee8b28
commit 0a38f7fdd8
10 changed files with 189 additions and 32 deletions

View File

@@ -0,0 +1,30 @@
package com.example.demo.http.controllers
import com.example.demo.http.exceptions.NotFoundException
import com.example.demo.services.database.customer.CustomerService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.util.*
@RestController
@RequestMapping(value = ["/api/customer"], produces = [MediaType.APPLICATION_JSON_VALUE])
class CustomerController(
@Autowired
private val customerService: CustomerService,
) {
@GetMapping("/{guid}")
@Throws(NotFoundException::class)
fun getCustomer(
@PathVariable guid: UUID,
): ResponseEntity<Any> {
val customer = customerService.findByGuid(guid) ?: throw NotFoundException()
return ResponseEntity(customer, HttpStatus.OK)
}
}

View File

@@ -10,6 +10,7 @@ import java.time.OffsetDateTime
import java.util.*
@Table("customer")
@Serializable
data class Customer(
@Id
val id: Long?,

View File

@@ -0,0 +1,9 @@
package com.example.demo.models
import kotlinx.serialization.Serializable
@Serializable
data class CustomerExtended(
val customer: Customer,
val city: City?,
)

View File

@@ -1,11 +1,12 @@
package com.example.demo.services.database.customer
import com.example.demo.models.Customer
import com.example.demo.models.CustomerExtended
import com.example.demo.services.database.exceptions.CityNotFoundException
import java.util.*
interface CustomerService {
fun findByGuid(guid: UUID): Customer?
fun findByGuid(guid: UUID): CustomerExtended?
@Throws(CityNotFoundException::class)
fun create(name: String, cityGuid: UUID?): Customer

View File

@@ -1,6 +1,7 @@
package com.example.demo.services.database.customer
import com.example.demo.models.Customer
import com.example.demo.models.CustomerExtended
import com.example.demo.providers.CityRepository
import com.example.demo.providers.CustomerRepository
import com.example.demo.services.database.exceptions.CityNotFoundException
@@ -11,7 +12,17 @@ class CustomerServiceImpl(
private val customerRepository: CustomerRepository,
private val cityRepository: CityRepository
): CustomerService {
override fun findByGuid(guid: UUID): Customer? = customerRepository.findByGuid(guid)
override fun findByGuid(guid: UUID): CustomerExtended? {
val customer = customerRepository.findByGuid(guid) ?: return null
if (customer.cityId == null) {
return CustomerExtended(customer, null)
}
val city = cityRepository.findById(customer.cityId)
return CustomerExtended(customer, city.orElse(null))
}
override fun create(name: String, cityGuid: UUID?): Customer {
val cityId: Long? = cityGuid?.let {