Ejemplo n.º 1
0
        async Task <CommandResult <CountResult> > HandleAsync(CountDocuments command)
        {
            var projectUri = command.ProjectId.ToKotoriProjectUri();

            var project = await FindProjectAsync(command.Instance, projectUri).ConfigureAwait(false);

            if (project == null)
            {
                throw new KotoriProjectException(command.ProjectId, "Project does not exist.")
                      {
                          StatusCode = System.Net.HttpStatusCode.NotFound
                      }
            }
            ;

            var documentTypeUri = command.ProjectId.ToKotoriDocumentTypeUri(command.DocumentType, command.DocumentTypeId);
            var documentType    = await FindDocumentTypeAsync(command.Instance, projectUri, documentTypeUri).ConfigureAwait(false);

            if (documentType == null)
            {
                throw new KotoriDocumentTypeException(command.DocumentTypeId, "Document type does not exist.")
                      {
                          StatusCode = System.Net.HttpStatusCode.NotFound
                      }
            }
            ;

            var sql = DocumentDbHelpers.CreateDynamicQueryForDocumentSearch
                      (
                command.Instance,
                projectUri,
                documentTypeUri,
                null,
                "count(1) as number",
                command.Filter,
                null,
                command.Drafts,
                command.Future
                      );

            var count = await CountDocumentsAsync(sql).ConfigureAwait(false);

            return(new CommandResult <CountResult>(new CountResult(count)));
        }
    }
}
Ejemplo n.º 2
0
        public async Task DeleteProjectAsync(IDeleteProject command)
        {
            var project = await _projectRepository.GetProjectAsync(command.Instance, command.ProjectId).ConfigureAwait(false);

            var projectUri = command.ProjectId.ToKotoriProjectUri();

            if (project == null)
            {
                throw new KotoriProjectException(command.ProjectId, "Project does not exist.")
                      {
                          StatusCode = System.Net.HttpStatusCode.NotFound
                      }
            }
            ;

            // TODO: use repository
            var sql = DocumentDbHelpers.CreateDynamicQueryForDocumentSearch
                      (
                command.Instance,
                projectUri,
                null,
                null,
                "count(1) as number",
                null,
                null,
                true,
                true
                      );

            var count = await CountDocumentsAsync(sql).ConfigureAwait(false);

            if (count > 0)
            {
                throw new KotoriProjectException(command.ProjectId, "Project contains documents.");
            }

            var documentTypes = (await HandleAsync(new GetDocumentTypes(command.Instance, command.ProjectId)).ConfigureAwait(false)).Record;

            if (documentTypes.Count > 0)
            {
                throw new KotoriProjectException(command.ProjectId, "Project contains document types.");
            }

            await _projectRepository.DeleteProjectAsync(project).ConfigureAwait(false);
        }
    }
