public async Task <IActionResult> Get(long id, [FromHeader(Name = HeaderNames.IfNoneMatch)] string ifNoneMatch) { try { var templateDescriptor = await _templatesStorageReader.GetTemplateDescriptor(id, null); Response.Headers[HeaderNames.ETag] = $"\"{templateDescriptor.VersionId}\""; Response.Headers[HeaderNames.LastModified] = templateDescriptor.LastModified.ToString("R"); if (!string.IsNullOrEmpty(ifNoneMatch) && ifNoneMatch.Trim('"') == templateDescriptor.VersionId) { return(NotModified()); } return(Json( new { id, templateDescriptor.VersionId, templateDescriptor.LastModified, templateDescriptor.Author, templateDescriptor.AuthorLogin, templateDescriptor.AuthorName, templateDescriptor.Properties, templateDescriptor.Elements })); } catch (ObjectNotFoundException) { return(NotFound()); } }
public async Task <SessionContext> GetSessionContext(Guid sessionId) { var(sessionDescriptor, authorInfo, expiresAt) = await _sessionStorageReader.GetSessionDescriptor(sessionId); var templateDescriptor = await _templatesStorageReader.GetTemplateDescriptor(sessionDescriptor.TemplateId, sessionDescriptor.TemplateVersionId); return(new SessionContext( templateDescriptor.Id, templateDescriptor, sessionDescriptor.Language, authorInfo, expiresAt)); }
public async Task <IVersionedTemplateDescriptor> GetTemplateDescriptor(long id, string versionId) { var(persistenceDescriptor, _, _, _) = await GetObjectFromS3 <ObjectPersistenceDescriptor>(id.AsS3ObjectKey(Tokens.ObjectPostfix), versionId, default); return(await _templatesStorageReader.GetTemplateDescriptor(persistenceDescriptor.TemplateId, persistenceDescriptor.TemplateVersionId)); }
public async Task <string> Create(long id, AuthorInfo authorInfo, IObjectDescriptor objectDescriptor) { CheckRequredProperties(id, objectDescriptor); var redLock = await _distributedLockManager.CreateLockAsync(id); try { 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, objectDescriptor)); } finally { redLock?.Dispose(); } }