Exemplo n.º 1
0
        public Boolean DeleteComment(Comment deletedComment)
        {
            using (var ctx = new MovieNetModelContainer())
            {
                Comment commentToDelete = FindCommentById(deletedComment.Id);

                if (commentToDelete != null)
                {
                    ctx.CommentSet.Attach(commentToDelete);
                    ctx.CommentSet.Remove(commentToDelete);

                    try
                    {
                        ctx.SaveChanges();
                        return(true);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Error deleting Comment.");
                        Console.WriteLine(e.Message);
                        return(false);
                    }
                }
                else
                {
                    Console.WriteLine("Couldn't find Comment to delete with ID " + deletedComment.Id);
                    return(false);
                }
            }
        }
Exemplo n.º 2
0
        public User UpdateUser(User updatedUser)
        {
            using (var ctx = new MovieNetModelContainer())
            {
                User userToUpdate = FindUserByIdPrivate(updatedUser.Id, ctx);

                if (userToUpdate != null)
                {
                    userToUpdate.Username = updatedUser.Username;
                    userToUpdate.Password = updatedUser.Password;

                    try
                    {
                        ctx.SaveChanges();
                        return(updatedUser);
                    }
                    catch
                    {
                        Console.WriteLine("Error updating user.");
                        return(null);
                    }
                }
                else
                {
                    Console.WriteLine("Couldn't find User to update with ID " + updatedUser.Id);
                    return(null);
                }
            }
        }
Exemplo n.º 3
0
        public Comment UpdateComment(Comment updatedComment)
        {
            using (var ctx = new MovieNetModelContainer())
            {
                Comment CommentToUpdate = FindCommentByIdPrivate(updatedComment.Id, ctx);
                updatedComment.ModificationDate = DateTime.Now;
                CommentToUpdate.Score           = updatedComment.Score;
                CommentToUpdate.Text            = updatedComment.Text;
                CommentToUpdate.User            = updatedComment.User;
                CommentToUpdate.Film            = updatedComment.Film;
                CommentToUpdate.CreationDate    = updatedComment.CreationDate;

                if (CommentToUpdate != null)
                {
                    CommentToUpdate = updatedComment;

                    try
                    {
                        ctx.SaveChanges();
                    }
                    catch
                    {
                        Console.WriteLine("Error updating Comment.");
                        CommentToUpdate = null;
                    }
                }
                else
                {
                    Console.WriteLine("Couldn't find Comment to update with ID " + updatedComment.Id);
                    CommentToUpdate = null;
                }
                return(CommentToUpdate);
            }
        }
Exemplo n.º 4
0
        // Methods with one return. It is advised to make only one return on the method.
        public Film UpdateFilm(Film film)
        {
            using (var ctx = new MovieNetModelContainer())
            {
                Film filmToUpdate = FindFilmByIdPrivate(film.Id, ctx);
                filmToUpdate.Id       = film.Id;
                filmToUpdate.Title    = film.Title;
                filmToUpdate.Genres   = film.Genres;
                filmToUpdate.Synopsis = film.Synopsis;
                filmToUpdate.Score    = film.Score;
                filmToUpdate.Year     = film.Year;
                filmToUpdate.Director = film.Director;

                if (filmToUpdate != null)
                {
                    try
                    {
                        //ctx.Entry(filmToUpdate).State = EntityState.Modified;
                        int num = ctx.SaveChanges();
                        Console.WriteLine(num);
                    }
                    catch
                    {
                        Console.WriteLine("[FILM DAO] Error updating.");
                        filmToUpdate = null;
                    }
                }
                else
                {
                    filmToUpdate = null;
                    Console.WriteLine("Couldn't update the film " + film.Title);
                }
                return(filmToUpdate);
            }
        }
