-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathTicketController.java
More file actions
74 lines (64 loc) · 2.78 KB
/
TicketController.java
File metadata and controls
74 lines (64 loc) · 2.78 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
package com.booleanuk.api.cinema.controllers;
import com.booleanuk.api.cinema.models.Customer;
import com.booleanuk.api.cinema.models.Screening;
import com.booleanuk.api.cinema.models.Ticket;
import com.booleanuk.api.cinema.repositories.CustomerRepository;
import com.booleanuk.api.cinema.repositories.ScreeningRepository;
import com.booleanuk.api.cinema.repositories.TicketRepository;
import com.booleanuk.api.cinema.responses.*;
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.util.List;
@RestController
@RequestMapping("customers/{customerId}/screenings/{screeningId}")
public class TicketController {
@Autowired
TicketRepository repository;
@Autowired
CustomerRepository customerRepository;
@Autowired
ScreeningRepository screeningRepository;
@PostMapping
public ResponseEntity<Response<?>> create(
@PathVariable int customerId,
@PathVariable int screeningId,
@RequestBody Ticket ticket) {
Customer customer = this.customerRepository.findById(customerId).orElse(null);
Screening screening = this.screeningRepository.findById(screeningId).orElse(null);
if (customer == null || screening == null) {
// TODO: Could improve ux with better error message / more specific
ErrorResponse error = new ErrorResponse();
error.set("Invalid customer or screening id");
return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST);
}
ticket.setCustomer(customer);
ticket.setScreening(screening);
try {
this.repository.save(ticket);
} catch (Exception e) {
ErrorResponse error = new ErrorResponse();
error.set("Could not create a new ticket, please check all fields are correct.");
return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST);
}
TicketResponse response = new TicketResponse();
response.set(ticket);
return new ResponseEntity<>(response, HttpStatus.CREATED);
}
@GetMapping
public ResponseEntity<Response<?>> getAll(
@PathVariable int customerId,
@PathVariable int screeningId
) {
TicketListResponse response = new TicketListResponse();
List<Ticket> tickets = this.repository.findAllByCustomerIdAndScreeningId(customerId, screeningId);
if (tickets.isEmpty()) {
ErrorResponse errorResponse = new ErrorResponse();
errorResponse.set("No tickets found");
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}
response.set(tickets);
return ResponseEntity.ok(response);
}
}