-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathScreeningController.java
More file actions
43 lines (35 loc) · 1.53 KB
/
ScreeningController.java
File metadata and controls
43 lines (35 loc) · 1.53 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
package com.booleanuk.api.cinema.screening;
import com.booleanuk.api.cinema.movie.Movie;
import com.booleanuk.api.cinema.movie.MovieRepository;
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.time.LocalDateTime;
import java.util.List;
@RestController
@RequestMapping("/movies/{id}/screenings")
public class ScreeningController {
@Autowired
private ScreeningRepository screeningRepository;
@Autowired
private MovieRepository movieRepository;
@GetMapping
public List<Screening> getAllScreenings(@PathVariable int id) {
Movie movie = this.movieRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found")
);
return movie.getScreenings();
}
@PostMapping
public ResponseEntity<Screening> create(@PathVariable int id, @RequestBody Screening screening) {
Movie movie = this.movieRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found")
);
screening.setMovie(movie);
screening.setCreated_at(String.valueOf(LocalDateTime.now()));
screening.setStarts_at(String.valueOf(LocalDateTime.now()));
return new ResponseEntity<Screening>(this.screeningRepository.save(screening), HttpStatus.CREATED);
}
}