-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathTicketController.java
More file actions
85 lines (61 loc) · 3 KB
/
TicketController.java
File metadata and controls
85 lines (61 loc) · 3 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
package com.booleanuk.api.cinema.Controller;
import com.booleanuk.api.cinema.Model.Customer;
import com.booleanuk.api.cinema.Model.Movie;
import com.booleanuk.api.cinema.Model.Screening;
import com.booleanuk.api.cinema.Model.Ticket;
import com.booleanuk.api.cinema.Repository.CustomerRepository;
import com.booleanuk.api.cinema.Repository.MovieRepository;
import com.booleanuk.api.cinema.Repository.ScreeningRepository;
import com.booleanuk.api.cinema.Repository.TicketRepository;
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.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("customers/{customerId}/screenings/{screeningId}")
public class TicketController {
@Autowired
private CustomerRepository customerRepository;
@Autowired
private ScreeningRepository screeningRepository;
@Autowired
private TicketRepository ticketRepository;
@PostMapping()
public ResponseEntity<Ticket> createTicket(@RequestBody Ticket ticket, @PathVariable("screeningId") Integer
screeningId, @PathVariable("customerId") Integer customerId){
try {
Screening screening = this.screeningRepository.findById(
screeningId).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "No screening with that id exists")
);
Customer customer = this.customerRepository.findById(
customerId).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "No customer with that id exists")
);
ticket.setCustomer(customer);
ticket.setScreening(screening);
return new ResponseEntity<Ticket>(this.ticketRepository.save(ticket),
HttpStatus.CREATED);
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Could not create ticket for the specified " +
"customer/screening, please check all required fields are correct.");
}
}
@GetMapping()
public ResponseEntity<List<Ticket>> getAll(@PathVariable("screeningId") Integer
screeningId, @PathVariable("customerId") Integer customerId) {
List<Ticket> allTickets=new ArrayList<>();
Customer customer = this.customerRepository.findById(
customerId).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "No customer with that id exists")
);
Screening screening = this.screeningRepository.findById(
screeningId).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "No customer with that id exists")
);
return new ResponseEntity<List<Ticket>>(screening.getTickets(), HttpStatus.FOUND);
}
}