-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathCustomerController.java
More file actions
58 lines (48 loc) · 2.21 KB
/
CustomerController.java
File metadata and controls
58 lines (48 loc) · 2.21 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
58
package com.booleanuk.api.cinema.customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import java.time.LocalDateTime;
import java.util.List;
@RestController
@RequestMapping("customers")
public class CustomerController {
@Autowired
private CustomerRepository customerRepository;
@GetMapping
public List<Customer> getAllCustomers() {
return this.customerRepository.findAll();
}
// @GetMapping("/{id}")
// public ResponseEntity<Customer> getOneCustomer(@PathVariable int id) {
// return this.customerRepository.findById(id)
// .map(ResponseEntity::ok)
// .orElse(ResponseEntity.notFound().build());
// }
@PostMapping
public ResponseEntity<Customer> create(@RequestBody Customer customer) {
customer.setCreated_at(String.valueOf(LocalDateTime.now()));
return new ResponseEntity<Customer>(this.customerRepository.save(customer), HttpStatus.CREATED);
}
@PutMapping("/{id}")
public ResponseEntity<Customer> updateCustomer(@PathVariable int id, @RequestBody Customer customer) {
Customer customerToUpdate = this.customerRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found")
);
customerToUpdate.setName(customer.getName());
customerToUpdate.setEmail(customer.getEmail());
customerToUpdate.setPhone(customer.getPhone());
customerToUpdate.setUpdated_at(String.valueOf(LocalDateTime.now()));
return new ResponseEntity<Customer>(this.customerRepository.save(customerToUpdate), HttpStatus.CREATED);
}
@DeleteMapping("/{id}")
public ResponseEntity<Customer> deleteCustomer(@PathVariable int id) {
Customer customerToDelete = this.customerRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found")
);
this.customerRepository.delete(customerToDelete);
return ResponseEntity.ok(customerToDelete);
}
}