-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathScreeningController.java
More file actions
54 lines (43 loc) · 1.96 KB
/
ScreeningController.java
File metadata and controls
54 lines (43 loc) · 1.96 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
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.repository.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.time.LocalDateTime;
import java.util.List;
@RestController
@RequestMapping("/movies")
public class ScreeningController {
@Autowired
private MovieRepository movieRepository;
@Autowired
private ScreeningRepository screeningRepository;
@PostMapping("/{id}/screenings")
public ResponseEntity<Screening> createScreening(@PathVariable int id, @RequestBody Screening screeningDetails) {
Movie movie = this.movieRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Movie with ID " + id + " not found.")
);
Screening screening = new Screening(
screeningDetails.getScreenNumber(),
screeningDetails.getCapacity(),
screeningDetails.getStartsAt(),
movie
);
screening.setCreatedAt(LocalDateTime.now());
screening.setUpdatedAt(LocalDateTime.now());
return new ResponseEntity<>(this.screeningRepository.save(screening), HttpStatus.CREATED);
}
@GetMapping("/{id}/screenings")
public ResponseEntity<List<Screening>> getAllScreeningsForMovie(@PathVariable int id) {
List<Screening> screenings = this.screeningRepository.findByMovieId(id);
if (screenings.isEmpty()) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No screenings found for Movie with ID " + id);
}
return ResponseEntity.ok(screenings);
}
}