コード例 #1
0
        /// <summary>
        /// Adds information if movie is added to favourites, hated or none of them
        /// </summary>
        /// <param name="whichcase">Determines if movie is added to favourites or hated</param>
        /// <param name="UserName">The name of the user that is log in</param>
        /// <param name="film">The name of file that we want to set</param>
        public static void AddFavouriteOrHated(int whichcase, string UserName, string film)
        {
            try
            {
                MovieDatabaseEntities db = new MovieDatabaseEntities();

                var result = db.MainMovieLists.SingleOrDefault(o => o.File_Name == film);

                var usr = db.Users.SingleOrDefault(o => o.Username == UserName);

                int ID = usr.UserID;

                var search = db.Favourite_Hated.SingleOrDefault(o => o.FilmID == result.ID && o.UserID == ID);

                if (search == null)
                {
                    Favourite_Hated favourite = new Favourite_Hated();

                    favourite.FilmID = result.ID;
                    favourite.UserID = usr.UserID;
                    favourite.LikeOrDislike = whichcase;

                    db.Favourite_Hated.Add(favourite);
                    db.SaveChanges();
                }
                else if (search.LikeOrDislike != whichcase)
                {
                    search.LikeOrDislike = whichcase;
                    db.SaveChanges();
                }
            }
            catch (DbEntityValidationException e)
            { //In case of error while adding stuff to database
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }
        }
コード例 #2
0
        /// <summary>
        /// Adds information if movie is added to favourites, hated or none of them
        /// </summary>
        /// <param name="whichcase">Determines if movie is added to favourites or hated</param>
        /// <param name="UserName">The name of the user that is log in</param>
        /// <param name="film">The name of file that we want to set</param>
        public static void AddFavouriteOrHated(int whichcase, string UserName, string film)
        {
            try
            {
                MovieDatabaseEntities db = new MovieDatabaseEntities();

                var result = db.MainMovieLists.SingleOrDefault(o => o.File_Name == film);

                var usr = db.Users.SingleOrDefault(o => o.Username == UserName);

                int ID = usr.UserID;

                var search = db.Favourite_Hated.SingleOrDefault(o => o.FilmID == result.ID && o.UserID == ID);

                if (search == null)
                {
                    Favourite_Hated favourite = new Favourite_Hated();

                    favourite.FilmID        = result.ID;
                    favourite.UserID        = usr.UserID;
                    favourite.LikeOrDislike = whichcase;

                    db.Favourite_Hated.Add(favourite);
                    db.SaveChanges();
                }
                else if (search.LikeOrDislike != whichcase)
                {
                    search.LikeOrDislike = whichcase;
                    db.SaveChanges();
                }
            }
            catch (DbEntityValidationException e)
            { //In case of error while adding stuff to database
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }
        }