updateCustomer fix
This commit is contained in:
@@ -9,6 +9,7 @@ import org.springframework.http.HttpHeaders
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.security.access.annotation.Secured
|
||||
import org.springframework.web.bind.annotation.*
|
||||
import java.util.*
|
||||
import javax.validation.Valid
|
||||
|
||||
|
||||
@@ -44,19 +45,59 @@ class CustomerController ( private val customerRepository: CustomerRepository )
|
||||
customerRepository.findByActive(active)
|
||||
|
||||
@Secured
|
||||
@PutMapping("/customers/{id}")
|
||||
@PostMapping("/customers/{id}")
|
||||
fun updateCustomerById(@PathVariable(value = "id") customerId: Long,
|
||||
@Valid @RequestBody newCustomer: Customer,
|
||||
@RequestHeader headers: HttpHeaders): ResponseEntity<Customer> {
|
||||
|
||||
return customerRepository.findById(customerId).map { existingCustomer ->
|
||||
val returnCustomer: Customer = customerRepository.findById(customerId).orElse(null)
|
||||
?: return ResponseEntity.notFound().build()
|
||||
|
||||
val password: String? = newCustomer.password
|
||||
val updatedCustomer: Customer
|
||||
if ( password == null || password.isEmpty() ) {
|
||||
updatedCustomer =
|
||||
returnCustomer.copy(
|
||||
name = newCustomer.name,
|
||||
firstname = newCustomer.firstname,
|
||||
sex = newCustomer.sex,
|
||||
birthYear = newCustomer.birthYear,
|
||||
fitnessLevel = newCustomer.fitnessLevel,
|
||||
bodyType = newCustomer.bodyType,
|
||||
goal = newCustomer.goal,
|
||||
weight = newCustomer.weight
|
||||
)
|
||||
} else {
|
||||
updatedCustomer =
|
||||
returnCustomer.copy(
|
||||
name = newCustomer.name,
|
||||
firstname = newCustomer.firstname,
|
||||
password = newCustomer.password,
|
||||
sex = newCustomer.sex,
|
||||
birthYear = newCustomer.birthYear,
|
||||
fitnessLevel = newCustomer.fitnessLevel,
|
||||
bodyType = newCustomer.bodyType,
|
||||
goal = newCustomer.goal,
|
||||
weight = newCustomer.weight
|
||||
)
|
||||
}
|
||||
|
||||
return ResponseEntity.ok().body(customerRepository.save(updatedCustomer))
|
||||
|
||||
/* return customerRepository.findById(customerId).map {
|
||||
existingCustomer ->
|
||||
val updatedCustomer: Customer = existingCustomer
|
||||
.copy(name = newCustomer.name,
|
||||
firstname = newCustomer.firstname,
|
||||
sex = newCustomer.sex,
|
||||
age = newCustomer.age)
|
||||
birthYear = newCustomer.birthYear,
|
||||
fitnessLevel = newCustomer.fitnessLevel,
|
||||
bodyType = newCustomer.bodyType,
|
||||
goal = newCustomer.goal,
|
||||
weight = newCustomer.weight
|
||||
)
|
||||
ResponseEntity.ok().body(customerRepository.save(updatedCustomer))
|
||||
}.orElse(ResponseEntity.notFound().build())
|
||||
}.orElse(ResponseEntity.notFound().build()) */
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user