Ejemplo n.º 1
0
        public IActionResult DeleteMovie(int?id)
        {
            var movie = movies.FindById((int)id);

            if (movie == null)
            {
                return(NotFound());
            }

            movies.DeleteMovie(movie.Id);

            TempData["message"] = "Se ha eliminado película correctamente";
            return(RedirectToAction("Index"));
        }
        private static void ReceiveCallback(IAsyncResult ar)
        {
            Socket current = (Socket)ar.AsyncState;
            int    received;

            try
            {
                received = current.EndReceive(ar);
            }
            catch (SocketException)
            {
                Console.WriteLine("Client forcefully disconnected");
                current.Close();
                ClientSockets.Remove(current);
                return;
            }

            byte[] recBuf = new byte[received];
            Array.Copy(Buffer, recBuf, received);

            string request = Encoding.ASCII.GetString(recBuf);

            Console.WriteLine("Received Request: " + request);

            var dataArray   = request.Split(';');
            var instruction = dataArray[0];

            switch (instruction)
            {
            case "login":
            {
                var data = _loginManager.GetLoggedInUserRole(dataArray);
                current.Send(data);
                break;
            }

            case "getMovies":
            {
                var movies = _movieManager.GetMovies();
                var jsonMoviesSerialized = JsonConvert.SerializeObject(movies, new JsonSerializerSettings {
                        TypeNameHandling = TypeNameHandling.Objects
                    });

                byte[] data = Encoding.ASCII.GetBytes(jsonMoviesSerialized);

                current.Send(data);
                break;
            }

            case "getUsers":
            {
                var users = _userManager.GetUsers();
                var jsonUsersSerialized = JsonConvert.SerializeObject(users, new JsonSerializerSettings {
                        TypeNameHandling = TypeNameHandling.Objects
                    });

                byte[] data = Encoding.ASCII.GetBytes(jsonUsersSerialized);

                current.Send(data);
                break;
            }

            case "addMovie":
            {
                var newMovie = JsonConvert.DeserializeObject <Movie>(dataArray[1],
                                                                     new JsonSerializerSettings {
                        TypeNameHandling = TypeNameHandling.Objects
                    });

                _movieManager.AddMovie(newMovie);

                byte[] data = Encoding.ASCII.GetBytes("Ok");

                current.Send(data);
                break;
            }

            case "addUser":
            {
                var newUser = JsonConvert.DeserializeObject <User>(dataArray[1],
                                                                   new JsonSerializerSettings {
                        TypeNameHandling = TypeNameHandling.Objects
                    });

                _userManager.AddUser(newUser);

                byte[] data = Encoding.ASCII.GetBytes("Ok");

                current.Send(data);
                break;
            }

            case "deleteMovie":
            {
                int.TryParse(dataArray[1], out int movieId);

                _movieManager.DeleteMovie(movieId);

                byte[] data = Encoding.ASCII.GetBytes("Ok");

                current.Send(data);
                break;
            }

            case "deleteUser":
            {
                int.TryParse(dataArray[1], out int userId);

                _userManager.DeleteUser(userId);

                byte[] data = Encoding.ASCII.GetBytes("Ok");

                current.Send(data);
                break;
            }

            case "updateMovie":
            {
                var movie = JsonConvert.DeserializeObject <Movie>(dataArray[1],
                                                                  new JsonSerializerSettings {
                        TypeNameHandling = TypeNameHandling.Objects
                    });

                _movieManager.UpdateMovie(movie);

                byte[] data = Encoding.ASCII.GetBytes("Ok");

                current.Send(data);
                break;
            }

            case "updateUser":
            {
                var user = JsonConvert.DeserializeObject <User>(dataArray[1],
                                                                new JsonSerializerSettings {
                        TypeNameHandling = TypeNameHandling.Objects
                    });

                _userManager.UpdateUser(user);

                byte[] data = Encoding.ASCII.GetBytes("Ok");

                current.Send(data);
                break;
            }

            case "addScoreToMovie":
            {
                var selectedMovie = JsonConvert.DeserializeObject <Movie>(dataArray[1],
                                                                          new JsonSerializerSettings {
                        TypeNameHandling = TypeNameHandling.Objects
                    });

                int.TryParse(dataArray[2], out int userId);
                int.TryParse(dataArray[3], out int scoreValue);

                _scoreManager.AddScore(userId, selectedMovie.Id, scoreValue);

                var calculatedScore = _scoreManager.GetMovieScore(selectedMovie.Id);


                _movieManager.SaveRating(selectedMovie, calculatedScore);

                byte[] data = Encoding.ASCII.GetBytes("Ok");

                current.Send(data);

                break;
            }

            case "getScore":
            {
                int.TryParse(dataArray[1], out int movieId);
                int.TryParse(dataArray[2], out int userId);

                var score = _scoreManager.GetScore(movieId, userId);
                var jsonScoreSerialized = JsonConvert.SerializeObject(score, new JsonSerializerSettings {
                        TypeNameHandling = TypeNameHandling.Objects
                    });

                byte[] data = Encoding.ASCII.GetBytes(jsonScoreSerialized);

                current.Send(data);

                break;
            }

            case "getFavoriteMovies":
            {
                int.TryParse(dataArray[1], out int userId);

                var favorites = _favoritesManager.GetFavorites(userId);
                var jsonFavoritesSerialized = JsonConvert.SerializeObject(favorites, new JsonSerializerSettings {
                        TypeNameHandling = TypeNameHandling.Objects
                    });

                byte[] data = Encoding.ASCII.GetBytes(jsonFavoritesSerialized);

                current.Send(data);
                break;
            }

            case "addFavoriteMovie":
            {
                var favorite = JsonConvert.DeserializeObject <Favorite>(dataArray[1],
                                                                        new JsonSerializerSettings {
                        TypeNameHandling = TypeNameHandling.Objects
                    });

                _favoritesManager.AddFavorite(favorite);

                byte[] data = Encoding.ASCII.GetBytes("Ok");

                current.Send(data);
                break;
            }

            case "removeFavoriteMovie":
            {
                int.TryParse(dataArray[1], out int favoriteMovieId);

                _favoritesManager.RemoveFavorite(favoriteMovieId);

                byte[] data = Encoding.ASCII.GetBytes("Ok");

                current.Send(data);
                break;
            }

            case "getMovieReviews":
            {
                int.TryParse(dataArray[1], out int movieId);

                var reviews = _reviewsManager.GetReviews(movieId);
                var jsonReviewsSerialized = JsonConvert.SerializeObject(reviews, new JsonSerializerSettings {
                        TypeNameHandling = TypeNameHandling.Objects
                    });

                byte[] data = Encoding.ASCII.GetBytes(jsonReviewsSerialized);

                current.Send(data);
                break;
            }

            case "addMovieReview":
            {
                var review = JsonConvert.DeserializeObject <Review>(dataArray[1],
                                                                    new JsonSerializerSettings {
                        TypeNameHandling = TypeNameHandling.Objects
                    });

                _reviewsManager.AddReview(review);

                byte[] data = Encoding.ASCII.GetBytes("Ok");

                current.Send(data);
                break;
            }

            case "AddReviewThumb":
            {
                var thumb = JsonConvert.DeserializeObject <Thumb>(dataArray[1],
                                                                  new JsonSerializerSettings {
                        TypeNameHandling = TypeNameHandling.Objects
                    });

                _reviewsManager.AddThumb(thumb);

                byte[] data = Encoding.ASCII.GetBytes("Ok");

                current.Send(data);
                break;
            }

            case "getReviewByMovieIdUserId":
            {
                int.TryParse(dataArray[1], out int movieId);
                int.TryParse(dataArray[2], out int userId);

                var review = _reviewsManager.GetReviewByMovieIdUserId(movieId, userId);
                var jsonReviewSerialized = JsonConvert.SerializeObject(review, new JsonSerializerSettings {
                        TypeNameHandling = TypeNameHandling.Objects
                    });

                byte[] data = Encoding.ASCII.GetBytes(jsonReviewSerialized);

                current.Send(data);
                break;
            }

            case "editMovieReview":
            {
                var review = JsonConvert.DeserializeObject <Review>(dataArray[1],
                                                                    new JsonSerializerSettings {
                        TypeNameHandling = TypeNameHandling.Objects
                    });

                _reviewsManager.UpdateReview(review);

                byte[] data = Encoding.ASCII.GetBytes("Ok");

                current.Send(data);
                break;
            }

            case "getThumb":
            {
                int.TryParse(dataArray[1], out int reviewId);
                int.TryParse(dataArray[2], out int userId);

                var thumb = _reviewsManager.GetThumbByReviewIdUserId(reviewId, userId, dataArray[3]);
                var jsonThumbSerialized = JsonConvert.SerializeObject(thumb, new JsonSerializerSettings {
                        TypeNameHandling = TypeNameHandling.Objects
                    });

                byte[] data = Encoding.ASCII.GetBytes(jsonThumbSerialized);

                current.Send(data);
                break;
            }

            case "DeleteReviewThumb":
            {
                int.TryParse(dataArray[1], out int thumbId);

                _reviewsManager.DeleteReviewThumb(thumbId);

                byte[] data = Encoding.ASCII.GetBytes("Ok");

                current.Send(data);
                break;
            }

            case "getWatchedMovies":
            {
                int.TryParse(dataArray[1], out int userId);

                var watchedMovies = _watchedManager.GetWatchedMovies(userId);
                var jsonWatchedMoviesSerialized = JsonConvert.SerializeObject(watchedMovies, new JsonSerializerSettings {
                        TypeNameHandling = TypeNameHandling.Objects
                    });

                byte[] data = Encoding.ASCII.GetBytes(jsonWatchedMoviesSerialized);

                current.Send(data);
                break;
            }

            case "addWatchedMovie":
            {
                var watchedMovie = JsonConvert.DeserializeObject <WatchedMovie>(dataArray[1],
                                                                                new JsonSerializerSettings {
                        TypeNameHandling = TypeNameHandling.Objects
                    });

                _watchedManager.AddWatched(watchedMovie);

                byte[] data = Encoding.ASCII.GetBytes("Ok");

                current.Send(data);
                break;
            }

            case "removeWatchedMovie":
            {
                int.TryParse(dataArray[1], out int watchedMovieId);

                _watchedManager.RemoveWatched(watchedMovieId);

                byte[] data = Encoding.ASCII.GetBytes("Ok");

                current.Send(data);
                break;
            }

            case "getPersonalizedMovies":
            {
                var watchedMovies = JsonConvert.DeserializeObject <List <WatchedMovie> >(dataArray[1],
                                                                                         new JsonSerializerSettings {
                        TypeNameHandling = TypeNameHandling.Objects
                    });

                var personalizedMovies = _movieManager.GetPersonalizedMovies(watchedMovies);

                var jsonPersonalizedMoviesSerialized = JsonConvert.SerializeObject(personalizedMovies, new JsonSerializerSettings {
                        TypeNameHandling = TypeNameHandling.Objects
                    });

                byte[] data = Encoding.ASCII.GetBytes(jsonPersonalizedMoviesSerialized);

                current.Send(data);
                break;
            }
            }

            current.BeginReceive(Buffer, 0, BUFFER_SIZE, SocketFlags.None, ReceiveCallback, current);
        }