Exemplo n.º 1
0
        public IActionResult Save(ProfileViewModel Data)
        {
            string UserName = HttpContext.Session.GetString("login");

            if (!string.IsNullOrEmpty(UserName))
            {
                string   MessageResult = "";
                KMS_User uData         = _ent.KMS_User.Where(w => w.Username == Data.User.Username).FirstOrDefault();
                if (uData != null)
                {
                    try
                    {
                        List <KMS_UserPermission> LstPermission = new List <KMS_UserPermission>();
                        List <KMS_UserMenu>       LstMenu       = new List <KMS_UserMenu>();
                        foreach (var p in Data.LstPermission.Where(o => o.IsActive))
                        {
                            LstPermission.Add(new KMS_UserPermission
                            {
                                Username     = Data.User.Username,
                                PermissionId = p.PermissionId,
                                IsActive     = p.IsActive
                            });
                        }
                        foreach (var m in Data.LstMenu.Where(o => o.IsActive))
                        {
                            LstMenu.Add(new KMS_UserMenu
                            {
                                Username = Data.User.Username,
                                MenuId   = m.MenuId,
                                IsActive = m.IsActive
                            });
                        }
                        _ent.RemoveRange(uData.Permission);
                        _ent.RemoveRange(uData.Menus);
                        uData.Username = Data.User.Username;
                        //uData..Password = Extensions.HashPasswordAuth.EncryptString(Data.User.Password);
                        uData.FName      = Data.User.FName;
                        uData.LName      = Data.User.LName;
                        uData.Email      = Data.User.Email;
                        uData.AddressL1  = Data.User.AddressL1;
                        uData.AddressL2  = Data.User.AddressL2;
                        uData.IsActive   = Data.User.IsActive;
                        uData.Cod_update = UserName;
                        uData.Dtm_update = DateTime.Now;
                        uData.Permission = LstPermission;
                        uData.Menus      = LstMenu;
                        _ent.SaveChanges();
                        MessageResult = "Update data success.";
                    }
                    catch (Exception ex)
                    {
                        MessageResult = ex.Message + "/ " + ex.InnerException.Message;
                    }
                }
                else
                {
                    //Insert
                    try
                    {
                        int RowId = _ent.KMS_User.OrderByDescending(o => o.UserId).Select(s => (int)s.UserId).FirstOrDefault() + 1;
                        List <KMS_UserPermission> LstPermission = new List <KMS_UserPermission>();
                        List <KMS_UserMenu>       LstMenu       = new List <KMS_UserMenu>();
                        foreach (var p in Data.LstPermission.Where(o => o.IsActive))
                        {
                            LstPermission.Add(new KMS_UserPermission
                            {
                                Username     = Data.User.Username,
                                PermissionId = p.PermissionId,
                                IsActive     = p.IsActive
                            });
                        }
                        foreach (var m in Data.LstMenu.Where(o => o.IsActive))
                        {
                            LstMenu.Add(new KMS_UserMenu
                            {
                                Username = Data.User.Username,
                                MenuId   = m.MenuId,
                                IsActive = m.IsActive
                            });
                        }
                        _ent.KMS_User.Add(new KMS_User
                        {
                            UserId     = RowId,
                            Username   = Data.User.Username,
                            Password   = HashPasswordAuth.EncryptString(Data.User.Password),
                            FName      = Data.User.FName,
                            LName      = Data.User.LName,
                            Email      = Data.User.Email,
                            AddressL1  = Data.User.AddressL1,
                            AddressL2  = Data.User.AddressL2,
                            IsActive   = Data.User.IsActive,
                            Cod_create = UserName,
                            Dtm_create = DateTime.Now,
                            Permission = LstPermission,
                            Menus      = LstMenu
                        });
                        _ent.SaveChanges();
                        MessageResult = "Insert data success.";
                    }
                    catch (Exception ex)
                    {
                        MessageResult = ex.Message + "/ " + ex.InnerException.Message;
                    }
                    MessageResult = "Insert data complete.";
                }
                return(RedirectToAction("ListUser", "Auth"));
            }
            else
            {
                return(RedirectToAction("Index", "Auth"));
            }
        }