api/customer
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package com.aitrainer.api.controller
|
||||
|
||||
import com.aitrainer.api.model.Customer
|
||||
import com.aitrainer.api.repository.CustomerRepository
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.web.bind.annotation.*
|
||||
import java.util.*
|
||||
import javax.validation.Valid
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
class CustomerController ( private val customerRepository: CustomerRepository ) {
|
||||
|
||||
@GetMapping("/customers")
|
||||
fun getAllCustomers(): List<Customer> =
|
||||
customerRepository.findAll()
|
||||
|
||||
@PostMapping("/customers")
|
||||
fun createNewArticle(@Valid @RequestBody customer: Customer): Customer =
|
||||
customerRepository.save(customer)
|
||||
|
||||
@GetMapping("/customers/{id}")
|
||||
fun getCustomerById(@PathVariable(value = "id") customerId: Long): ResponseEntity<Customer> {
|
||||
return customerRepository.findById(customerId).map { customer ->
|
||||
ResponseEntity.ok(customer)
|
||||
}.orElse(ResponseEntity.notFound().build())
|
||||
}
|
||||
|
||||
@PutMapping("/customers/{id}")
|
||||
fun updateCustomerById(@PathVariable(value = "id") customerId: Long,
|
||||
@Valid @RequestBody newCustomer: Customer): ResponseEntity<Customer> {
|
||||
|
||||
return customerRepository.findById(customerId).map { existingCustomer ->
|
||||
val updatedCustomer: Customer = existingCustomer
|
||||
.copy(name = newCustomer.name,
|
||||
firstname = newCustomer.firstname,
|
||||
sex = newCustomer.sex,
|
||||
age = newCustomer.age)
|
||||
ResponseEntity.ok().body(customerRepository.save(updatedCustomer))
|
||||
}.orElse(ResponseEntity.notFound().build())
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user