-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathMovieController.java
More file actions
102 lines (87 loc) · 4.65 KB
/
MovieController.java
File metadata and controls
102 lines (87 loc) · 4.65 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
package com.booleanuk.api.cinema.Controller;
import com.booleanuk.api.cinema.Model.Movie;
import com.booleanuk.api.cinema.Model.Screening;
import com.booleanuk.api.cinema.Repository.MovieRepository;
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.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
@RestController
@RequestMapping("movies")
public class MovieController {
@Autowired
private MovieRepository movieRepository;
@ResponseStatus(HttpStatus.CREATED)
@PostMapping
public ResponseEntity<ResponseWrapper<Object>> create(@RequestBody Movie newMovie) {
try {
LocalDateTime currentDateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
newMovie.setCreatedAt(LocalDateTime.parse(currentDateTime.format(formatter)));
newMovie.setUpdatedAt(LocalDateTime.parse(currentDateTime.format(formatter)));
newMovie.setScreenings(new ArrayList<Screening>());
Movie savedMovie = this.movieRepository.save(newMovie);
Map<String, Object> response = new LinkedHashMap<>();
response.put("id", savedMovie.getId());
response.put("title", newMovie.getTitle());
response.put("description", newMovie.getDescription());
response.put("runtimeMins", newMovie.getRuntimeMins());
response.put("createdAt", currentDateTime.format(formatter));
response.put("updatedAt", currentDateTime.format(formatter));
return ResponseEntity.status(HttpStatus.CREATED).body(new ResponseWrapper<>("success", response));
} catch (Exception e) {
//System.out.println(e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseWrapper<>("error", "Could not create movie, please check all required fields are correct."));
}
}
@ResponseStatus(HttpStatus.OK)
@GetMapping
public ResponseWrapper getAll() {
return new ResponseWrapper<>("success", this.movieRepository.findAll());
}
@ResponseStatus(HttpStatus.CREATED)
@PutMapping("{id}")
public ResponseEntity<ResponseWrapper<Object>> update(@PathVariable("id") Integer id, @RequestBody Movie updatedMovie) {
Optional<Movie> existingMovieOptional = this.movieRepository.findById(id);
LocalDateTime currentDateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
if (existingMovieOptional.isPresent()) {
try {
Movie existingMovie = existingMovieOptional.get();
existingMovie.setTitle(updatedMovie.getTitle());
existingMovie.setRating(updatedMovie.getRating());
existingMovie.setDescription(updatedMovie.getDescription());
existingMovie.setRuntimeMins(updatedMovie.getRuntimeMins());
existingMovie.setUpdatedAt(LocalDateTime.parse(currentDateTime.format(formatter)));
Movie savedMovie = this.movieRepository.save(existingMovie);
return ResponseEntity.status(HttpStatus.CREATED).body(new ResponseWrapper<>("success", savedMovie));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseWrapper<>("error", "Could not update movie, please check all fields are correct."));
}
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ResponseWrapper<>("error", "No movie with that id found."));
//return new ResponseWrapper<>("error", "No movie with that id found.");
}
}
@ResponseStatus(HttpStatus.OK)
@DeleteMapping("{id}")
public Optional<Movie> delete(@PathVariable("id") Integer id) {
Optional<Movie> movieOptional = this.movieRepository.findById(id);
if (movieOptional.isPresent()) {
Movie deletedMovie = movieOptional.get();
this.movieRepository.deleteById(id);
// return ResponseEntity.ok(new ResponseWrapper<>("success", deletedMovie));
return movieOptional;
} else {
return Optional.empty();
// ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ResponseWrapper<>("error", "No movie with that id found."));
}
}
}