public ActionResult Edit(EditVM model)
        {
            if (HttpContext.Session.GetObjectFromJson <LoggedUser>("loggedUser") == null)
            {
                return(RedirectToAction("Login", "Home"));
            }

            if (!HttpContext.Session.GetObjectFromJson <LoggedUser>("loggedUser").IsAdmin)
            {
                return(RedirectToAction("Login", "Home"));
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            User item = new User
            {
                Id       = model.Id,
                Username = model.Username,
                Email    = model.Email,
                IsAdmin  = model.IsAdmin
            };

            using RestaurantManagerContext context = new RestaurantManagerContext();
            context.Users.Update(item);
            context.SaveChanges();

            return(RedirectToAction("Index", "User"));
        }
示例#2
0
 public void Update(T model)
 {
     using (var context = new RestaurantManagerContext()) {
         context.Set <T>().Attach(model);
         context.Set <T>().Update(model);
         context.SaveChanges();
     }
 }
        public ActionResult Create(CreateVM model)
        {
            //if (HttpContext.Session.GetObjectFromJson<LoggedUser>("loggedUser") == null)
            //    return RedirectToAction("Login", "Home");

            //if (!HttpContext.Session.GetObjectFromJson<LoggedUser>("loggedUser").IsAdmin)
            //    return RedirectToAction("Login", "Home");

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            if (model.Password == model.ConfirmPassword)
            {
                using RestaurantManagerContext context = new RestaurantManagerContext();
                if (context.Users.Count() == 0)
                {
                    User item = new User
                    {
                        Username = model.Username,
                        Password = model.ConfirmPassword,
                        Email    = model.Email,
                        IsAdmin  = true
                    };

                    context.Users.Add(item);
                    context.SaveChanges();
                }
                else
                {
                    User item = new User
                    {
                        Username = model.Username,
                        Password = model.ConfirmPassword,
                        Email    = model.Email,
                        IsAdmin  = false
                    };

                    context.Users.Add(item);
                    context.SaveChanges();
                }
            }
            return(RedirectToAction("Index", "User"));
        }
        // GET: User/Delete/5
        public ActionResult Delete(int id)
        {
            if (HttpContext.Session.GetObjectFromJson <LoggedUser>("loggedUser") == null)
            {
                return(RedirectToAction("Login", "Home"));
            }

            if (!HttpContext.Session.GetObjectFromJson <LoggedUser>("loggedUser").IsAdmin)
            {
                return(RedirectToAction("Login", "Home"));
            }

            using RestaurantManagerContext context = new RestaurantManagerContext();
            context.Users.Remove(context.Users.Find(id));
            context.SaveChanges();

            return(RedirectToAction("Index", "User"));
        }