Example #1
0
        public ActionResult <BaseResponse> ModifyUser(UserDto request)
        {
            var  entity = _mapper.Map <User>(request);
            bool result = false;

            if (entity.Id > 0)
            {
                var user = _accountStoreService.QueryUserById(request.Id);
                user.Name   = entity.Name;
                user.Avatar = entity.Avatar;
                user.Status = entity.Status;

                if (request.RoleIds != null && request.RoleIds.Count > 0)
                {
                    Utils.ListBatchAddOrDelete <long>(user.UserRoleRelations.Select(x => x.RoleId).ToList(), request.RoleIds, out List <long> deleteList, out List <long> addList);

                    if (deleteList.Any())
                    {
                        _accountStoreService.DeleteUserRoleRelations(user.Id, deleteList);
                    }

                    if (addList.Any())
                    {
                        var addEntities = new List <UserRoleRelation>();
                        addList.ForEach(id =>
                        {
                            addEntities.Add(new UserRoleRelation()
                            {
                                UserId = user.Id, RoleId = id
                            });
                        });
                        _accountStoreService.AddUserRoleRelations(addEntities);
                    }
                }

                result = _accountStoreService.UpdateUser(user);
            }
            return(BaseResponse.GetResponse(result));
        }