-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathCustomerController.java
More file actions
57 lines (48 loc) · 2.46 KB
/
CustomerController.java
File metadata and controls
57 lines (48 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.booleanuk.api.cinema.customers;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
@RestController
@RequestMapping("customers")
public class CustomerController {
@Autowired
private CustomerRepository customerRepository;
@GetMapping
public ResponseEntity<List<Customer>> getAllCustomers() {
return ResponseEntity.ok(customerRepository.findAll());
}
@PostMapping
public ResponseEntity<Customer> addCustomer(@RequestBody Customer customer) {
customer.setCreatedAt(LocalDateTime.now().toString());
customer.setUpdatedAt(LocalDateTime.now().toString());
return new ResponseEntity<Customer>(customerRepository.save(customer), HttpStatus.CREATED);
}
@PutMapping("{id}")
public ResponseEntity<Customer> updateCustomer(@PathVariable int id, @RequestBody Customer customer) {
Customer customerToUpdate = customerRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Could not find customer with this id"));
customerToUpdate.setName(customer.getName());
customerToUpdate.setEmail(customer.getEmail());
customerToUpdate.setPhone(customer.getPhone());
customerToUpdate.setUpdatedAt(LocalDateTime.now().toString());
return new ResponseEntity<Customer>(customerRepository.save(customerToUpdate), HttpStatus.CREATED);
}
@DeleteMapping("{id}")
public ResponseEntity<Customer> deleteCustomer(@PathVariable int id) {
Customer customerToDelete = customerRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Could not find customer with this id"));
customerRepository.delete(customerToDelete);
return ResponseEntity.ok(customerToDelete);
}
}