public ActionResult DeleteConfirmed(string id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var context = new TelesalesScheduleDbContext())
            {
                // Get user from database
                var user = context.Users
                           .Where(u => u.Id.Equals(id))
                           .First();

                // when deleting user to change IsDeleted = true in Employee
                var employee = context.Employees.SingleOrDefault(e => e.UserName == user.UserName);
                employee.IsDeleted            = true;
                context.Entry(employee).State = EntityState.Modified;

                // Delete user and save changes
                context.Users.Remove(user);
                context.SaveChanges();

                this.AddNotification("User deleted.", NotificationType.WARNING);

                return(RedirectToAction("List"));
            }
        }
Beispiel #2
0
        public ActionResult Edit(EditEmployeeViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                using (var context = new TelesalesScheduleDbContext())
                {
                    var employee = context.Employees
                                   .FirstOrDefault(e => e.Id == viewModel.Id);

                    if (employee == null)
                    {
                        return(HttpNotFound());
                    }

                    employee.FullName         = viewModel.FullName;
                    employee.UserName         = viewModel.UserName;
                    employee.Manager          = context.Employees.FirstOrDefault(m => m.FullName == viewModel.ManagerFullName);
                    employee.BirthDay         = viewModel.BirthDay;
                    employee.FullTimeAgent    = viewModel.FullTimeAgent;
                    employee.SaveDeskAgent    = viewModel.SaveDeskAgent;
                    employee.SeniorSpecialist = viewModel.SeniorSpecialist;
                    employee.IsDeleted        = viewModel.IsDeleted;

                    context.Entry(employee).State = EntityState.Modified;
                    context.SaveChanges();

                    this.AddNotification("Employee edited.", NotificationType.INFO);

                    return(RedirectToAction("List"));
                }
            }

            return(View(viewModel));
        }
Beispiel #3
0
        public ActionResult Edit(Computer computer)
        {
            if (ModelState.IsValid)
            {
                using (var context = new TelesalesScheduleDbContext())
                {
                    context.Entry(computer).State = EntityState.Modified;
                    context.SaveChanges();

                    this.AddNotification("Computer edited.", NotificationType.INFO);

                    return(RedirectToAction("List"));
                }
            }

            return(View(computer));
        }
Beispiel #4
0
        public ActionResult Create(Computer computer)
        {
            if (ModelState.IsValid)
            {
                using (var context = new TelesalesScheduleDbContext())
                {
                    context.Computers.Add(computer);
                    context.SaveChanges();

                    this.AddNotification("Computer created.", NotificationType.SUCCESS);

                    return(RedirectToAction("List"));
                }
            }

            return(View(computer));
        }
        public ActionResult Edit(string id, EditUserViewModel viewModel)
        {
            // Check if model is valid
            if (ModelState.IsValid)
            {
                using (var context = new TelesalesScheduleDbContext())
                {
                    // Get user from database
                    var user = context.Users.FirstOrDefault(u => u.Id == id);

                    // Check if user exists
                    if (user == null)
                    {
                        return(HttpNotFound());
                    }

                    // If password field is not empty, change password
                    if (!string.IsNullOrEmpty(viewModel.Password))
                    {
                        var hasher       = new PasswordHasher();
                        var passwordHash = hasher.HashPassword(viewModel.Password);
                        user.PasswordHash = passwordHash;
                    }

                    // Set user properties
                    user.Email    = viewModel.User.Email;
                    user.UserName = viewModel.User.Email;
                    this.SetUserRoles(viewModel, user, context);

                    // Save changes
                    context.Entry(user).State = EntityState.Modified;
                    context.SaveChanges();

                    this.AddNotification("User edited", NotificationType.INFO);

                    return(RedirectToAction("List"));
                }
            }

            return(View(viewModel));
        }
Beispiel #6
0
        public ActionResult Create(EditEmployeeViewModel employee)
        {
            if (ModelState.IsValid)
            {
                using (var db = new TelesalesScheduleDbContext())
                {
                    var emoloyees = db.Employees.Select(e => e.FullName).ToList();
                    if (emoloyees.Contains(employee.FullName))
                    {
                        ViewBag.ErrorMessage = "Employee already exist!";
                        return(View());
                    }

                    else
                    {
                        var emp = new Employee
                        {
                            FullName         = employee.FullName,
                            BirthDay         = Convert.ToDateTime(employee.BirthDay),
                            FullTimeAgent    = employee.FullTimeAgent,
                            IsDeleted        = employee.IsDeleted,
                            SaveDeskAgent    = employee.SaveDeskAgent,
                            UserName         = employee.UserName,
                            SeniorSpecialist = employee.SeniorSpecialist,
                            Manager          = db.Employees.Where(e => e.FullName == employee.ManagerFullName).FirstOrDefault()
                        };

                        db.Employees.Add(emp);
                        db.SaveChanges();

                        this.AddNotification("Employee created.", NotificationType.SUCCESS);
                    }

                    return(RedirectToAction("List"));
                }
            }

            return(View(employee));
        }