Exemplo n.º 5
0
        public Boolean DeleteUser(User deletedUser)
        {
            using (var ctx = new MovieNetModelContainer())
            {
                User userToDelete = FindUserById(deletedUser.Id);

                if (userToDelete != null)
                {
                    ctx.UserSet.Attach(userToDelete);
                    ctx.UserSet.Remove(userToDelete);

                    try
                    {
                        ctx.SaveChanges();
                        return(true);
                    }
                    catch
                    {
                        Console.WriteLine("Error deleting user.");
                        return(false);
                    }
                }
                else
                {
                    Console.WriteLine("Couldn't find User to delete with ID " + deletedUser.Id);
                    return(false);
                }
            }
        }
Exemplo n.º 6
0
        public User UserValidation(String username, String password)
        {
            User user = null;

            using (var ctx = new MovieNetModelContainer())
                try
                {
                    user = ctx.UserSet.Where(u => u.Username == username && u.Password == password).Single();
                }
                catch (InvalidOperationException)
                {
                    Console.WriteLine("Context problems");
                }
            return(user);
        }
Exemplo n.º 7
0
 public List <Film> FindFilmsByTitle(string title)
 {
     using (var ctx = new MovieNetModelContainer())
     {
         List <Film> filmsByTitle = new List <Film>();
         try
         {
             filmsByTitle = ctx.FilmSet.Where(f => f.Title.Contains(title)).ToList();
         }
         catch (ArgumentNullException)
         {
             Console.WriteLine("[FILM DAO] Nothing Found under title :" + title);
         }
         return(filmsByTitle);
     }
 }
Exemplo n.º 8
0
        // Find All
        public List <Film> FindAllFilms()
        {
            using (var ctx = new MovieNetModelContainer())
            {
                List <Film> allFilms = new List <Film>();

                try
                {
                    allFilms = ctx.FilmSet.ToList();
                }
                catch (ArgumentNullException)
                {
                    Console.WriteLine("[FILM DAO] Nothing found.");
                }
                return(allFilms);
            }
        }
Exemplo n.º 9
0
        // Find All
        public List <User> FindAllUsers()
        {
            using (var ctx = new MovieNetModelContainer())
            {
                List <User> allUsers = new List <User>();

                try
                {
                    allUsers = ctx.UserSet.ToList();
                }
                catch (ArgumentNullException)
                {
                    Console.WriteLine("No user found.");
                }

                return(allUsers);
            }
        }
Exemplo n.º 10
0
        private Film FindFilmByIdPrivate(int id, MovieNetModelContainer ctx)
        {
            Film filmById = null;

            try
            {
                filmById = ctx.FilmSet.Where(f => f.Id.Equals(id)).Single();
            }
            catch (ArgumentNullException)
            {
                Console.WriteLine("[FILM DAO] Nothing Found under id:" + id);
            }
            catch (InvalidOperationException)
            {
                Console.WriteLine("[FILM DAO] Film not found");
            }
            return(filmById);
        }
Exemplo n.º 11
0
        // CRUD
        public Film CreateFilm(Film film)
        {
            using (var ctx = new MovieNetModelContainer())
            {
                ctx.FilmSet.Add(film);

                try
                {
                    ctx.SaveChanges();
                }
                catch
                {
                    Console.WriteLine("[FILM DAO] Error executing query");
                    return(null);
                }
                return(film);
            }
        }
Exemplo n.º 12
0
        // ...

        // CRUD
        public User CreateUser(User createdUser)
        {
            using (var ctx = new MovieNetModelContainer())
            {
                ctx.UserSet.Add(createdUser);

                try
                {
                    ctx.SaveChanges();
                }
                catch
                {
                    Console.WriteLine("Error creating user.");
                    return(null);
                }

                return(createdUser);
            }
        }
Exemplo n.º 13
0
        private Comment FindCommentByIdPrivate(int id, MovieNetModelContainer ctx)
        {
            Comment CommentById = null;

            try
            {
                CommentById = ctx.CommentSet.Where(c => c.Id == id).Single();
            }
            catch (InvalidOperationException)
            {
                Console.WriteLine("Error executing query.");
            }
            catch (ArgumentNullException)
            {
                Console.WriteLine("Error: could not find Comment with id " + id + ".");
            }

            return(CommentById);
        }
