예제 #1
0
        public bool AuthenticateUser() // This is a method that checks if user is logged in. It will return TRUE and set a ViewBag if user is logged in.
        {                              // then it will see if user is administrator and set a Viewbag.IsAdmin to true if he is.
            var id = User.Identity.GetUserId();

            if (id != null)
            {                           // If user is registered show him 'profile' and 'logout' options
                ViewBag.IsRegistered = true;
                using (var context = new MovieReviewsDbContext())
                {
                    if (UserManager == null)
                    {
                        UserStore <ApplicationUser> userStore =
                            new UserStore <ApplicationUser>(new MovieReviewsDbContext());
                        UserManager = new UserManager <ApplicationUser>(userStore);
                    }
                    if (UserManager.IsInRole(id, "Administrator"))
                    {
                        ViewBag.IsAdmin = true;
                    }
                    if (UserManager.IsInRole(id, "Guest"))
                    {
                        ViewBag.IsGuest = true;
                    }
                }
                return(true); // if user is registered
            }
            return(false);    // if user is not registered
        }
예제 #2
0
 public ActionResult ShowMovies()
 {
     AuthenticateUser();
     using (var context = new MovieReviewsDbContext())
     {
         var movies = (from movie in context.Movies
                       select movie).ToList();
         return(View(movies));
     }
 }
예제 #3
0
        public ActionResult ShowUsers()
        {
            AuthenticateUser();
            using (var context = new MovieReviewsDbContext())
            {
                context.Configuration.LazyLoadingEnabled = false; // turn off lazy load and eager load the roles

                var movies = (from movie in context.Users.Include(i => i.Roles)
                              select movie).ToList();
                return(View(movies));
            }
        }
예제 #4
0
        public ActionResult DeleteUser(string id)
        {
            using (var context = new MovieReviewsDbContext())
            { // find and delete user
                var user = (from usr in context.Users
                            where usr.Id == id
                            select usr).SingleOrDefault();


                UserManager.Delete(user); // done
            }
            return(RedirectToAction("Index"));
        }
예제 #5
0
 public ActionResult AddMovie(Movie movie)
 {
     if (ModelState.IsValid)
     {
         using (var context = new MovieReviewsDbContext())
         {
             context.Movies.Add(movie);
             context.SaveChanges();
         }
         return(RedirectToAction("Index"));
     }
     return(View(movie));
 }
예제 #6
0
        public ActionResult MyProfile()
        {
            if (!AuthenticateUser())
            {
                RedirectToAction("Index", "Home");                      // if user is not logged in redirect to index
            }
            var matchedId = User.Identity.GetUserId();

            using (MovieReviewsDbContext context = new MovieReviewsDbContext())
            {
                var user = context.Users.Where(obj => obj.Id == matchedId).SingleOrDefault();
                return(View("Profile", user));
            }
        }
예제 #7
0
 public ActionResult Index([Bind(Exclude = "Id")] AddReviewModel userReviewVM, string movie)
 {
     AuthenticateUser();
     if (ModelState.IsValid)
     {
         UserReview userReview = new UserReview();
         using (var context = new MovieReviewsDbContext())
         {
             try
             {
                 var             currentUser = User.Identity.GetUserId();
                 var             movieInDb   = context.Movies.Where(o => o.MovieName == userReviewVM.MovieName).SingleOrDefault();
                 ApplicationUser user        = context.Users.Where(usr => usr.Id == currentUser).SingleOrDefault();
                 userReview.User   = user;
                 userReview.Movie  = movieInDb;
                 userReview.Review = userReviewVM.Review;
                 ModelState.AddModelError("MovieName", "Wrong movie name");
                 if (userReview.Movie == null)
                 {
                     return(View(userReview));
                 }
                 context.UserReviews.Add(userReview);
                 movieInDb.UserReviews.Add(userReview);
                 context.SaveChanges();
             }
             catch (DbEntityValidationException dbEx)
             {
                 foreach (var validationErrors in dbEx.EntityValidationErrors)
                 {
                     foreach (var validationError in validationErrors.ValidationErrors)
                     {
                         Trace.TraceInformation("Property: {0} Error: {1}",
                                                validationError.PropertyName,
                                                validationError.ErrorMessage);
                     }
                 }
             }
             // return Content("<script>alert('Done!')</script>");
             return(RedirectToAction("Movies", "Home"));
         }
     }
     else
     {
         if (movie != null)
         {
             ViewBag.movieName = movie;
         }
         return(View(userReviewVM));
     }
 }
예제 #8
0
 public ActionResult EditMovies(int id)
 {
     AuthenticateUser();
     using (var context = new MovieReviewsDbContext())
     {
         // if movie is not found go to index otherwise proceed
         var getMovie = context.Movies.Where(i => i.MovieId == id).SingleOrDefault();
         if (getMovie == null)
         {
             return(RedirectToAction("Index"));
         }
         return(View(getMovie));
     }
 }
예제 #9
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            // Force create DB
            // MovieReviewsDbContext context = new MovieReviewsDbContext();

            AntiForgeryConfig.SuppressXFrameOptionsHeader = true;
            using (var context = new MovieReviewsDbContext())
            {
                context.Database.Initialize(true);
            }
        }
예제 #10
0
        public PartialViewResult AjaxMovies(string searchString, int?page)  // Use this Ajax method for the search field
        {
            using (var context = new MovieReviewsDbContext())
            {
                var movies = from s in context.Movies // save only the matched movies
                             where s.MovieName.Contains(searchString)
                             select s;
                movies = movies.OrderBy(i => i.MovieId);
                ViewBag.searchString = searchString; // pass the search string to the view ( see bottom of the view )

                int pageSize   = 3;
                int pageNumber = (page ?? 1); // if page is null then value is 1
                return(PartialView(movies.ToPagedList(pageNumber, pageSize)));
            }
        }
