-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathTicketController.java
More file actions
67 lines (59 loc) · 3.04 KB
/
TicketController.java
File metadata and controls
67 lines (59 loc) · 3.04 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
package com.booleanuk.api.cinema.Controller;
import com.booleanuk.api.cinema.Model.Ticket;
import com.booleanuk.api.cinema.Repository.TicketRepository;
import com.booleanuk.api.cinema.ResponseWrapper.ResponseWrapper;
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.Optional;
@RestController
@RequestMapping("tickets")
public class TicketController {
@Autowired
private TicketRepository ticketRepository;
@ResponseStatus(HttpStatus.CREATED)
@PostMapping
public ResponseEntity<ResponseWrapper<Object>> create(@RequestBody Ticket newTicket) {
try {
Ticket savedTicket = this.ticketRepository.save(newTicket);
return ResponseEntity.status(HttpStatus.CREATED).body(new ResponseWrapper<>("success", savedTicket));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseWrapper<>("error", "Could not create ticket, please check all required fields are correct."));
}
}
@ResponseStatus(HttpStatus.OK)
@GetMapping
public ResponseWrapper getAll() {
return new ResponseWrapper<>("success", this.ticketRepository.findAll());
}
@ResponseStatus(HttpStatus.CREATED)
@PutMapping("{id}")
public ResponseEntity<ResponseWrapper<Object>> update(@PathVariable("id") Integer id, @RequestBody Ticket updatedTicket) {
Optional<Ticket> existingTicketOptional = this.ticketRepository.findById(id);
if (existingTicketOptional.isPresent()) {
try {
Ticket existingTicket = existingTicketOptional.get();
existingTicket.setCustomerId(updatedTicket.getCustomerId());
existingTicket.setScreeningId(updatedTicket.getScreeningId());
existingTicket.setNumSeats(updatedTicket.getNumSeats());
Ticket savedTicket = this.ticketRepository.save(existingTicket);
return ResponseEntity.status(HttpStatus.CREATED).body(new ResponseWrapper<>("success", savedTicket));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseWrapper<>("error", "Could not update ticket, please check all fields are correct."));
}
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ResponseWrapper<>("error", "No ticket with that id found."));
}
}
@ResponseStatus(HttpStatus.OK)
@DeleteMapping("{id}")
public ResponseEntity<ResponseWrapper<Object>> delete(@PathVariable("id") Integer id) {
if (this.ticketRepository.existsById(id)) {
this.ticketRepository.deleteById(id);
return ResponseEntity.ok(new ResponseWrapper<>("success", "Ticket deleted successfully."));
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ResponseWrapper<>("error", "No ticket with that ID was found."));
}
}
}