Example
package org.bookmytickets.controller;
import java.util.List;
import org.bookmytickets.model.Customer;
import org.bookmytickets.repository.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/customer")
public class CustomerController {
@Autowired
private CustomerRepository repository;
@GetMapping("")
public List<Customer> selectAll(){
List<Customer> customerList = repository.findAll();
return customerList;
}
@GetMapping("/{id}")
public List<Customer> getSpecificCustomer(@PathVariable String id){
return repository.findById(id);
}
@GetMapping("/search/lastName/{lastName}")
public List<Customer> searchByLastName(@PathVariable String lastName){
return repository.findByLasttName(lastName);
}
@GetMapping("/search/firstname/{firstname}")
public List<Customer> searchByFirstName(@PathVariable String firstName){
return repository.findByFirstName(firstName);
}
@PostMapping("")
public void insert(@RequestBody Customer customer) {
repository.save(customer);
}
@PatchMapping("/{id}")
public void update(@RequestParam String id, @RequestBody Customer customer) {
Customer oldCustomer = repository.finedById(id);
if(customer.getFirstName() != null) {
oldCustomer.setFristName(customer.getFirstName());
}
if(customer.getLastName() != null) {
oldCustomer.setLastName(customer.getLastName());
}
repository.save(oldCustomer);
}
@DeleteMapping("/{id}")
public void delete(@RequestParam String id) {
Customer deleteCustomer = repository.findById(id);
repository.delete(deleteCustomer);
}
}