1 | package com.wediscussmovies.project.web.controller.rest;
|
---|
2 |
|
---|
3 | import com.wediscussmovies.project.LoggedUser;
|
---|
4 | import com.wediscussmovies.project.ajaxmodels.Grade;
|
---|
5 | import com.wediscussmovies.project.service.MovieService;
|
---|
6 | import org.springframework.http.ResponseEntity;
|
---|
7 | import org.springframework.web.bind.annotation.*;
|
---|
8 |
|
---|
9 | @RestController
|
---|
10 | @RequestMapping("/api/movies")
|
---|
11 | public class MovieRestController {
|
---|
12 |
|
---|
13 | private final MovieService movieService;
|
---|
14 |
|
---|
15 |
|
---|
16 | public MovieRestController(MovieService movieService) {
|
---|
17 | this.movieService = movieService;
|
---|
18 | }
|
---|
19 |
|
---|
20 | @DeleteMapping("/delete/{id}")
|
---|
21 | public ResponseEntity deleteById(@PathVariable Integer id){
|
---|
22 | try {
|
---|
23 | this.movieService.deleteById(id);
|
---|
24 | return ResponseEntity.ok(true);
|
---|
25 | }
|
---|
26 | catch (RuntimeException exception){
|
---|
27 | return ResponseEntity.ok(false);
|
---|
28 |
|
---|
29 | }
|
---|
30 | }
|
---|
31 | @GetMapping("/like/{movieId}")
|
---|
32 | public ResponseEntity likeMovie(@PathVariable Integer movieId, @RequestParam Integer userId){
|
---|
33 | try {
|
---|
34 | this.movieService.likeMovie(movieId,userId);
|
---|
35 | return ResponseEntity.ok(true);
|
---|
36 | }
|
---|
37 | catch (RuntimeException exception){
|
---|
38 | return ResponseEntity.ok(false);
|
---|
39 | }
|
---|
40 | }
|
---|
41 | @GetMapping("/unlike/{movieId}")
|
---|
42 | public ResponseEntity unlikeMovie(@PathVariable Integer movieId, @RequestParam Integer userId){
|
---|
43 | try {
|
---|
44 | this.movieService.unlikeMovie(movieId,userId);
|
---|
45 | return ResponseEntity.ok(true);
|
---|
46 | }
|
---|
47 | catch (RuntimeException exception){
|
---|
48 | return ResponseEntity.ok(false);
|
---|
49 | }
|
---|
50 | }
|
---|
51 | @PostMapping("/grade/{movieId}")
|
---|
52 | public ResponseEntity addGrade(@PathVariable Integer movieId, @RequestBody Grade grade){
|
---|
53 | try {
|
---|
54 | if(grade.getRating() < 5)
|
---|
55 | grade.setRating(5);
|
---|
56 | else if(grade.getRating()>10)
|
---|
57 | grade.setRating(10);
|
---|
58 | this.movieService.addGradeMovie(movieId, LoggedUser.getLoggedUser(),grade);
|
---|
59 | return ResponseEntity.ok(true);
|
---|
60 | }
|
---|
61 | catch (RuntimeException exception){
|
---|
62 | return ResponseEntity.ok(false);
|
---|
63 | }
|
---|
64 |
|
---|
65 | }
|
---|
66 |
|
---|
67 | }
|
---|