diff --git a/src/main/kotlin/com/aitrainer/api/controller/CustomerController.kt b/src/main/kotlin/com/aitrainer/api/controller/CustomerController.kt index 9ab807b..78e9ada 100644 --- a/src/main/kotlin/com/aitrainer/api/controller/CustomerController.kt +++ b/src/main/kotlin/com/aitrainer/api/controller/CustomerController.kt @@ -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 { - 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()) */ } diff --git a/src/test/kotlin/com/aitrainer/api/test/CustomerTests.kt b/src/test/kotlin/com/aitrainer/api/test/CustomerTests.kt index ae53ccd..89f1e1d 100644 --- a/src/test/kotlin/com/aitrainer/api/test/CustomerTests.kt +++ b/src/test/kotlin/com/aitrainer/api/test/CustomerTests.kt @@ -10,6 +10,7 @@ import org.junit.jupiter.api.Test import org.junit.jupiter.api.TestInstance import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest +import org.springframework.http.HttpHeaders import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity import kotlin.test.assertEquals @@ -69,7 +70,7 @@ class CustomerTests { @Test fun testUpdateCustomer() { - var id:Long = 90 + val id:Long = 90 val customer: Customer = customerRepository.findById( id ).orElse(null) @@ -91,6 +92,44 @@ class CustomerTests { assertEquals(updatedCustomer.fitnessLevel, "advanced") } + @Test + fun testUpdateCustomer2() { + var id: Long = 90 + + // update no password + val customer = Customer() + + customer.customerId = id + customer.firstname = "Tib" + customer.name = "Bossi" + customer.admin = 1 + customer.sex = "m" + customer.fitnessLevel = "intermediate" + customer.weight = 79 + customer.birthYear = 1972 + + val customerController = CustomerController(customerRepository) + var response: ResponseEntity<*> = customerController.updateCustomerById(id, customer, HttpHeaders.readOnlyHttpHeaders(HttpHeaders.EMPTY) ) + var newCustomer: Customer? = response.body as Customer + assertEquals(response.statusCode, HttpStatus.OK) + assertEquals(newCustomer?.weight, 79) + + // test not found + id = 1000 + response = customerController.updateCustomerById(id, customer, HttpHeaders.readOnlyHttpHeaders(HttpHeaders.EMPTY) ) + assertEquals(response.statusCode, HttpStatus.NOT_FOUND) + + + // update Password + id = 90 + customer.password = "blabal" + response = customerController.updateCustomerById(id, customer, HttpHeaders.readOnlyHttpHeaders(HttpHeaders.EMPTY) ) + assertEquals(response.statusCode, HttpStatus.OK) + newCustomer = response.body as Customer + assertEquals(newCustomer.password, "blabal") + + } + @Test fun testRegistration() { val json = "{\"username\":\"bosi@example.com\",\"password\":\"94385\"}"