Beispiel #1
0
        public async Task <List <Category> > GetCategoriesWithPosts(SearchCategoryOptions searchCategoryOption)
        {
            var allCategories = await _dbContext.Categories.Include(p => p.Posts).ToListAsync();

            switch (searchCategoryOption)
            {
            case SearchCategoryOptions.All:
                return(allCategories);

            case SearchCategoryOptions.FirstBestThisMonth:
            {
                DateTime d = DateTime.Now;
                allCategories = allCategories.Where(p => p.Posts.Any(p => (p.Date.Month == d.Month && p.Date.Year == d.Date.Year))).ToList();

                //for every category I'm creating a list and add one best post (list because this is the type where category keeps posts in its property)
                return(GetCategoriesWithTheBestPost(allCategories));
            }

            case SearchCategoryOptions.FirstBestThisTime:
            {
                return(GetCategoriesWithTheBestPost(allCategories));
            }

            default:
                return(null);
            }
        }
        public async Task <ActionResult <List <CategoryPostListViewModel> > > GetCategoriesWithPosts
            (SearchCategoryOptions searchOptions)
        {
            GetCategoriesWithPostListQuery getCategoriesListWithPostsQuery =
                new GetCategoriesWithPostListQuery()
            {
                searchCategory = searchOptions
            };

            var dtos = await _mediator.Send(getCategoriesListWithPostsQuery);

            return(Ok(dtos));
        }
        public async Task <List <Category> > GetCategoriesWithPost(SearchCategoryOptions searchCategory)
        {
            var allCategories = await _dbContext.Categories.Include(p => p.Posts).ToListAsync();

            if (searchCategory == SearchCategoryOptions.FirstBestAllTheTime)
            {
                foreach (var c in allCategories)
                {
                    Post max = null;
                    foreach (var p in c.Posts)
                    {
                        if (max == null)
                        {
                            max = p;
                            break;
                        }

                        if (max.Rate < p.Rate)
                        {
                            max = p;
                        }
                    }
                    c.Posts = new List <Post>();
                    if (max != null)
                    {
                        c.Posts.Add(max);
                    }
                }

                return(allCategories);
            }
            else if (searchCategory == SearchCategoryOptions.FirstBestThisMonth)
            {
                DateTime d = DateTime.Now;

                allCategories = allCategories.Where(c =>
                                                    c.Posts.Any(p => (p.Date.Month == d.Month && d.Year == p.Date.Year)))
                                .ToList();

                foreach (var c in allCategories)
                {
                    Post max = null;
                    foreach (var p in c.Posts)
                    {
                        if (max == null)
                        {
                            max = p;
                            break;
                        }

                        if (max.Rate < p.Rate)
                        {
                            max = p;
                        }
                    }
                    c.Posts = new List <Post>();
                    if (max != null)
                    {
                        c.Posts.Add(max);
                    }
                }

                return(allCategories);
            }


            return(allCategories);
        }
Beispiel #4
0
        public async Task <ActionResult <List <CategoryWithPostsInListViewModel> > > GetCategoriesWithPosts(SearchCategoryOptions searchCategoryOption)
        {
            var getCategoriesListWithPostsQuery = new GetCategoriesListWithPostsQuery()
            {
                SearchCategoryOption = searchCategoryOption
            };

            var result = await _mediator.Send(getCategoriesListWithPostsQuery);

            return(Ok(result));
        }
        public async Task <List <CategoryWithPostsBlazorVM> > GetAllCategoriesWithPosts(SearchCategoryOptions searchCategoryOptions)
        {
            var allCategories = await _client.GetCategoriesWithPostsAsync(searchCategoryOptions);

            var mappedCategories = _mapper.Map <ICollection <CategoryWithPostsBlazorVM> >(allCategories);

            return(mappedCategories.ToList());
        }