Ejemplo n.º 3
0
        async Task <CommandResult> HandleAsync(DeleteDocument command)
        {
            var documentTypeUri = command.ProjectId.ToKotoriDocumentTypeUri(command.DocumentType, command.DocumentTypeId);
            var projectUri      = command.ProjectId.ToKotoriProjectUri();
            var documentUri     = command.ProjectId.ToKotoriDocumentUri(command.DocumentType, command.DocumentTypeId, command.DocumentId, command.Index);

            var project = await FindProjectAsync(command.Instance, projectUri).ConfigureAwait(false);

            if (project == null)
            {
                throw new KotoriProjectException(command.ProjectId, "Project does not exist.")
                      {
                          StatusCode = System.Net.HttpStatusCode.NotFound
                      }
            }
            ;

            if (command.DocumentType == Enums.DocumentType.Data)
            {
                if (!command.Index.HasValue)
                {
                    throw new KotoriDocumentException(command.DocumentId, "Data document cannot be deleted without index.");
                }
            }

            var document = await FindDocumentByIdAsync
                           (
                command.Instance,
                projectUri,
                documentUri,
                null
                           ).ConfigureAwait(false);

            if (document == null)
            {
                throw new KotoriDocumentException(command.DocumentId, "Document does not exist.")
                      {
                          StatusCode = System.Net.HttpStatusCode.NotFound
                      }
            }
            ;

            if (command.DocumentType == Enums.DocumentType.Data)
            {
                var sql = DocumentDbHelpers.CreateDynamicQueryForDocumentSearch
                          (
                    command.Instance,
                    projectUri,
                    documentTypeUri,
                    null,
                    "count(1) as number",
                    null,
                    null,
                    true,
                    true
                          );

                var count = await CountDocumentsAsync(sql).ConfigureAwait(false);

                var result = await DeleteDocumentAsync(document).ConfigureAwait(false);

                if (!result)
                {
                    throw new KotoriDocumentException(command.DocumentId, "Document has not been deleted.");
                }

                if (command.Index.HasValue &&
                    command.Index.Value != count - 1)
                {
                    var reindexTasks = new List <Task>();

                    for (var i = command.Index.Value + 1; i < count; i++)
                    {
                        var durl = command.ProjectId.ToKotoriDocumentUri(command.DocumentType, command.DocumentTypeId, command.DocumentId, i);
                        var d    = await FindDocumentByIdAsync(command.Instance, projectUri, durl, null).ConfigureAwait(false);

                        if (d != null)
                        {
                            reindexTasks.Add(ReindexDocumentAsync(d, i - 1));
                        }
                    }

                    Task.WaitAll(reindexTasks.ToArray());
                }

                if (result)
                {
                    return(new CommandResult());
                }
            }
            else
            {
                if (await DeleteDocumentAsync(document).ConfigureAwait(false))
                {
                    var sql = DocumentDbHelpers.CreateDynamicQueryForDocumentSearch
                              (
                        command.Instance,
                        projectUri,
                        documentTypeUri,
                        null,
                        "count(1) as number",
                        null,
                        null,
                        true,
                        true
                              );

                    return(new CommandResult());
                }
            }

            throw new KotoriDocumentException(command.DocumentId, "Document has not been deleted.");
        }
    }
}
 private string GetFullDocumentId <T>(string documentId)
 {
     return(DocumentDbHelpers.GetFullDocumentId <T>(documentId));
 }
Ejemplo n.º 5
0
        async Task <CommandResult <ComplexCountResult <SimpleDocument> > > HandleAsync(FindDocuments command)
        {
            var projectUri      = command.ProjectId.ToKotoriProjectUri();
            var documentTypeUri = command.ProjectId.ToKotoriDocumentTypeUri(command.DocumentType, command.DocumentTypeId);

            var project = await FindProjectAsync(command.Instance, projectUri).ConfigureAwait(false);

            if (project == null)
            {
                throw new KotoriProjectException(command.ProjectId, "Project does not exist.")
                      {
                          StatusCode = System.Net.HttpStatusCode.NotFound
                      }
            }
            ;

            var top = command.Top;

            if (command.Skip.HasValue &&
                command.Top.HasValue)
            {
                top = command.Skip.Value + command.Top.Value;
            }

            var sql = DocumentDbHelpers.CreateDynamicQueryForDocumentSearch
                      (
                command.Instance,
                projectUri,
                documentTypeUri,
                top,
                command.Select,
                command.Filter,
                command.OrderBy,
                command.Drafts,
                command.Future
                      );

            var documents = await GetDocumentsAsync(sql).ConfigureAwait(false);

            var simpleDocuments = documents.Select(d => new SimpleDocument
                                                   (
                                                       d.Identifier != null ? new Uri(d.Identifier).ToKotoriDocumentIdentifier().DocumentId : null,
                                                       d.Slug,
                                                       d.Meta,
                                                       DocumentHelpers.PostProcessedContent(d.Content, d.Meta, command.Format),
                                                       d.Date?.DateTime,
                                                       d.Modified?.DateTime,
                                                       d.Draft,
                                                       d.Version
                                                   ));

            if (command.Skip.HasValue)
            {
                var skip = command.Skip.Value;

                if (skip >= simpleDocuments.Count())
                {
                    return(new CommandResult <ComplexCountResult <SimpleDocument> >(new ComplexCountResult <SimpleDocument>(0, new List <SimpleDocument>())));
                }

                if (top.HasValue)
                {
                    simpleDocuments = simpleDocuments.Skip(skip).Take(top.Value);
                }
                else
                {
                    simpleDocuments = simpleDocuments.Skip(skip);
                }
            }

            var count = simpleDocuments.Count();
            var finalSimpleDocuments = simpleDocuments.Take(Constants.MaxDocuments);

            return(new CommandResult <ComplexCountResult <SimpleDocument> >(new ComplexCountResult <SimpleDocument>(count, finalSimpleDocuments)));
        }
    }
