package net.toracode.moviedb.controllers.api;

import net.toracode.moviedb.entities.Movie;
import net.toracode.moviedb.entities.Review;
import net.toracode.moviedb.entities.User;
import net.toracode.moviedb.services.MovieService;
import net.toracode.moviedb.services.ReviewService;
import net.toracode.moviedb.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by sayemkcn on 10/25/16.
 */
@RestController
@RequestMapping(value = "/api/review")
public class ReviewApiController {
    @Autowired
    private ReviewService reviewService;
    @Autowired
    private UserService userService;
    @Autowired
    private MovieService movieService;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ResponseEntity<Review> createReview(@ModelAttribute Review review, BindingResult bindingResult, @RequestParam("accountId") String accountId, @RequestParam("movieId") Long movieId) {
        User user = this.userService.getUserByAccountId(accountId);
        Movie movie = this.movieService.getMovie(movieId);
        // check if this fucking user has already a review in this movie
        // if true then restrict the fuck him from submitting another fucking review
        List<Review> reviewList = this.reviewService.getReviewByMovie(movie);
        if (reviewList!= null){
            for (Review r : reviewList){
                if (r.getUser().getAccountId().equals(accountId))
                    return new ResponseEntity<Review>(HttpStatus.LOCKED);
            }
        }

        // end
        if (user == null || movie == null)
            return new ResponseEntity<Review>(HttpStatus.NOT_ACCEPTABLE);
        review.setUser(user);
        review.setMovie(movie);
        review = this.reviewService.saveReview(review);
        return new ResponseEntity<Review>(review,HttpStatus.CREATED);
    }

}

@@ -321,24 +321,71 @@
 serService;%0A
+import org.apache.catalina.connector.Response;%0A
 import org.s
@@ -435,16 +435,16 @@
 owired;%0A
-
 import o
@@ -1602,16 +1602,17 @@
 List
+ 
 != null)
 %7B%0A  
@@ -1607,16 +1607,17 @@
 != null)
+ 
 %7B%0A      
@@ -1649,16 +1649,17 @@
 iewList)
+ 
 %7B%0A      
@@ -2067,16 +2067,16 @@
 eview);%0A
-
         
@@ -2116,16 +2116,1021 @@
 (review,
+ HttpStatus.CREATED);%0A    %7D%0A%0A    @RequestMapping(value = %22/update/%7BreviewId%7D%22, method = RequestMethod.POST)%0A    public ResponseEntity%3CReview%3E updateReview(@ModelAttribute Review review, BindingResult bindingResult, @PathVariable(%22reviewId%22) Long reviewId, @RequestParam(%22accountId%22) String accountId, @RequestParam(%22movieId%22) Long movieId) %7B%0A        if (bindingResult.hasErrors())%0A            return new ResponseEntity%3CReview%3E(HttpStatus.BAD_REQUEST);%0A        review.setUniqueId(reviewId);%0A        User user = this.userService.getUserByAccountId(accountId);%0A        if (user == null) %7B%0A            return new ResponseEntity%3CReview%3E(HttpStatus.FORBIDDEN);%0A        %7D%0A        review.setUser(user);%0A        Movie movie = this.movieService.getMovie(movieId);%0A        if (movie == null) %7B%0A            return new ResponseEntity%3CReview%3E(HttpStatus.NOT_ACCEPTABLE);%0A        %7D%0A        review.setMovie(movie);%0A        review = this.reviewService.saveReview(review);%0A        return new ResponseEntity%3CReview%3E(review, 
 HttpStat

update review