-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathScreeningController.java
More file actions
50 lines (43 loc) · 2.14 KB
/
ScreeningController.java
File metadata and controls
50 lines (43 loc) · 2.14 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
package com.booleanuk.api.cinema.controllers;
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 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/{id}/screenings")
public class ScreeningController {
@Autowired
private ScreeningRepository screeningRepository;
@Autowired
private MovieRepository movieRepository;
@GetMapping
public ResponseEntity<List<Screening>> getAll(@PathVariable int id){
if (!movieRepository.existsById(id)) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Movie not found");
}
List<Screening> screeningsToGet = screeningRepository.findByMovieId(id);
return ResponseEntity.ok(screeningsToGet);
}
@PostMapping
public ResponseEntity<Screening> createScreening(@PathVariable int id, @RequestBody Screening screening){
Movie movie = movieRepository.findById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Movie not found"));
screening.setMovie(movie);
return new ResponseEntity<Screening>(this.screeningRepository.save(screening), HttpStatus.CREATED);
}
// @PutMapping("{id}")
// public ResponseEntity<Screening> updateScreening(@PathVariable int id, @RequestBody Screening screening){
// Screening screeningToUpdate = this.repository.findById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found"));
//
// screeningToUpdate.setScreenNumber(screening.getScreenNumber());
// screeningToUpdate.setCapacity(screening.getCapacity());
// screeningToUpdate.setStartsAt(screening.getStartsAt());
//
// return new ResponseEntity<>(this.repository.save(screeningToUpdate), HttpStatus.CREATED);
// }
}