public async Task <Guid> CreateExperimentTemplate(string name, string description, Guid projectId) { await VerifyAccessToProject(projectId); var accounts = await dbContext.RkAccounts.ToListAsync(); var experimentTemplate = new RkExperimentTemplate() { Name = name, Description = description, CreatedAt = DateTime.UtcNow, ModifiedAt = DateTime.UtcNow, OwnerId = userContext.UserId, ProjectId = projectId, Accounts = accounts.Select(a => new RkAccountInExperiment() { AccountId = a.RkAccountId }).ToList() }; await dbContext.AddAsync(experimentTemplate); await dbContext.SaveChangesAsync(); return(experimentTemplate.RkExperimentTemplateId); }
private void VerifyEditAccess(RkExperimentTemplate experimentTemplate) { if (experimentTemplate.OwnerId != userContext.UserId && !userContext.IsAdmin()) { throw new ApplicationException($"User[{userContext.UserId}] does not have edit access to template [{experimentTemplate.RkExperimentTemplateId}]"); } }
private void VerifyUpdatedExperimentModel(RkExperimentTemplateModel model, RkExperimentTemplate experimentTemplate) { if (experimentTemplate.OwnerId != model.OwnerId) { throw new ApplicationException("Owner of experiment template cannot be changed"); } if (experimentTemplate.CreatedAt != model.CreatedAt) { throw new ApplicationException("Creation date of experiment template cannot be changed"); } foreach (var accountInModel in model.Accounts) { if (experimentTemplate.Accounts .FirstOrDefault(a => a.AccountId == accountInModel.AccountId && a.ExperimentTemplateId == accountInModel.ExperimentTemplateId && a.RkAccountInExperimentId == accountInModel.RkAccountInExperimentId) == null) { throw new ApplicationException($"Account list in experiment template cannot be edited"); } } if (experimentTemplate.Accounts.Count != model.Accounts.Count) { throw new ApplicationException($"Account list in experiment template cannot be edited"); } foreach (var resourceInModel in model.Resources) { if (experimentTemplate.Resources .FirstOrDefault(r => r.ResourceId == resourceInModel.ResourceId && r.RkResourceInExperimentId == resourceInModel.RkResourceInExperimentId && r.ExperimentTemplateId == r.ExperimentTemplateId) == null) { throw new ApplicationException($"Resource in updated model [{resourceInModel.ResourceId}] does not exists in resources list of experiment [{experimentTemplate.RkExperimentTemplateId}]"); } } if (experimentTemplate.Resources.Count != model.Resources.Count) { throw new ApplicationException($"Resource list in experiment template cannot be edited"); } foreach (var productInModel in model.Products) { if (experimentTemplate.Products .FirstOrDefault(r => r.ProductId == productInModel.ProductId && r.RkProductInExperimentId == productInModel.RkProductInExperimentId && r.ExperimentTemplateId == r.ExperimentTemplateId) == null) { throw new ApplicationException($"Product in updated model [{productInModel.ProductId}] does not exists in products list of experiment [{experimentTemplate.RkExperimentTemplateId}]"); } foreach (var resource in productInModel.Resources) { if (experimentTemplate.Resources.FirstOrDefault(p => p.RkResourceInExperimentId == resource.RkResourceInExperimentId) == null) { throw new ApplicationException($"ResourceProduct for product in experiment [{resource.RkResourceInExperimentId}] cannot be added - resource of this ResourceProduct does not exist in current experiment [{experimentTemplate.RkExperimentTemplateId}]"); } } var groupedResources = productInModel.Resources.GroupBy(r => r.RkResourceInExperimentId); foreach (var group in groupedResources) { if (group.Count() > 1) { throw new ApplicationException($"ResourceProduct (resource [{group.Key}]) can be added to product only once"); } } } if (experimentTemplate.Products.Count != model.Products.Count) { throw new ApplicationException($"Product list in experiment template cannot be edited"); } }