public async Task <string> CreateTemplate(long id, AuthorInfo authorInfo, ITemplateDescriptor templateDescriptor) { if (id == 0) { throw new ArgumentException("Template Id must be set", nameof(id)); } using (await _distributedLockManager.AcquireLockAsync(id)) { if (await _templatesStorageReader.IsTemplateExists(id)) { throw new ObjectAlreadyExistsException(id); } await PutTemplate(id, authorInfo, templateDescriptor); // ceph does not return version-id response header, so we need to do another request to get version return(await _templatesStorageReader.GetTemplateLatestVersion(id)); } }
public async Task <string> Create(long id, AuthorInfo authorInfo, IObjectDescriptor objectDescriptor) { CheckRequredProperties(id, objectDescriptor); using (await _distributedLockManager.AcquireLockAsync(id)) { if (await _objectsStorageReader.IsObjectExists(id)) { throw new ObjectAlreadyExistsException(id); } var templateDescriptor = await _templatesStorageReader.GetTemplateDescriptor(objectDescriptor.TemplateId, objectDescriptor.TemplateVersionId); var latestTemplateVersionId = await _templatesStorageReader.GetTemplateLatestVersion(objectDescriptor.TemplateId); if (!templateDescriptor.VersionId.Equals(latestTemplateVersionId, StringComparison.OrdinalIgnoreCase)) { throw new InvalidOperationException( $"Template '{objectDescriptor.TemplateId}' has an outdated version. " + $"Latest versionId for template '{objectDescriptor.TemplateId}' is '{latestTemplateVersionId}'."); } if (templateDescriptor.Elements.Count != objectDescriptor.Elements.Count) { throw new ObjectInconsistentException( id, $"Quantity of elements in the object doesn't match to the quantity of elements in the corresponding template with Id '{objectDescriptor.TemplateId}' and versionId '{objectDescriptor.TemplateVersionId}'."); } var elementIds = new HashSet <long>(objectDescriptor.Elements.Select(x => x.Id)); if (elementIds.Count != objectDescriptor.Elements.Count) { throw new ObjectInconsistentException(id, "Some elements have non-unique identifiers."); } EnsureObjectElementsState(id, templateDescriptor.Elements, objectDescriptor.Elements); return(await PutObject(id, null, authorInfo, Enumerable.Empty <IObjectElementDescriptor>(), objectDescriptor)); } }
public async Task <string> ModifyTemplate(long id, string versionId, AuthorInfo authorInfo, ITemplateDescriptor templateDescriptor) { if (id == default) { throw new InputDataValidationException("Template Id must be set"); } if (string.IsNullOrEmpty(versionId)) { throw new InputDataValidationException("VersionId must be set"); } using (await _distributedLockManager.AcquireLockAsync(id)) { var(latestVersionId, latestVersionIndex) = await _templatesStorageReader.GetTemplateLatestVersion(id); if (!versionId.Equals(latestVersionId, StringComparison.Ordinal)) { throw new ConcurrencyException(id, versionId, latestVersionId); } return(await PutTemplate(id, authorInfo, templateDescriptor, latestVersionIndex)); } }