Beispiel #1
0
        public async Task <IActionResult> Post([FromBody] sys_user value)
        {
            using (var tran = _context.Database.BeginTransaction())
            {
                try
                {
                    var oldSysUser = _sysUserRpt.FindBy(f => f.UserId == value.UserId);
                    if (oldSysUser.Any())
                    {
                        return(BadRequest(string.Concat(value.UserId, "已经存在。")));
                    }
                    value.CreatedAt = DateTime.Now;
                    value.UpdatedAt = DateTime.Now;
                    value.IsDelete  = false;
                    if (User.Identity is ClaimsIdentity identity)
                    {
                        value.CreatedBy = identity.Name ?? "admin";
                    }
                    _sysUserRpt.Add(value);
                    _sysUserRpt.Commit();

                    if (!string.IsNullOrEmpty(value.RoleIds) && value.RoleIds.Length > 1)
                    {
                        //新增用户角色关系表
                        string[] roles = value.RoleIds.Split(",".ToArray());
                        foreach (var item in roles)
                        {
                            if (!string.IsNullOrEmpty(item))
                            {
                                var userrole = new sys_role_user {
                                    RoleId = int.Parse(item), UserId = value.Id
                                };
                                _sysRoleUserRpt.Add(userrole);
                            }
                        }
                        _sysRoleUserRpt.Commit();
                    }
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    tran.Rollback();
                    return(BadRequest(ex));
                }
            }
            return(new OkObjectResult(value));
        }
Beispiel #2
0
        public async Task <IActionResult> Put(int id, [FromBody] sys_user value)
        {
            if (id == 0)
            {
                //修改密码
                var usr = _sysUserRpt.GetSingle(f => f.Id == value.Id);
                if (usr != null)
                {
                    usr.Pwd = value.Pwd;
                    _sysUserRpt.Update(usr);
                    _sysUserRpt.Commit();
                }
            }
            else
            {
                using (var tran = _context.Database.BeginTransaction())
                {
                    try
                    {
                        sys_user userDb = _sysUserRpt.GetSingle(id);
                        if (userDb == null)
                        {
                            return(NotFound());
                        }
                        if (value.RoleIds != userDb.RoleIds)
                        {
                            //修改了用户角色
                            _sysRoleUserRpt.DeleteWhere(f => f.UserId == id);
                            _sysRoleUserRpt.Commit();

                            //新增用户角色关系表
                            string[] roles = value.RoleIds.Split(",".ToArray());
                            foreach (var item in roles)
                            {
                                if (!string.IsNullOrEmpty(item))
                                {
                                    var userrole = new sys_role_user {
                                        RoleId = int.Parse(item), UserId = id
                                    };
                                    _sysRoleUserRpt.Add(userrole);
                                }
                            }
                            _sysRoleUserRpt.Commit();
                        }
                        userDb.IsValid   = value.IsValid;
                        userDb.Mobile    = value.Mobile;
                        userDb.Tel       = value.Tel;
                        userDb.Works     = value.Works;
                        userDb.Title     = value.Title;
                        userDb.UserId    = value.UserId;
                        userDb.UserName  = value.UserName;
                        userDb.UpdatedAt = DateTime.Now;
                        userDb.RoleIds   = value.RoleIds;
                        _sysUserRpt.Commit();
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                        tran.Rollback();
                        return(BadRequest(ex));
                    }
                }
            }
            return(new NoContentResult());
        }