-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathMovieController.java
More file actions
100 lines (89 loc) · 3.42 KB
/
MovieController.java
File metadata and controls
100 lines (89 loc) · 3.42 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
package com.booleanuk.api.cinema.controllers;
import com.booleanuk.api.cinema.models.Customer;
import com.booleanuk.api.cinema.models.Movie;
import com.booleanuk.api.cinema.models.Screening;
import com.booleanuk.api.cinema.repositories.MovieRepository;
import com.booleanuk.api.cinema.repositories.ScreeningRepository;
import com.booleanuk.api.cinema.responses.MovieResponse;
import com.booleanuk.api.cinema.responses.Response;
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.List;
@RestController
@RequestMapping("movies")
public class MovieController {
@Autowired
private MovieRepository repository;
@Autowired
private ScreeningRepository associatedScreeningRepository;
// TODO: Handle case when the payload is a screening object instead of Id's
@PostMapping
public ResponseEntity<Response<?>> create(@RequestBody Movie movie) {
Movie newMovie = this.repository.save(movie);
// Create corresponding screenings
List<Screening> screenings = movie.getScreenings();
for (Screening s : screenings) {
Screening existingScreening = this.associatedScreeningRepository.findById(s.getId()).orElse(null);
if (existingScreening == null) {
s.setMovie(newMovie);
this.associatedScreeningRepository.save(s);
} else {
existingScreening.setMovie(newMovie);
}
}
// Save to database
this.repository.save(newMovie);
// Update response
MovieResponse response = new MovieResponse();
response.set(newMovie);
return new ResponseEntity<>(response, HttpStatus.CREATED);
}
@GetMapping
public ResponseEntity<List<Movie>> getAll() {
return ResponseEntity.ok(this.repository.findAll());
}
@PutMapping("{id}")
public ResponseEntity<Movie> update(
@PathVariable int id,
@RequestBody Movie movie)
{
Movie originalMovie = getObjectById(id);
movie.setId(id);
movie.setCreatedAt(originalMovie.getCreatedAt());
movie.setScreenings(originalMovie.getScreenings());
return new ResponseEntity<>(this.repository.save(movie), HttpStatus.CREATED);
}
@DeleteMapping("{id}")
public ResponseEntity<Movie> delete(@PathVariable int id) {
Movie movie = getObjectById(id);
try {
this.repository.deleteById(id);
} catch (Exception e) {
throw new ResponseStatusException(
HttpStatus.BAD_REQUEST,
"Could not delete customer. Detailed information: "+e.getMessage()
);
}
return ResponseEntity.ok(movie);
}
/**
* Get object by id.
* Can be used to check for valid id (throws exception if id doesn't exist).
* @param id .
* @return Customer
*/
private Movie getObjectById(int id) {
Movie movie = this.repository
.findById(id)
.orElseThrow(
() -> new ResponseStatusException(
HttpStatus.NOT_FOUND,
"No movie with id #"+id+" found."
)
);
return movie;
}
}