Esempio n. 1
0
        public ActionResult <BaseResponse> ModifyRole(RoleDto request)
        {
            var  entity = _mapper.Map <Role>(request);
            bool result;

            if (request.Id > 0)
            {
                var old = _accountStoreService.QueryRoleById(request.Id);
                if (request.RouterIds != null && request.RouterIds.Count > 0)
                {
                    Utils.ListBatchAddOrDelete <long>(old.RoleRouterRelations.Select(x => x.RouterId).ToList(), request.RouterIds, out List <long> deleteList, out List <long> addList);

                    if (deleteList.Any())
                    {
                        _accountStoreService.DeleteRoleRouterRelations(old.Id, deleteList);
                    }

                    if (addList.Any())
                    {
                        var addEntities = new List <RoleRouterRelation>();
                        addList.ForEach(id =>
                        {
                            addEntities.Add(new RoleRouterRelation()
                            {
                                RoleId = old.Id, RouterId = id
                            });
                        });
                        _accountStoreService.AddRoleRouterRelations(addEntities);
                    }
                }
                result = _accountStoreService.UpdateRole(entity);
            }
            else
            {
                entity.CreateTime = DateTime.Now;

                var roleRouterRelations = new List <RoleRouterRelation>();
                request.RouterIds.ForEach(id =>
                {
                    roleRouterRelations.Add(new RoleRouterRelation()
                    {
                        RouterId = id
                    });
                });
                entity.RoleRouterRelations = roleRouterRelations;

                result = _accountStoreService.AddRole(entity);
            }
            return(BaseResponse.GetResponse(result));
        }