예제 #11
0
 public ActionResult AjaxUsers(string searchString)
 {
     using (var context = new MovieReviewsDbContext())
     {
         var user = context.Users.Where(usr => usr.UserName == searchString).Include(usr => usr.Roles).SingleOrDefault();
         if (user != null)
         {
             return(PartialView(user));
         }
         else
         {
             return(JavaScript("<script>alert('Not found')</script>"));
         }
     }
 }
예제 #12
0
 // these are Ajax methods for quick search
 public ActionResult AjaxMovies(string searchString)
 {
     using (var context = new MovieReviewsDbContext())
     {
         var resultMovie = context.Movies.Where(movie => movie.MovieId.ToString() == searchString).SingleOrDefault();
         if (resultMovie != null)
         {
             return(PartialView(resultMovie));
         }
         else
         {
             return(JavaScript("<script>alert('Not found')</script>"));
         }
     }
 }
예제 #13
0
        public ActionResult RemoveAdmin(string id)
        {
            using (var context = new MovieReviewsDbContext())
            { // find and remove role
                var user = (from usr in context.Users
                            where usr.Id == id
                            select usr).SingleOrDefault();

                UserStore <ApplicationUser> userStore =
                    new UserStore <ApplicationUser>(new MovieReviewsDbContext());
                var UserManager = new UserManager <ApplicationUser>(userStore);
                UserManager.RemoveFromRole(id.ToString(), "Administrator");
            }
            return(RedirectToAction("Index"));
        }
예제 #14
0
        public ViewResult Movies(string sortOrder, string searchString, int?page)
        {
            AuthenticateUser();

            ViewBag.CurrentSort = sortOrder; // set the sort

            MovieReviewsDbContext context = new MovieReviewsDbContext();
            var movies = from s in context.Movies
                         select s;

            if (searchString != null) // if this is not null we've been sent here from ajax method
            {
                movies = movies.Where(i => i.MovieName.Contains(searchString));
                movies = movies.OrderBy(i => i.MovieId);
            }
            else
            { // if we are here then this method is called normally. We need to see if the user has any sorting options activated.
                switch (sortOrder)
                {
                case "oldest":
                    movies = movies.OrderBy(s => s.MovieYear);
                    break;

                case "newest":
                    movies = movies.OrderByDescending(s => s.MovieYear);
                    break;

                case "best":
                    movies = movies.OrderByDescending(s => s.Stars);
                    break;

                case "worst":
                    movies = movies.OrderBy(s => s.Stars);
                    break;

                default:      // id
                    movies = movies.OrderBy(s => s.MovieId);
                    break;
                }
            }
            int pageSize   = 3;           // 3 records per page
            int pageNumber = (page ?? 1); // if page is null set it to 1

            return(View(movies.ToPagedList(pageNumber, pageSize)));
        }
예제 #15
0
        public ActionResult Index(string movie)
        {
            //UserReview review = new UserReview();
            //Movie movieInDb;
            AuthenticateUser();
            AddReviewModel userReviewVM = new AddReviewModel();

            using (var context = new MovieReviewsDbContext())
            {
                // movieInDb = context.Movies.Where(mv => mv.MovieName == movie).SingleOrDefault();

                userReviewVM.MovieName = movie;
            }
            if (userReviewVM.MovieName != null)
            {
                return(View(userReviewVM));
            }
            return(Content("<script>alert('Movie not found');</script>"));
        }
예제 #16
0
        public ActionResult Index(int?id)
        {
            if (id == null)
            {
                return(RedirectToAction("Index", "Home"));
            }
            AuthenticateUser();
            Movie movieInDb = null;

            using (var context = new MovieReviewsDbContext())
            {
                movieInDb = context.Movies.Where(obj => obj.MovieId == id).Include((obj => obj.UserReviews.Select(o => o.User))).SingleOrDefault();
            }
            if (movieInDb != null)
            {
                return(View(movieInDb));
            }
            return(Content("<script>alert('Movie not found') </script>"));
        }
예제 #17
0
        public ActionResult EditMovies([Bind(Include = "MovieId,MovieName,MovieYear,PathToImage,Description,Category,Stars,Review,RowVersion")] Movie movie, byte[] rowVersion)
        {
            using (var context = new MovieReviewsDbContext())
            {
                if (ModelState.IsValid)
                {
                    try // Check for optimistic concurrency
                    {
                        context.Entry(movie).State = EntityState.Modified;
                        context.SaveChanges();
                        return(RedirectToAction("Index"));
                    }
                    catch (DbUpdateConcurrencyException ex)      // If there is concurrency
                    {
                        var entry         = ex.Entries.Single(); // get the entry
                        var clientValues  = (Movie)entry.Entity;
                        var databaseEntry = entry.GetDatabaseValues();

                        if (databaseEntry == null)
                        { // deleted
                            ModelState.AddModelError("", "Record has been deleted from another user");
                        }
                        else
                        { // values are changed by other user
                            var databaseValues = (Movie)databaseEntry.ToObject();

                            ModelState.AddModelError("", "Record has been update from another user. Please check the values in the list. Otherwise click 'Save' again and re-write the changes. ");
                            movie.RowVersion = databaseValues.RowVersion; // set the row version
                        }
                        return(View(movie));
                    }
                }
                ModelState.AddModelError("", "Record is not valid.");
                return(View(movie));
            }
        }