mirror of
https://github.com/Dannecron/spring-boot-demo.git
synced 2025-12-26 08:42:33 +03:00
add customer controller with find by guid method
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import java.time.OffsetDateTime
|
||||
import java.util.*
|
||||
|
||||
@Table("customer")
|
||||
@Serializable
|
||||
data class Customer(
|
||||
@Id
|
||||
val id: Long?,
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.example.demo.models
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class CustomerExtended(
|
||||
val customer: Customer,
|
||||
val city: City?,
|
||||
)
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user