Exemplo n.º 14
0
        private User FindUserByIdPrivate(int id, MovieNetModelContainer ctx)
        {
            User userById = null;

            try
            {
                userById = ctx.UserSet.Where(u => u.Id == id).Single();
            }
            catch (ArgumentNullException)
            {
                Console.WriteLine("No user found.");
            }
            catch (InvalidOperationException)
            {
                Console.WriteLine("Error excuting the query");
            }

            return(userById);
        }
Exemplo n.º 15
0
        // ...

        // CRUD
        public Comment CreateComment(Comment createdComment)
        {
            using (var ctx = new MovieNetModelContainer())
            {
                ctx.CommentSet.Add(createdComment);

                try
                {
                    ctx.SaveChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error creating Comment.");
                    Console.WriteLine(e.Source);
                    return(null);
                }

                return(createdComment);
            }
        }
Exemplo n.º 16
0
        public List <Comment> FindCommentsByFilmId(int filmId)
        {
            using (var ctx = new MovieNetModelContainer())
            {
                List <Comment> CommentsByFilmId = new List <Comment>();

                try
                {
                    CommentsByFilmId = ctx.CommentSet.Where(c => c.Film.Id == filmId).ToList();
                }
                catch (ArgumentNullException)
                {
                    Console.WriteLine("Error executing query.");
                }
                catch (InvalidOperationException)
                {
                    Console.WriteLine("Error: could not find Comments with FilmId " + filmId + ".");
                }

                return(CommentsByFilmId);
            }
        }
Exemplo n.º 17
0
        // Find All
        public List <Comment> FindAllComments()
        {
            using (var ctx = new MovieNetModelContainer())
            {
                List <Comment> allComments = new List <Comment>();

                try
                {
                    allComments = ctx.CommentSet.ToList();
                }
                catch (ArgumentNullException)
                {
                    Console.WriteLine("Could not found any comment");
                }
                catch (InvalidOperationException)
                {
                    Console.WriteLine("Error executing query.");
                }

                return(allComments);
            }
        }
Exemplo n.º 18
0
        // Find by Criteria
        public User FindUserByUsername(string username)
        {
            using (var ctx = new MovieNetModelContainer())
            {
                User userByUsername = null;

                try
                {
                    userByUsername = ctx.UserSet.Where(u => u.Username == username).Single();
                }
                catch (InvalidOperationException)
                {
                    Console.WriteLine("Error executing query.");
                }
                catch (ArgumentNullException)
                {
                    Console.WriteLine("Error: could not find user with username " + username + ".");
                }

                return(userByUsername);
            }
        }
Exemplo n.º 19
0
        public Boolean DeleteFilm(Film deletedFilm)
        {
            using (var ctx = new MovieNetModelContainer())
            {
                Film filmToDelete = FindFilmByIdPrivate(deletedFilm.Id, ctx);
                filmToDelete.Id       = deletedFilm.Id;
                filmToDelete.Title    = deletedFilm.Title;
                filmToDelete.Genres   = deletedFilm.Genres;
                filmToDelete.Synopsis = deletedFilm.Synopsis;
                filmToDelete.Score    = deletedFilm.Score;
                filmToDelete.Year     = deletedFilm.Year;
                filmToDelete.Director = deletedFilm.Director;
                Boolean isDeleted = false;

                if (filmToDelete != null)
                {
                    //ctx.FilmSet.Attach(filmToDelete);
                    ctx.FilmSet.Remove(filmToDelete);

                    try
                    {
                        ctx.SaveChanges();
                        isDeleted = true;
                    }
                    catch
                    {
                        Console.WriteLine("[FILM DAO] Couldn't delete the film " + filmToDelete.Title);
                    }
                }
                else
                {
                    Console.WriteLine("[FILM DAO] Couldn't find the film " + filmToDelete.Title);
                }

                return(isDeleted);
            }
        }