-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathCustomerController.java
More file actions
138 lines (117 loc) · 5.4 KB
/
CustomerController.java
File metadata and controls
138 lines (117 loc) · 5.4 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.booleanuk.library.controllers;
import com.booleanuk.library.models.Customer;
import com.booleanuk.library.models.Screening;
import com.booleanuk.library.payload.response.*;
import com.booleanuk.library.repository.CustomerRepository;
import com.booleanuk.library.repository.ScreeningRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("customers")
public class CustomerController {
@Autowired
private CustomerRepository customerRepository;
@Autowired
private ScreeningRepository screeningRepository;
//extension
@PostMapping("/{custId}/screenings/{screenId}")
public ResponseEntity<Response<?>> createScreening(@PathVariable int id, @RequestBody Screening screening) {
// Movie movieToUpdate = this.movieRepository.findById(id).orElse(null);
// if (movieToUpdate == null) {
// ErrorResponse error = new ErrorResponse();
// error.set("not found");
// return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
// }
ScreeningResponse screeningResponse = new ScreeningResponse();
try {
screeningResponse.set(this.screeningRepository.save(screening));
} catch (Exception e) {
ErrorResponse error = new ErrorResponse();
error.set("Bad request");
return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>(screeningResponse, HttpStatus.CREATED);
}
@GetMapping("/{custId}/screenings/{screenId}")
public ResponseEntity<Response<?>> getScreeningById(@PathVariable int id) {
Screening screening = this.screeningRepository.findById(id).orElse(null);
if (screening == null) {
ErrorResponse error = new ErrorResponse();
error.set("not found");
return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
}
ScreeningResponse screeningResponse = new ScreeningResponse();
screeningResponse.set(screening);
return ResponseEntity.ok(screeningResponse);
}
//core
@GetMapping
public ResponseEntity<CustomerListResponse> getAllCustomers() {
CustomerListResponse customerListResponse = new CustomerListResponse();
customerListResponse.set(this.customerRepository.findAll());
return ResponseEntity.ok(customerListResponse);
}
@PostMapping
public ResponseEntity<Response<?>> createCustomer(@RequestBody Customer customer) {
CustomerResponse customerResponse = new CustomerResponse();
try {
customerResponse.set(this.customerRepository.save(customer));
} catch (Exception e) {
ErrorResponse error = new ErrorResponse();
error.set("Bad request");
return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>(customerResponse, HttpStatus.CREATED);
}
@GetMapping("/{id}")
public ResponseEntity<Response<?>> getCustomerById(@PathVariable int id) {
Customer customer = this.customerRepository.findById(id).orElse(null);
if (customer == null) {
ErrorResponse error = new ErrorResponse();
error.set("not found");
return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
}
CustomerResponse customerResponse = new CustomerResponse();
customerResponse.set(customer);
return ResponseEntity.ok(customerResponse);
}
@PutMapping("/{id}")
public ResponseEntity<Response<?>> updateCustomer(@PathVariable int id, @RequestBody Customer customer) {
Customer customerToUpdate = this.customerRepository.findById(id).orElse(null);
if (customerToUpdate == null) {
ErrorResponse error = new ErrorResponse();
error.set("not found");
return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
}
customerToUpdate.setName(customer.getName());
customerToUpdate.setEmail(customer.getEmail());
customerToUpdate.setPhone(customer.getPhone());
customerToUpdate.setCreatedAt(customer.getCreatedAt());
customerToUpdate.setUpdatedAt(customer.getUpdatedAt());
try {
customerToUpdate = this.customerRepository.save(customerToUpdate);
} catch (Exception e) {
ErrorResponse error = new ErrorResponse();
error.set("Bad request");
return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST);
}
CustomerResponse customerResponse = new CustomerResponse();
customerResponse.set(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("not found");
return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
}
this.customerRepository.delete(customerToDelete);
CustomerResponse customerResponse = new CustomerResponse();
customerResponse.set(customerToDelete);
return ResponseEntity.ok(customerResponse);
}
}