public async Task <OperationResult> Create(InnerResourceModel model) { try { var innerResource = new InnerResource() { Name = model.Name, ExternalId = model.ExternalId, }; await innerResource.Create(_dbContext); model.Id = innerResource.Id; if (model.RelatedOuterResourcesIds != null) { foreach (var outerResourceId in model.RelatedOuterResourcesIds) { var macth = await _dbContext.OuterInnerResources.SingleOrDefaultAsync(x => x.InnerResourceId == model.Id && x.OuterResourceId == outerResourceId); if (macth == null) { var outerInnerResource = new Microting.eFormOuterInnerResourceBase.Infrastructure.Data.Entities. OuterInnerResource { OuterResourceId = outerResourceId, InnerResourceId = model.Id }; await outerInnerResource.Create(_dbContext); } } await _bus.SendLocal(new OuterInnerResourceCreate(model, null)); } return(new OperationResult(true, _localizationService.GetString("InnerResourceCreatedSuccessfully", model.Name))); } catch (Exception e) { Trace.TraceError(e.Message); _logger.LogError(e.Message); return(new OperationResult(false, _localizationService.GetString("ErrorCreatingInnerResource"))); } }
public async Task <OperationResult> Update(InnerResourceModel model) { try { var innerResource = await _dbContext.InnerResources.SingleOrDefaultAsync(x => x.Id == model.Id); innerResource.ExternalId = model.ExternalId; innerResource.Name = model.Name; await innerResource.Update(_dbContext); var outerInnerResources = await _dbContext.OuterInnerResources.Where(x => x.InnerResourceId == innerResource.Id && x.WorkflowState != Constants.WorkflowStates.Removed).ToListAsync(); var requestedOuterResourceIds = model.RelatedOuterResourcesIds; var deployedOuterResourceIds = new List <int>(); var toBeDeployed = new List <int>(); foreach (var outerInnerResource in outerInnerResources) { deployedOuterResourceIds.Add(outerInnerResource.OuterResourceId); if (!model.RelatedOuterResourcesIds.Contains(outerInnerResource.OuterResourceId)) { await outerInnerResource.Delete(_dbContext); await _bus.SendLocal(new OuterInnerResourceUpdate(outerInnerResource.Id)); } } if (requestedOuterResourceIds.Count != 0) { toBeDeployed.AddRange(requestedOuterResourceIds.Where(x => !deployedOuterResourceIds.Contains(x))); } foreach (var outerResourceId in toBeDeployed) { var outerResource = _dbContext.OuterResources.SingleOrDefault(x => x.Id == outerResourceId); if (outerResource != null) { var outerInnerResource = await _dbContext.OuterInnerResources.SingleOrDefaultAsync(x => x.InnerResourceId == innerResource.Id && x.OuterResourceId == outerResourceId); if (outerInnerResource == null) { outerInnerResource = new Microting.eFormOuterInnerResourceBase.Infrastructure.Data.Entities.OuterInnerResource() { OuterResourceId = outerResourceId, InnerResourceId = innerResource.Id }; await outerInnerResource.Create(_dbContext); } else { outerInnerResource.WorkflowState = Constants.WorkflowStates.Created; await outerInnerResource.Update(_dbContext); } await _bus.SendLocal(new OuterInnerResourceUpdate(outerInnerResource.Id)); } } return(new OperationResult(true, _localizationService.GetString("InnerResourceUpdatedSuccessfully"))); } catch (Exception e) { Trace.TraceError(e.Message); _logger.LogError(e.Message); return(new OperationResult(false, _localizationService.GetString("ErrorUpdatingInnerResource"))); } }