Merge branch 'tibor' into 'master'
updateCustomer fix See merge request bossanyit/aitrainer_server!7
This commit is contained in:
commit
8f86b5c145
@ -9,6 +9,7 @@ import org.springframework.http.HttpHeaders
|
|||||||
import org.springframework.http.ResponseEntity
|
import org.springframework.http.ResponseEntity
|
||||||
import org.springframework.security.access.annotation.Secured
|
import org.springframework.security.access.annotation.Secured
|
||||||
import org.springframework.web.bind.annotation.*
|
import org.springframework.web.bind.annotation.*
|
||||||
|
import java.util.*
|
||||||
import javax.validation.Valid
|
import javax.validation.Valid
|
||||||
|
|
||||||
|
|
||||||
@ -44,19 +45,59 @@ class CustomerController ( private val customerRepository: CustomerRepository )
|
|||||||
customerRepository.findByActive(active)
|
customerRepository.findByActive(active)
|
||||||
|
|
||||||
@Secured
|
@Secured
|
||||||
@PutMapping("/customers/{id}")
|
@PostMapping("/customers/{id}")
|
||||||
fun updateCustomerById(@PathVariable(value = "id") customerId: Long,
|
fun updateCustomerById(@PathVariable(value = "id") customerId: Long,
|
||||||
@Valid @RequestBody newCustomer: Customer,
|
@Valid @RequestBody newCustomer: Customer,
|
||||||
@RequestHeader headers: HttpHeaders): ResponseEntity<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
|
val updatedCustomer: Customer = existingCustomer
|
||||||
.copy(name = newCustomer.name,
|
.copy(name = newCustomer.name,
|
||||||
firstname = newCustomer.firstname,
|
firstname = newCustomer.firstname,
|
||||||
sex = newCustomer.sex,
|
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))
|
ResponseEntity.ok().body(customerRepository.save(updatedCustomer))
|
||||||
}.orElse(ResponseEntity.notFound().build())
|
}.orElse(ResponseEntity.notFound().build()) */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@ import org.junit.jupiter.api.Test
|
|||||||
import org.junit.jupiter.api.TestInstance
|
import org.junit.jupiter.api.TestInstance
|
||||||
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
import org.springframework.boot.test.context.SpringBootTest
|
import org.springframework.boot.test.context.SpringBootTest
|
||||||
|
import org.springframework.http.HttpHeaders
|
||||||
import org.springframework.http.HttpStatus
|
import org.springframework.http.HttpStatus
|
||||||
import org.springframework.http.ResponseEntity
|
import org.springframework.http.ResponseEntity
|
||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
@ -69,7 +70,7 @@ class CustomerTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testUpdateCustomer() {
|
fun testUpdateCustomer() {
|
||||||
var id:Long = 90
|
val id:Long = 90
|
||||||
|
|
||||||
val customer: Customer = customerRepository.findById( id ).orElse(null)
|
val customer: Customer = customerRepository.findById( id ).orElse(null)
|
||||||
|
|
||||||
@ -91,6 +92,44 @@ class CustomerTests {
|
|||||||
assertEquals(updatedCustomer.fitnessLevel, "advanced")
|
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
|
@Test
|
||||||
fun testRegistration() {
|
fun testRegistration() {
|
||||||
val json = "{\"username\":\"bosi@example.com\",\"password\":\"94385\"}"
|
val json = "{\"username\":\"bosi@example.com\",\"password\":\"94385\"}"
|
||||||
|
Loading…
Reference in New Issue
Block a user