-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathCustomerController.java
More file actions
157 lines (122 loc) · 6.14 KB
/
CustomerController.java
File metadata and controls
157 lines (122 loc) · 6.14 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.booleanuk.api.cinema.customer.controller;
import com.booleanuk.api.cinema.customer.model.Customer;
import com.booleanuk.api.cinema.customer.model.CustomerResponseDTO;
import com.booleanuk.api.cinema.customer.repository.CustomerRepository;
import com.booleanuk.api.cinema.response.Response;
import com.booleanuk.api.cinema.response.ResponseFactory;
import com.booleanuk.api.cinema.screening.model.Screening;
import com.booleanuk.api.cinema.screening.repository.ScreeningRepository;
import com.booleanuk.api.cinema.ticket.model.Ticket;
import com.booleanuk.api.cinema.ticket.repository.TicketRepository;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static com.booleanuk.api.cinema.response.ResponseFactory.*;
@RestController
@RequestMapping("/customers")
public class CustomerController {
@Autowired
CustomerRepository customerRepository;
@Autowired
TicketRepository ticketRepository;
@Autowired
ScreeningRepository screeningRepository;
// Workaround for exception 415
@PostMapping(consumes = {"application/json", "application/json;charset=UTF-8"})
public ResponseEntity<Response> addCustomer(@Valid @RequestBody Customer customer, BindingResult result) {
if (result.hasErrors()) {
return badRequestErrorResponse();
}
Customer savedCustomer = this.customerRepository.save(customer);
CustomerResponseDTO response = convertToCustomerResponseDTO(savedCustomer);
return createdSuccessResponse(response);
}
@GetMapping
public ResponseEntity<Response> getAllCustomers() {
List<CustomerResponseDTO> response = new ArrayList<>();
this.customerRepository.findAll().forEach(customer ->
response.add(convertToCustomerResponseDTO(customer))
);
return okSuccessResponse(response);
}
@GetMapping("/{id}")
public ResponseEntity<Response> getCustomerById(@PathVariable (name = "id") int id) {
return this.customerRepository.findById(id).
map(customer -> {
CustomerResponseDTO response = convertToCustomerResponseDTO(customer);
return okSuccessResponse(response);
})
.orElseGet(ResponseFactory::notFoundErrorResponse);
}
@PutMapping("/{id}")
public ResponseEntity<Response> updateCustomer(@PathVariable (name = "id") int id, @Valid @RequestBody Customer updatedCustomer, BindingResult result) {
if (result.hasErrors()) {
return badRequestErrorResponse();
}
return this.customerRepository.findById(id).map(customerToUpdate -> {
updateCustomerDetails(customerToUpdate, updatedCustomer);
Customer savedCustomer = this.customerRepository.save(customerToUpdate);
CustomerResponseDTO response = convertToCustomerResponseDTO(savedCustomer);
return createdSuccessResponse(response);
}).orElseGet(ResponseFactory::notFoundErrorResponse);
}
@DeleteMapping("/{id}")
public ResponseEntity<Response> deleteCustomer(@PathVariable (name = "id") int id){
return this.customerRepository.findById(id).map(customerToDelete -> {
this.customerRepository.delete(customerToDelete);
return okSuccessResponse(customerToDelete);
}).orElseGet(ResponseFactory::notFoundErrorResponse);
}
/* Tickets */
@PostMapping("/{customerId}/screenings/{screeningId}")
public ResponseEntity<Response> bookTicket(@PathVariable (name = "customerId") int customerId,
@PathVariable (name = "screeningId") int screeningId,
@Valid @RequestBody Ticket ticket, BindingResult result) {
if (result.hasErrors()) {
return badRequestErrorResponse();
}
Optional<Customer> optionalCustomer = this.customerRepository.findById(customerId);
if (optionalCustomer.isEmpty()){
return notFoundErrorResponse();
}
Optional<Screening> optionalScreening = this.screeningRepository.findById(screeningId);
if (optionalScreening.isEmpty()){
return notFoundErrorResponse();
}
Customer customer = optionalCustomer.get();
Screening screening = optionalScreening.get();
ticket.setCustomer(customer);
ticket.setScreening(screening);
Ticket savedTicket = this.ticketRepository.save(ticket);
return createdSuccessResponse(savedTicket);
}
@GetMapping("/{customerId}/screenings/{screeningId}")
public ResponseEntity<Response> getAllTickets(@PathVariable (name = "customerId") int customerId,
@PathVariable (name = "screeningId") int screeningId) {
if (this.customerRepository.findById(customerId).isEmpty()) {
return notFoundErrorResponse();
}
if (this.screeningRepository.findById(screeningId).isEmpty()) {
return notFoundErrorResponse();
}
Customer customer = this.customerRepository.findById(customerId).get();
Screening screening = this.screeningRepository.findById(screeningId).get();
List<Ticket> ticketList = this.ticketRepository.findAllByCustomerAndScreening(customer, screening);
return okSuccessResponse(ticketList);
}
private void updateCustomerDetails(Customer oldCustomer, Customer newCustomer) {
oldCustomer.setName(newCustomer.getName());
oldCustomer.setPhone(newCustomer.getPhone());
oldCustomer.setEmail(newCustomer.getEmail());
oldCustomer.setUpdatedAt(OffsetDateTime.now());
}
private CustomerResponseDTO convertToCustomerResponseDTO(Customer customer){
return new CustomerResponseDTO(customer.getId(), customer.getName(), customer.getEmail(), customer.getPhone(), customer.getCreatedAt(), customer.getUpdatedAt());
}
}