Ejemplo n.º 6
0
        async Task <CommandResult> HandleAsync(DeleteDocumentType command)
        {
            var projectUri = command.ProjectId.ToKotoriProjectUri();
            var project    = await FindProjectAsync(command.Instance, projectUri).ConfigureAwait(false);

            var documentTypeUri = command.ProjectId.ToKotoriDocumentTypeUri(command.DocumentType, command.DocumentTypeId);

            if (project == null)
            {
                throw new KotoriProjectException(command.ProjectId, "Project not found.")
                      {
                          StatusCode = System.Net.HttpStatusCode.NotFound
                      }
            }
            ;

            var docType = await FindDocumentTypeByIdAsync
                          (
                command.Instance,
                projectUri,
                documentTypeUri
                          ).ConfigureAwait(false);

            if (docType == null)
            {
                throw new KotoriDocumentTypeException(command.DocumentTypeId, "Document type not found.")
                      {
                          StatusCode = System.Net.HttpStatusCode.NotFound
                      }
            }
            ;

            var sql = DocumentDbHelpers.CreateDynamicQueryForDocumentSearch
                      (
                command.Instance,
                projectUri,
                documentTypeUri,
                null,
                "count(1) as number",
                null,
                null,
                true,
                true
                      );

            var count = await CountDocumentsAsync(sql).ConfigureAwait(false);

            if (count > 0)
            {
                throw new KotoriDocumentTypeException(command.DocumentTypeId, $"{count} document{(count > 1 ? "s" : "")} found of this document type.")
                      {
                          StatusCode = System.Net.HttpStatusCode.NotFound
                      }
            }
            ;

            await DeleteDocumentTypeAsync(docType.Id).ConfigureAwait(false);

            return(new CommandResult());
        }
    }
}
Ejemplo n.º 7
0
        public async Task <OperationResult> UpsertDocumentAsync(IUpsertDocument command)
        {
            var projectUri      = command.ProjectId.ToKotoriProjectUri();
            var documentTypeUri = command.ProjectId.ToKotoriDocumentTypeUri(command.DocumentType, command.DocumentTypeId);
            var documentUri     = command.ProjectId.ToKotoriDocumentUri(command.DocumentType, command.DocumentTypeId, command.DocumentId, command.Index);

            var project = await FindProjectAsync(command.Instance, projectUri).ConfigureAwait(false);

            if (project == null)
            {
                throw new KotoriProjectException(command.ProjectId, "Project does not exist.")
                      {
                          StatusCode = System.Net.HttpStatusCode.NotFound
                      }
            }
            ;

            if (command.DocumentType == Enums.DocumentType.Content)
            {
                var result = await UpsertDocumentHelperAsync
                             (
                    command.CreateOnly,
                    command.Instance,
                    command.ProjectId,
                    command.DocumentType,
                    command.DocumentTypeId,
                    command.DocumentId,
                    command.Index,
                    command.Content,
                    command.Date,
                    command.Draft
                             ).ConfigureAwait(false);

                return(result);
            }

            if (command.DocumentType == Enums.DocumentType.Data)
            {
                var idx       = command.Index;
                var data      = new Data(command.DocumentId, command.Content);
                var documents = data.GetDocuments();

                var sql = DocumentDbHelpers.CreateDynamicQueryForDocumentSearch
                          (
                    command.Instance,
                    projectUri,
                    documentTypeUri,
                    null,
                    "count(1) as number",
                    null,
                    null,
                    true,
                    true
                          );

                var count = await CountDocumentsAsync(sql).ConfigureAwait(false);

                if (idx == null)
                {
                    idx = count;
                }

                if (idx > count)
                {
                    throw new KotoriDocumentException(command.DocumentId, $"When creating data document at a particular index, your index must be 0 - {count}.");
                }

                OperationResult lastResult = null;

                for (var dc = 0; dc < documents.Count; dc++)
                {
                    var jo  = JObject.FromObject(documents[dc]);
                    var dic = jo.ToObject <Dictionary <string, object> >();
                    var doc = Markdown.ConstructDocument(dic, null);

                    var finalIndex = idx == null ? dc : idx + dc;

                    lastResult = await UpsertDocumentHelperAsync(
                        command.CreateOnly,
                        command.Instance,
                        command.ProjectId,
                        command.DocumentType,
                        command.DocumentTypeId,
                        command.DocumentId,
                        finalIndex,
                        doc,
                        command.Date,
                        command.Draft
                        );
                }

                return(lastResult);
            }

            throw new KotoriDocumentException(command.DocumentId, "Unknown document type.");
        }

        async Task <OperationResult> UpsertDocumentHelperAsync(bool createOnly, string instance, string projectId, Enums.DocumentType documentType, string documentTypeId, string documentId, long?index, string content, DateTime?date, bool?draft)
        {
            var projectUri      = projectId.ToKotoriProjectUri();
            var documentTypeUri = projectId.ToKotoriDocumentTypeUri(documentType, documentTypeId);
            var documentUri     = projectId.ToKotoriDocumentUri(documentType, documentTypeId, documentId, index);
            var documentType2   = await FindDocumentTypeAsync(instance, projectUri, documentTypeUri).ConfigureAwait(false);

            var transformation  = new Transformation(documentTypeUri.ToKotoriDocumentTypeIdentifier().DocumentTypeId, documentType2?.Transformations);
            var document        = new Markdown(documentUri.ToKotoriDocumentIdentifier(), content, transformation, date, draft);
            var documentTypeId2 = documentTypeUri.ToKotoriDocumentTypeIdentifier();

            IDocumentResult documentResult = null;

            documentResult = document.Process();

            if (documentType == Enums.DocumentType.Content)
            {
                var slug = await FindDocumentBySlugAsync(instance, projectUri, documentResult.Slug, documentUri).ConfigureAwait(false);

                if (slug != null)
                {
                    throw new KotoriDocumentException(documentId, $"Slug '{documentResult.Slug}' is already being used for another document.");
                }
            }

            documentType2 = await UpsertDocumentTypeAsync
                            (
                instance,
                documentTypeId2,
                new UpdateToken <dynamic>(DocumentHelpers.CleanUpMeta(documentResult.Meta), false),
                new UpdateToken <string>(null, true)
                            ).ConfigureAwait(false);

            transformation = new Transformation(documentTypeUri.ToKotoriDocumentTypeIdentifier().DocumentTypeId, documentType2.Transformations);
            document       = new Markdown(documentUri.ToKotoriDocumentIdentifier(), content, transformation, date, draft);
            documentResult = document.Process();

            var d = await FindDocumentByIdAsync(instance, projectUri, documentUri, null).ConfigureAwait(false);

            var isNew = d == null;
            var id    = d?.Id;

            if (!isNew)
            {
                if (createOnly &&
                    documentType == Enums.DocumentType.Content)
                {
                    throw new KotoriDocumentException(documentId, "Document cannot be created. It already exists.");
                }
            }

            long version = 0;

            if (d != null)
            {
                version = d.Version + 1;
            }

            d = new Entities.Document
                (
                instance,
                projectUri.ToString(),
                documentUri.ToString(),
                documentTypeUri.ToString(),
                documentResult.Hash,
                documentResult.Slug,
                documentResult.OriginalMeta,
                documentResult.Meta,
                documentResult.Content,
                documentResult.Date,
                documentResult.Draft,
                version
                )
            {
                Id = id
            };

            var newDocument = await UpsertDocumentAsync(d).ConfigureAwait(false);

            var result = new OperationResult(newDocument, isNew);

            return(result);
        }
    }