public ActionResult Create([DataSourceRequest] DataSourceRequest request, UserAdministrationViewModel user)
        {
            if (user != null && this.ModelState.IsValid)
            {
                var town = this.towns.Find(user.Town.Id);
                if (town == null || user.Town.Id == 0)
                {
                    town = this.towns.GetFirst();
                }

                var newUser = new ApplicationUser()
                {
                    FirstName = user.FirstName,
                    LastName = user.LastName,
                    Email = user.Email,
                    PhoneNumber = user.PhoneNumber,
                    UserName = user.UserName,
                    Town = town
                };

                this.context.Users.Add(newUser);
                this.context.SaveChanges();

                user.Town.Name = newUser.Town.Name;
            }

            return this.Json(new[] { user }.ToDataSourceResult(request, this.ModelState), JsonRequestBehavior.AllowGet);
        }
        public ActionResult Edit(UserAdministrationViewModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return this.View(model);
            }

            this.users.Edit(model.Id, model.Email, model.UserName, model.PhoneNumber);

            this.SetTempDataSuccessMessage("User edited successfully!");

            return this.RedirectToAction("Index");
        }
        public void EditWithPostRequestShouldRedirectCorrectly()
        {
            UserAdministrationViewModel model = new UserAdministrationViewModel
            {
                Email = "*****@*****.**",
                Id = "userId",
                PhoneNumber = "123",
                UserName = "******"
            };

            this.controller.WithCallTo(x => x.Edit(model))
                .ShouldRedirectTo(x => x.Index());
        }
Ejemplo n.º 4
0
        public ActionResult Index()
        {
            var userManager = HttpContext.GetOwinContext().GetUserManager <ApplicationUserManager>();

            var users = userManager.Users.Select(x => new UserViewModel
            {
                Id       = x.Id,
                UserName = x.UserName,
                UserType = userManager.GetRolesAsync(x.Id).ConfigureAwait(false).GetAwaiter().GetResult().FirstOrDefault() ?? "",
            }).ToList();
            var userAdministrationViewModel = new UserAdministrationViewModel {
                Users = users
            };


            return(View(userAdministrationViewModel));
        }
        public ActionResult DeleteUser([DataSourceRequest] DataSourceRequest request, UserAdministrationViewModel user)
        {
            if (user != null && this.ModelState.IsValid)
            {
                try
                {
                    var userToDelete = this.data.Users.All().FirstOrDefault(u => u.Id == user.Id);
                    this.data.Users.Delete(userToDelete);
                    this.data.SaveChanges();
                }
                catch (Exception ex)
                {
                    this.ModelState.AddModelError(string.Empty, ex.Message);
                }
            }

            return Json(new[] { user }.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }
Ejemplo n.º 6
0
 public MainWindowViewModel()
 {
     UserAdministration    = new UserAdministrationViewModel();
     OrderLog              = new OrderLogViewModel();
     CustomerOrderOverView = new CustomerOrderOverViewViewModel();
 }
        public ActionResult Delete([DataSourceRequest] DataSourceRequest request, UserAdministrationViewModel user)
        {
            if (user != null && !string.IsNullOrEmpty(user.Id))
            {
                var userDb = this.context.Users.FirstOrDefault(u => u.Id == user.Id);

                foreach (var advertisementId in userDb.Advertisements.Select(a => a.Id).ToList())
                {
                    var comments = this.comments
                        .GetAll()
                        .Where(c => c.AdvertisementId == advertisementId)
                        .Select(c => c.Id)
                        .ToList();

                    foreach (var commentId in comments)
                    {
                        var currentComment = this.comments.GetById(commentId).FirstOrDefault();
                        this.comments.Remove(currentComment);
                    }

                    var currentAdvertisement = this.advertisements.GetById(advertisementId).FirstOrDefault();
                    this.advertisements.Remove(currentAdvertisement);
                }

                foreach (var messageId in userDb.Messages.Select(m => m.Id).ToList())
                {
                    var currentMessage = this.messages.GetById(messageId);

                    this.messages.Remove(currentMessage);
                }

                this.context.Users.Remove(userDb);
                this.context.SaveChanges();
            }

            return this.Json(new[] { user }.ToDataSourceResult(request, this.ModelState), JsonRequestBehavior.AllowGet);
        }
        public ActionResult Update([DataSourceRequest] DataSourceRequest request, UserAdministrationViewModel user)
        {
            if (user != null && this.ModelState.IsValid)
            {
                var userDb = this.manager.Users.FirstOrDefault(u => u.Id == user.Id);
                var town = this.towns.Find(user.Town.Id);

                if (town == null || user.Town.Id == 0)
                {
                    userDb.TownId = this.towns.GetFirst().Id;
                }
                else
                {
                    userDb.TownId = user.Town.Id;
                }

                if (string.IsNullOrWhiteSpace(user.UserName))
                {
                    userDb.UserName = user.Email;
                }
                else
                {
                    userDb.UserName = user.UserName;
                }

                userDb.FirstName = user.FirstName;
                userDb.LastName = user.LastName;
                userDb.Email = user.Email;
                userDb.PhoneNumber = user.PhoneNumber;

                this.manager.Update(userDb);

                user.Town.Name = userDb.Town.Name;
                user.UserName = userDb.UserName;
            }

            return this.Json(new[] { user }.ToDataSourceResult(request, this.ModelState), JsonRequestBehavior.AllowGet);
        }
        public ActionResult UpdateUser([DataSourceRequest] DataSourceRequest request, UserAdministrationViewModel user)
        {
            if (user != null && this.ModelState.IsValid)
            {
                var existingUser = this.data.Users.GetById(user.Id);

                existingUser.UserName = user.Username;
                existingUser.Email = user.Email;
                existingUser.PhoneNumber = user.PhoneNumber;
                this.data.SaveChanges();

                this.data.Users.Update(existingUser);
                this.data.SaveChanges();
            }

            return Json(new[] { user }.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }