public Task Handle(NewUserAdded notification, CancellationToken cancellationToken)
 {
     return(foldersStorage.AddFolder(
                new Folder(
                    FolderIdentity.RootFolderId(notification.AddedUser.Id),
                    "root", null, new List <FolderIdentity>()),
                cancellationToken));
 }
Ejemplo n.º 2
0
        public static Guid GetClientId(this FolderIdentity folderIdentity)
        {
            if (folderIdentity == null)
            {
                throw new ArgumentNullException(nameof(folderIdentity));
            }

            return(folderIdentity.Id);
        }
Ejemplo n.º 3
0
 public async Task <IReadOnlyCollection <Folder> > GetFolders(FolderIdentity parentFolderId, int limit, int skip,
                                                              CancellationToken cancellationToken) =>
 (await context.Folders
  .Where(x => x.ParentId == parentFolderId.Id && x.UserIdentity == parentFolderId.UserIdentity)
  .OrderBy(x => x.Name)
  .Skip(skip)
  .Take(limit)
  .ToArrayAsync(cancellationToken))
 .Select(ToModel)
 .ToArray();
Ejemplo n.º 4
0
        public async Task <PaginationApiResult <IReadOnlyCollection <GetFolderItem> > > GetFolderItems(Guid id, [FromQuery] FolderItemsGetQuery query)
        {
            var currentUser = await userService.GetCurrentUser();

            var parentFolderId = new FolderIdentity(id, currentUser.Id);
            var folders        = await foldersStorage.GetFolders(parentFolderId, query.Limit, query.Skip,
                                                                 CancellationToken.None);

            IReadOnlyCollection <DocumentInfo> documents = new List <DocumentInfo>();

            var foldersCount = await foldersStorage.GetFoldersCount(parentFolderId, CancellationToken.None);

            var totalCount = foldersCount;

            if (!query.OnlyFolders)
            {
                if (folders.Count < query.Limit)
                {
                    var limitForDocuments = query.Limit - folders.Count;
                    var skipForDocuments  = Math.Max(query.Skip - foldersCount, 0);
                    documents = await documentStorage.GetDocuments(currentUser, parentFolderId, limitForDocuments,
                                                                   skipForDocuments, CancellationToken.None);
                }

                totalCount += await documentStorage.GetDocumentsCount(parentFolderId, CancellationToken.None);
            }

            var result = folders
                         .Select(f => new GetFolderItem
            {
                Folder = f.ToDto()
            })
                         .Concat(documents
                                 .Select(d => new GetFolderItem
            {
                Document = new GetDocumentDto
                {
                    Id               = d.Id.GetClientId(),
                    FileName         = d.FileName,
                    ModificationDate = d.ModificationDate,
                    Size             = d.Content.Size
                }
            }))
                         .ToArray();

            return(ApiResult.SuccessPaginationResult(
                       (IReadOnlyCollection <GetFolderItem>)result,
                       new PaginationData
            {
                TotalCount = totalCount
            }));
        }
Ejemplo n.º 5
0
        public FolderId PickCurrentObjInList(FolderIdentity obj, List <FolderIdentity> list)
        {
            FolderId id = null;

            foreach (FolderIdentity item in list)
            {
                if (string.Equals(obj.FolderPath, item.FolderPath, StringComparison.CurrentCultureIgnoreCase))
                {
                    id = item.FolderID;
                }
            }
            return(id);
        }
Ejemplo n.º 6
0
        public async Task RemoveDocumentsFromFolder(FolderIdentity folderId, CancellationToken cancellationToken)
        {
            if (folderId == null)
            {
                throw new ArgumentNullException(nameof(folderId));
            }

            var response = await elasticClient.DeleteByQueryAsync <DocumentInfoModel>(dbqd => dbqd
                                                                                      .Index(options.IndexName)
                                                                                      .Query(qcd => qcd
                                                                                             .Term("parentFoldersPath", folderId.ToString())),
                                                                                      CancellationToken.None);

            CheckResponse(response, $"Error occured during removing items from folder {folderId}");
        }
Ejemplo n.º 7
0
        public async Task <ApiResult <GetFolderDto> > AddFolder(AddFolderDto addFolder)
        {
            var currentUser = await userService.GetCurrentUser();

            var parentFolder = await foldersStorage.GetFolder(
                new FolderIdentity(addFolder.ParentId, currentUser.Id),
                CancellationToken.None);

            var newFolderId = new FolderIdentity(Guid.NewGuid(), currentUser.Id);
            var newFolder   = await foldersStorage.AddFolder(
                new Folder(newFolderId, addFolder.Name, parentFolder.Id,
                           parentFolder.ParentsPath.Concat(parentFolder.Id.AsArray()).ToList()),
                CancellationToken.None);

            return(ApiResult.SuccessResultWithData(newFolder.ToDto()));
        }
Ejemplo n.º 8
0
        public async Task <int> GetDocumentsCount(FolderIdentity folderId, CancellationToken cancellationToken)
        {
            if (folderId == null)
            {
                throw new ArgumentNullException(nameof(folderId));
            }

            var response = await elasticClient.CountAsync <DocumentInfoModel>(
                cd => cd
                .Index(options.IndexName)
                .Query(qcd => qcd
                       .Term("folderId", folderId.ToString())),
                cancellationToken);

            CheckResponse(response, "Error during getting count documents in folder");

            return((int)response.Count);
        }
Ejemplo n.º 9
0
        public async Task <IReadOnlyCollection <DocumentInfo> > GetDocuments(User user, FolderIdentity parentFolderId,
                                                                             int limit, int skip, CancellationToken cancellationToken)
        {
            var response = await elasticClient.SearchAsync <DocumentInfoModel>(
                sd => sd
                .Index(options.IndexName)
                .Query(qd => qd
                       .Bool(bqd => bqd
                             .Must(
                                 qcd => qcd.Term("id.userIdentity.keyword", user.Id),
                                 qcd => qcd.Term("folderId", parentFolderId.ToString()))))
                .Source(sfd => sfd.Excludes(fd => fd.Field("text")))
                .Take(limit)
                .Skip(skip),
                cancellationToken);

            return(response.Hits.Select(x => ToDocumentInfo(x.Source, cancellationToken)).ToArray());
        }
Ejemplo n.º 10
0
 public Task <int> GetFoldersCount(FolderIdentity parentFolderId, CancellationToken cancellationToken) =>
 context.Folders.CountAsync(x => x.ParentId == parentFolderId.Id && x.UserIdentity == parentFolderId.UserIdentity, cancellationToken);
Ejemplo n.º 11
0
 public async Task <Folder> GetFolder(FolderIdentity folderId, CancellationToken cancellationToken) =>
 ToModel(await context.Folders.FindAsync(new object[] { folderId.Id, folderId.UserIdentity }, cancellationToken));