public async Task CreateResourceAsync(CreateResourceCommand command) { Check.NotNull(command, nameof(command)); Resource resource = _mapper.Map <Resource>(command); await _applicationContext.Resources.AddAsync(resource); await _applicationContext.SaveChangesAsync(); }
public ResourceAggregateRoot Create(IWorkContext context, CreateResourceCommand command) { this.ApplyEvent(new CreateResourceEvent() { AggregateId = this.AggregateId, Version = this.Version, CreateDate = context.WorkTime, Creator = context.GetWorkerName(), ActionDescn = command.Resource.ActionDescn, GroupSort = command.Resource.GroupSort }); return(this); }
public async Task <IActionResult> PostResource() { var resourceId = ResourceId.New; var id = resourceId.GetGuid(); var command = new CreateResourceCommand(resourceId); var result = await System.GetExtension <ServiceProviderHolder>().ServiceProvider.GetRequiredService <ICommandBus>().Publish(command); if (result.IsSuccess) { var location = new Uri($"{GetAbsoluteUri(HttpContext)}/api/operations/{id}"); var contentLocation = new Uri($"{GetAbsoluteUri(HttpContext)}/api/resources/{id}"); HttpContext.Response.Headers.Add("Location", location.ToString()); HttpContext.Response.Headers.Add("Content-Location", contentLocation.ToString()); return(Accepted(new ResourceResponseModel(id, id))); } return(BadRequest()); }
public async Task <IActionResult> PostResource() { var resourceId = ResourceId.New; var id = resourceId.GetGuid(); var command = new CreateResourceCommand(resourceId); var result = await _resourceManager.Ask <IExecutionResult>(command); if (result.IsSuccess) { var location = new Uri($"{GetAbsoluteUri(HttpContext)}/api/operations/{id}"); var contentLocation = new Uri($"{GetAbsoluteUri(HttpContext)}/api/resources/{id}"); HttpContext.Response.Headers.Add("Location", location.ToString()); HttpContext.Response.Headers.Add("Content-Location", contentLocation.ToString()); return(Accepted(new ResourceResponseModel(id, id))); } else { return(BadRequest()); } }
public async Task <IActionResult> Create([FromBody] CreateResourceCommand command) { var id = await Mediator.Send(command); return(CreatedAtAction(nameof(Get), new { id }, new { id })); }
/// <summary> /// 因为是启动的事件,而且资源修改与发布事件,在这个系统内通过事件总线发布没多大意义(但在webservice这些设定中就有意义了) /// </summary> /// <param name="command"></param> /// <param name="communication"></param> public ICommandHandlerResult Execute(ICommandContext context, InitResourceCommand command) { if (command.Resources == null) { return(context.CreateResult(CommandHandlerStatus.NothingChanged)); } var list = resourceRepository.RebuildRoots(); if (list.IsNullOrEmpty()) { //第一次初始化,实际上要不要将部门与组的权限一起删除呢?因为有可能是在运营过程中删除的 var roots = new List <ResourceAggregateRoot>(command.Resources.Count()); foreach (var r in command.Resources) { var cmd = new CreateResourceCommand(r.AggregateId) { Resource = new ActionResourceDescriptor() { ActionDescn = r.ActionDescn, GroupSort = r.GroupSort, AggregateId = r.AggregateId } }; var root = context.GetAggregateRoot(cmd.AggregateId, () => ResourceAggregateRoot.Register(context, cmd)); if (root.CanCommit()) { roots.Add(root); } } //保存权限 this.resourceRepository.Save(roots); return(context.CreateResult(CommandHandlerStatus.Success)); } //表明这些是新增的,在新加的记录中删除已经存在的 var news = command.Resources.ToList(); news.RemoveAll(n => list.Any(o => o.AggregateId == n.AggregateId)); //表明这些是已经存在,在已经存在的记录中删除不是新的记录中 var deletes = list.ToList(); deletes.RemoveAll(n => command.Resources.Any(o => o.AggregateId == n.AggregateId)); //原来那些已经存在的 var exists = list.ToList().FindAll(n => command.Resources.Any(o => o.AggregateId == n.AggregateId)); if (deletes != null && deletes.Count > 0) { var targets = new List <ResourceAggregateRoot>(deletes.Count); foreach (var r in deletes) { r.Remove(context); if (r.CanCommit()) { targets.Add(r); } } this.resourceRepository.Delete(targets); this.groupResourceRepository.RemoveNotExistsGroupResource(targets); this.departmentResourceRepository.RemoveNotExistsDepartmentResource(targets); } if (news != null && news.Count > 0) { var targets = new List <ResourceAggregateRoot>(news.Count); foreach (var r in news) { var cmd = new CreateResourceCommand(r.AggregateId) { Resource = new ActionResourceDescriptor() { AggregateId = r.AggregateId, ActionDescn = r.ActionDescn, GroupSort = r.GroupSort } }; var root = ResourceAggregateRoot.Register(context, cmd); if (root.CanCommit()) { targets.Add(root); } } this.resourceRepository.Save(targets); } if (exists != null && exists.Count > 0) { var targets = new List <ResourceAggregateRoot>(exists.Count); foreach (var r in exists) { var res = command.Resources.FirstOrDefault(ta => ta.AggregateId == r.AggregateId); r.ChangeInfo(context, res.ActionDescn, res.GroupSort); if (r.CanCommit()) { targets.Add(r); } } if (targets.Any()) { resourceRepository.Change(targets); } } return(context.CreateResult(CommandHandlerStatus.Success)); }
public async Task <IActionResult> CreateResource([FromBody] CreateResourceCommand command) { await _resourceBusiness.CreateResourceAsync(command); return(Ok()); }
public static ResourceAggregateRoot Register(IWorkContext context, CreateResourceCommand command) { return(new ResourceAggregateRoot(command.AggregateId).Create(context, command)); }