-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathCustomerController.java
More file actions
94 lines (73 loc) · 3.65 KB
/
CustomerController.java
File metadata and controls
94 lines (73 loc) · 3.65 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.booleanuk.api.cinema.controllers;
import com.booleanuk.api.cinema.models.Customer;
import com.booleanuk.api.cinema.repository.CustomerRepository;
import com.booleanuk.api.cinema.response.CustomerListResponse;
import com.booleanuk.api.cinema.response.CustomerResponse;
import com.booleanuk.api.cinema.response.ErrorResponse;
import com.booleanuk.api.cinema.response.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;
@RestController
@RequestMapping("customers")
public class CustomerController {
private CustomerListResponse customerListResponse = new CustomerListResponse();
private CustomerResponse customerResponse = new CustomerResponse();
@Autowired
private CustomerRepository customerRepository;
@GetMapping
public ResponseEntity<Response<?>> getAllCustomers () {
List<Customer> customers = customerRepository.findAll();
this.customerListResponse.set(customers);
return ResponseEntity.ok(customerListResponse);
}
@PostMapping
public ResponseEntity<Response<?>> createCustomer(@RequestBody Customer customer){
if (customer.getName() == null || customer.getEmail() == null || customer.getPhone() == null){
ErrorResponse error = new ErrorResponse();
error.set("Could not create a new customer, please check all fields are correct");
return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST);
}
customer.setCreatedAt(LocalDateTime.now());
customer.setUpdatedAt(LocalDateTime.now());
this.customerResponse.set(customer);
this.customerRepository.save(customer);
return new ResponseEntity<>(customerResponse, HttpStatus.CREATED);
}
@PutMapping("{id}")
public ResponseEntity<Response<?>> updateCustomer(@PathVariable int id, @RequestBody Customer customer){
if (customer.getName() == null || customer.getEmail() == null || customer.getPhone() == null){
ErrorResponse error = new ErrorResponse();
error.set("Could not create a new customer, please check all fields are correct");
return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST);
}
Customer customerToUpdate = customerRepository.findById(id).orElse(null);
if(customerToUpdate == null){
ErrorResponse error = new ErrorResponse();
error.set("No customer with that ID found");
return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
}
customerToUpdate.setName(customer.getName());
customerToUpdate.setEmail(customer.getEmail());
customerToUpdate.setPhone(customer.getPhone());
customerToUpdate.setUpdatedAt(LocalDateTime.now());
this.customerResponse.set(customerToUpdate);
this.customerRepository.save(customerToUpdate);
return new ResponseEntity<>(customerResponse, HttpStatus.CREATED);
}
@DeleteMapping("{id}")
public ResponseEntity<Response<?>> deleteCustomer(@PathVariable int id){
Customer customerToDelete = this.customerRepository.findById(id).orElse(null);
if (customerToDelete == null){
ErrorResponse error = new ErrorResponse();
error.set("No customer with that ID found");
return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
}
this.customerResponse.set(customerToDelete);
this.customerRepository.delete(customerToDelete);
return ResponseEntity.ok(customerResponse);
}
}