public IActionResult Delete(int id)
        {
            Project task = _taskContext.Projects.FirstOrDefault(x => x.id == id);

            _taskContext.Remove(task);
            _taskContext.SaveChanges();

            return(NoContent());
        }
Beispiel #2
0
        public IActionResult Update(UsersModel model, int id)
        {
            if (ModelState.IsValid)
            {
                var UserCurrentRole = User.Identity.GetUserCurrentRole();

                model.login_name = model.login_name?.ToLowerInvariant();

                // Check for duplicate login name.
                var duplicateUsers = from x in _taskContext.Users
                                     where x.login_name.ToLower() == model.login_name.ToLower() &&
                                     x.id != id
                                     select x;

                User user = _taskContext.Users.Include(x => x.Roles).FirstOrDefault(x => x.id == id);

                if (UserCurrentRole == RoleType.ADMIN)
                {
                    if (!duplicateUsers.Any())
                    {
                        // Save values into DB.
                        user.login_name = model.login_name;
                        user.password   = (model.password == null) ? user.password : usersControl.HashPassword(model.password);
                        user.full_name  = model.full_name;
                        user.active     = model.active;

                        if (model.role == RoleType.ADMIN)
                        {
                            if (!user.Roles.Any(u => u.role == RoleType.ADMIN))
                            {
                                user.Roles.Add(new UserRole {
                                    role = RoleType.ADMIN
                                });
                            }
                        }
                        else if (user.Roles.Any(u => u.role == RoleType.ADMIN))
                        {
                            _taskContext.Remove(user.Roles.First(u => u.role == RoleType.ADMIN));
                        }


                        if (model.role == RoleType.Staff)
                        {
                            if (!user.Roles.Any(u => u.role == RoleType.Staff))
                            {
                                user.Roles.Add(new UserRole {
                                    role = RoleType.Staff
                                });
                            }
                        }
                        else if (user.Roles.Any(u => u.role == RoleType.Staff))
                        {
                            _taskContext.Remove(user.Roles.First(u => u.role == RoleType.Staff));
                        }


                        if (user.Roles.Any())
                        {
                            // Save to DB.
                            _taskContext.SaveChanges();
                            return(RedirectToAction("Index"));
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "The User Name is already exsit.");
                    }
                }

                else
                {
                    user.password  = (model.password == null) ? user.password : usersControl.HashPassword(model.password);
                    user.full_name = model.full_name;

                    // Save to DB.
                    _taskContext.SaveChanges();
                    return(RedirectToAction("Index"));
                }
            }

            return(View(model));
        }