private OuterInnerResourceVersion MapVersions(OuterInnerResource outerInnerResource) { OuterInnerResourceVersion outerInnerResourceVersionVer = new OuterInnerResourceVersion(); outerInnerResourceVersionVer.OuterResourceId = outerInnerResource.OuterResourceId; outerInnerResourceVersionVer.InnerResourceId = outerInnerResource.InnerResourceId; outerInnerResourceVersionVer.Version = outerInnerResource.Version; outerInnerResourceVersionVer.CreatedAt = outerInnerResource.CreatedAt; outerInnerResourceVersionVer.UpdatedAt = outerInnerResource.UpdatedAt; outerInnerResourceVersionVer.OuterInnerResourceId = outerInnerResource.Id; return(outerInnerResourceVersionVer); }
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 Delete(OuterInnerResourcePnDbContext dbContext) { OuterInnerResource outerInnerResource = dbContext.OuterInnerResources.FirstOrDefault(x => x.Id == Id); if (outerInnerResource == null) { throw new NullReferenceException($"Could not find machineArea with id: {Id}"); } outerInnerResource.WorkflowState = eForm.Infrastructure.Constants.Constants.WorkflowStates.Removed; if (dbContext.ChangeTracker.HasChanges()) { outerInnerResource.UpdatedAt = DateTime.Now; outerInnerResource.Version += 1; dbContext.OuterInnerResourceVersions.Add(MapVersions(outerInnerResource)); dbContext.SaveChanges(); } }
public async Task Update(OuterInnerResourcePnDbContext dbContext) { OuterInnerResource outerInnerResource = dbContext.OuterInnerResources.FirstOrDefault(x => x.Id == Id); if (outerInnerResource == null) { throw new NullReferenceException($"Could not find machineArea with id: {Id}"); } outerInnerResource.OuterResourceId = OuterResourceId; outerInnerResource.InnerResourceId = InnerResourceId; if (dbContext.ChangeTracker.HasChanges()) { outerInnerResource.UpdatedAt = DateTime.Now; outerInnerResource.Version += 1; dbContext.OuterInnerResourceVersions.Add(MapVersions(outerInnerResource)); dbContext.SaveChanges(); } }
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"))); } }