public async Task <PagedOperationResult <List <Presentation> > > UserPresentationsFromCategory(string userId, string categoryName, PagingOptions options)
        {
            var result           = new PagedOperationResult <List <Presentation> >();
            var categoryIdResult = await GetIdOfCategoryWithName(categoryName);

            if (categoryIdResult.ErrorMessageIfAny != null)
            {
                result.ErrorMessageIfAny = categoryIdResult.ErrorMessageIfAny;
                return(result);
            }
            var catId = categoryIdResult.Value;

            var userPresentations = await _context.UserPresentations.Where(u => u.UserId == userId).
                                    Include(u => u.Presentation).Where(u => u.Presentation.CategoryId == catId)
                                    .Select(u => u.Presentation)
                                    .Skip(options.ToSkip).Take(options.ItemsPerPage)
                                    .ToListAsync();


            var count = _context.UserPresentations.Where(u => u.UserId == userId).Include(u => u.Presentation).Count(u =>
                                                                                                                     u.Presentation.CategoryId == catId);

            result.Value      = userPresentations;
            result.TotalPages = count / options.ItemsPerPage;
            if (result.TotalPages == 0)
            {
                result.TotalPages++;
            }
            return(result);
        }
Beispiel #2
0
        public async Task <PagedOperationResult <List <User> > > GetUsersForPresentation(int presentationId, PagingOptions options)
        {
            var result           = new PagedOperationResult <List <User> >();
            var presentationList = await _context.Presentations.Where(p => p.Id == presentationId).ToListAsync();

            if (presentationList.Count == 0)
            {
                result.ErrorMessageIfAny = "No such presentation found";
                return(result);
            }

            if (presentationList.First().IsPublic == false)
            {
                result.ErrorMessageIfAny = "The presentation cannot be accessed because it is private";
                return(result);
            }

            var count  = _context.UserPresentations.Count(up => up.PresentationId == presentationId);
            var upList = await _context.UserPresentations.Where(up => up.PresentationId == presentationId).Include(up => up.User).Select(up => up.User)
                         .Skip(options.ToSkip).Take(options.ItemsPerPage).ToListAsync();

            result.TotalPages = count / options.ItemsPerPage;
            result.Value      = upList;

            return(result);
        }
        public async Task <PagedOperationResult <List <Presentation> > > GetPresentationsForUser(string userId, PagingOptions options)
        {
            var toSkip = (options.PageIndex - 1) * options.ItemsPerPage;
            var toTake = options.ItemsPerPage;

            var count      = _context.UserPresentations.Count(up => up.UserId == userId);
            var totalPages = (int)(Math.Ceiling((float)count / options.ItemsPerPage));

            var upList = await _context.UserPresentations.Where(up => up.UserId == userId).Include(up => up.Presentation)
                         .Select(up => up.Presentation).Skip(toSkip).Take(toTake).ToListAsync();


            var result = new PagedOperationResult <List <Presentation> >
            {
                Value        = upList,
                TotalPages   = totalPages,
                ItemsPerPage = options.ItemsPerPage
            };

            if (result.TotalPages == 0)
            {
                result.TotalPages++;
            }
            return(result);
        }
        public async Task <PagedOperationResult <List <Presentation> > > PublicPresentationsForUser(string userId, PagingOptions options, string excludeUserId)
        {
            var result = new PagedOperationResult <List <Presentation> >();

            var upList = _context.UserPresentations.Where(u => u.UserId == userId).Include(u => u.Presentation)
                         .Where(u => u.Presentation.IsPublic);

            if (excludeUserId != null)
            {
                upList = upList.Where(u => !_context.UserPresentations.Any(up => up.UserId == excludeUserId && up.PresentationId == u.PresentationId));
            }

            var count = upList.Count();



            var list = await upList.Select(up => up.Presentation).
                       Skip(options.ToSkip).Take(options.ItemsPerPage).
                       ToListAsync();


            result.Value      = list;
            result.TotalPages = count / options.ItemsPerPage;
            if (result.TotalPages == 0)
            {
                result.TotalPages++;
            }
            return(result);
        }
        public async Task <PagedOperationResult <List <Presentation> > > PublicPresentations(PagingOptions options, string excludeUserIdIfAny)
        {
            var count = _context.Presentations.Count(p => p.IsPublic);
            List <Presentation> presentations;

            if (excludeUserIdIfAny != null)
            {
                presentations = await _context.Presentations.Where(p => p.IsPublic)
                                .Include(p => p.UserPresentations)
                                .Where(p => !p.UserPresentations.Any(up => up.UserId == excludeUserIdIfAny)).ToListAsync();
            }
            else
            {
                presentations = await _context.Presentations.Where(p => p.IsPublic).Skip(options.ToSkip).Take(options.ItemsPerPage)
                                .ToListAsync();
            }


            var result = new PagedOperationResult <List <Presentation> >
            {
                Value        = presentations,
                TotalPages   = (int)Math.Ceiling((float)count / options.ItemsPerPage),
                ItemsPerPage = options.ItemsPerPage
            };

            if (result.TotalPages == 0)
            {
                result.TotalPages++;
            }
            return(result);
        }
        public async Task <PagedOperationResult <List <Presentation> > > GetUserPresentationsFromTag(string tag, string userId, PagingOptions options)
        {
            var count = _context.UserPresentations.Where(u => u.UserId == userId)
                        .Include(u => u.Presentation)
                        .Select(u => u.Presentation)
                        .Include(p => p.PresentationTags)
                        .Count(p => p.PresentationTags.Any(pt => pt.Tag.Name == tag));

            var upList = await _context.UserPresentations.Where(u => u.UserId == userId)
                         .Include(u => u.Presentation)
                         .Select(u => u.Presentation)
                         .Include(p => p.PresentationTags)
                         .Where(p => p.PresentationTags.Any(pt => pt.Tag.Name == tag))
                         .Skip(options.ToSkip)
                         .Take(options.ItemsPerPage)
                         .ToListAsync();

            var result = new PagedOperationResult <List <Presentation> >
            {
                Value        = upList,
                ItemsPerPage = options.ItemsPerPage,
                TotalPages   = (int)(Math.Ceiling((float)count / options.ItemsPerPage))
            };

            if (result.TotalPages == 0)
            {
                result.TotalPages++;
            }
            return(result);
        }
        public async Task <PagedOperationResult <List <Presentation> > > GetUserPresentationsFromCategory(string categoryName, string userId, PagingOptions options)
        {
            var count = _context.UserPresentations.Where(up => up.UserId == userId)
                        .Include(up => up.Presentation)
                        .Select(up => up.Presentation)
                        .Include(p => p.Category)
                        .Where(p => p.Category.Name == categoryName).Count();

            var upList = await _context.UserPresentations.Where(up => up.UserId == userId)
                         .Include(up => up.Presentation)
                         .Select(up => up.Presentation)
                         .Include(p => p.Category)
                         .Where(p => p.Category.Name == categoryName)
                         .Skip(options.ToSkip)
                         .Take(options.ItemsPerPage)
                         .ToListAsync();


            var pages = Math.Ceiling((float)count / options.ItemsPerPage);

            var result = new PagedOperationResult <List <Presentation> >
            {
                Value        = upList,
                ItemsPerPage = options.ItemsPerPage,
                TotalPages   = (int)pages
            };

            if (result.TotalPages == 0)
            {
                result.TotalPages++;
            }
            return(result);
        }
        public async Task <IActionResult> SearchPublicPresentations(string keywords, string where, int?page, int?itemsPerPage)
        {
            var vm = new PresentationsViewModel();

            vm.NavbarIndexPair = defaultNavbarIndexPair;

            if (keywords == null || keywords.Length == 0)
            {
                vm.TopMessage = "You provided no keywords to search with";
                return(base.DisplayListPage(vm));
            }

            string excludeUserId = null;

            if (this.User != null && this.User.Identity.IsAuthenticated)
            {
                excludeUserId = _userManager.GetUserId(this.User);
            }

            _userManager.GetUserId(User);
            var pagingOptions = PagingOptions.CreateWithTheseOrDefaults(page, itemsPerPage);
            var searchType    = CreateSearchType(where);
            var keywordsList  = CreateKeywordsList(keywords);

            PagedOperationResult <List <Presentation> > presentations = await _presentationsRepository.
                                                                        SearchPublicPresentations(keywordsList, pagingOptions, searchType, excludeUserId);

            if (presentations.ErrorMessageIfAny != null)
            {
                vm.ErrorMessage = presentations.ErrorMessageIfAny;
                return(base.DisplayListPage(vm));
            }

            if (presentations.Value.Count == 0)
            {
                vm.TopMessage = "The search returned no results for the keywords " + keywords;
                return(base.DisplayListPage(vm));
            }


            vm.TopMessage    = $"Search results for the keywords \"{keywords}\" in public presentations";
            vm.Presentations = await base.CreateCardsModel(presentations.Value);

            vm.PaginationModel = await CreateSearchPaginationModel(keywordsList, searchType, presentations.TotalPages, pagingOptions);

            return(DisplayPublicListPage(vm));
        }
        public async Task <PagedOperationResult <List <Presentation> > > PublicPresentationsFromCategory(string categoryName,
                                                                                                         PagingOptions options, string excludeUserId)
        {
            var result           = new PagedOperationResult <List <Presentation> >();
            var categoryIdResult = await GetIdOfCategoryWithName(categoryName);

            if (categoryIdResult.ErrorMessageIfAny != null)
            {
                result.ErrorMessageIfAny = categoryIdResult.ErrorMessageIfAny;
                return(result);
            }
            var catId = categoryIdResult.Value;

            var presentationSource = _context.Presentations.Where(p => p.CategoryId == catId && p.IsPublic);

            if (excludeUserId != null)
            {
                presentationSource = presentationSource.Where(p => !_context.UserPresentations.Any(u => u.UserId == excludeUserId &&
                                                                                                   u.PresentationId == p.Id));
            }

            var count         = presentationSource.Count();
            var presentations = await presentationSource
                                .Skip(options.ToSkip).Take(options.ItemsPerPage)
                                .ToListAsync();

            result = new PagedOperationResult <List <Presentation> >
            {
                Value      = presentations,
                TotalPages = count / options.ItemsPerPage
            };
            if (result.TotalPages == 0)
            {
                result.TotalPages++;
            }
            return(result);
        }