Esempio n. 1
0
        public static void Equal(ArticleListDTO expected, ArticleListDTO actual)
        {
            Equal(expected.Limit, actual.Limit);
            Equal(expected.Count, actual.Count);
            Equal(expected.Page, actual.Page);
            Equal(expected.PageCount, actual.PageCount);
            True(expected.Articles.Count == actual.Articles.Count);
            for (int i = 0; i < expected.Articles.Count; i++)
            {
                Equal(expected.Articles[i], actual.Articles[i]);
            }

            //Equal(expected.Articles, actual.Articles);
        }
Esempio n. 2
0
        public async void GetArticlesOverPageTest()
        {
            var controller = new ArticlesController(_context, _userManager);
            var result     = await controller.GetArticles(2, null);

            // Assert
            var objectResult = Assert.IsType <OkObjectResult>(result);
            var model        = Assert.IsAssignableFrom <ArticleListDTO>(objectResult.Value);

            var emptyDTO = new ArticleListDTO()
            {
                Limit     = 20,
                Count     = _articleDTOs.Count,
                PageCount = 1,
                Page      = 2,
                Articles  = new List <ArticleListElemDTO>()
            };

            Assert.Equal(emptyDTO, model);
        }
Esempio n. 3
0
        public NewsPortalTest()
        {
            var options = new DbContextOptionsBuilder <NewsContext>()
                          .UseInMemoryDatabase("NewsPortalTest")
                          .Options;

            _context = new NewsContext(options);
            _context.Database.EnsureCreated();


            _user = new Editor
            {
                UserName    = "******",
                Name        = "Adminisztrátor",
                Email       = "*****@*****.**",
                PhoneNumber = "+36123456789",
                Address     = "Nevesincs utca 1."
            };

            var userList = new List <Editor>();

            userList.Add(_user);

            _userManager   = new FakeUserManager(_context);
            _signInManager = new FakeSignInManager(_userManager);


            var adminPassword = "******";
            var adminRole     = new IdentityRole <int>("administrator");

            var result1 = _userManager.CreateAsync(_user, adminPassword).Result;

            // adatok inicializációja
            var articleData = new List <Article>
            {
                new Article
                {
                    Id            = 1,
                    Name          = "TestArticle",
                    Lead          = "This is a test article",
                    Content       = "This is REALLY just a test article",
                    CreatedAt     = DateTime.Now,
                    HighlightedAt = DateTime.Now,
                    PublishedAt   = DateTime.Now,
                    Author        = _user,
                    Images        = new List <ArticleImage>()
                    {
                        new ArticleImage()
                        {
                            Name  = "TEst image",
                            Image = Convert.FromBase64String(testImg)
                        }
                    }
                },
                new Article
                {
                    Id            = 2,
                    Name          = "TestArticle 2",
                    Lead          = "This is a test article too",
                    Content       = "This is REALLY just a test article too",
                    CreatedAt     = DateTime.Now.AddDays(-1),
                    HighlightedAt = null,
                    PublishedAt   = null,
                    Author        = _user,
                    Images        = new List <ArticleImage>()
                    {
                        new ArticleImage()
                        {
                            Name  = "TEst image 2",
                            Image = Convert.FromBase64String(testImg)
                        }
                    }
                },
                new Article
                {
                    Id            = 3,
                    Name          = "TestArticle 2",
                    Lead          = "This is a test article too",
                    Content       = "This is REALLY just a test article too",
                    CreatedAt     = DateTime.Now.AddDays(-2),
                    HighlightedAt = null,
                    PublishedAt   = DateTime.Now.AddDays(-2),
                    Author        = _user,
                    Images        = new List <ArticleImage>()
                    {
                        new ArticleImage()
                        {
                            Name  = "TEst image 2",
                            Image = Convert.FromBase64String(testImg)
                        }
                    }
                }
            };

            _context.Articles.AddRange(articleData);

            _context.SaveChanges();

            _articleDTOs = articleData.Select(a => new ArticleDTO()
            {
                Id            = a.Id,
                Name          = a.Name,
                Lead          = a.Lead,
                Content       = a.Content,
                PublishedAt   = a.PublishedAt,
                HighlightedAt = a.HighlightedAt,
                CreatedAt     = a.CreatedAt,
                Images        = a.Images.Select(i => new ImageDTO()
                {
                    Id     = i.Id,
                    Name   = i.Name,
                    Base64 = Convert.ToBase64String(i.Image)
                }).ToList(),
                Author        = a.Author.Name,
                IsHighlighted = a.IsHighlighted,
                IsPublished   = a.IsPublished
            }).ToList();

            _articleListDTO = new ArticleListDTO()
            {
                Page      = 1,
                PageCount = 1,
                Count     = _articleDTOs.Count,
                Limit     = 20,
                Articles  = _articleDTOs.Select(a => new ArticleListElemDTO()
                {
                    Id            = a.Id,
                    Name          = a.Name,
                    Lead          = a.Lead,
                    Author        = a.Author,
                    CreatedAt     = a.CreatedAt,
                    HighlightedAt = a.HighlightedAt,
                    ImageCount    = a.Images.Count,
                    IsHighlighted = a.IsHighlighted,
                    IsPublished   = a.IsPublished,
                    PublishedAt   = a.PublishedAt
                }).ToList()
            };
        }
Esempio n. 4
0
        public async Task <IActionResult> GetArticles(int page, [FromQuery] SearchParameters parameters)
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(Unauthorized());
            }

            if (parameters == null)
            {
                parameters = new SearchParameters();
            }

            if (page == 0)
            {
                page = 1;
            }

            ArticleSearchType searchType = 0;

            if (parameters.SearchTitle)
            {
                searchType = searchType | ArticleSearchType.Name;
            }
            if (parameters.SearchLead)
            {
                searchType = searchType | ArticleSearchType.Lead;
            }
            if (parameters.SearchContent)
            {
                searchType = searchType | ArticleSearchType.Content;
            }


            ArticleApiOptions options = new ArticleApiOptions
            {
                Offset        = (page - 1) * LIST_LIMIT,
                Limit         = LIST_LIMIT,
                ArticleType   = ArticleType.All,
                IncludeAuthor = true,
                SearchType    = searchType,
                SearchString  = parameters.Search,
                DateStart     = parameters.DateStart,
                DateEnd       = parameters.DateEnd
            };

            if (options.DateEnd.HasValue)
            {
                options.DateEnd = options.DateEnd.Value.AddDays(1).Date; //Add one day to include DateEnd in search
            }
            if (options.DateStart.HasValue)
            {
                options.DateStart = options.DateStart.Value.Date; //Set DateTime to midnight;
            }


            //_newsService.GetArticles(options);

            IQueryable <Article> query = _context.Articles;

            query = RestrictArticleType(query, options.ArticleType.Value);
            query = RestrictDate(query, options);
            query = RestrictSearch(query, options);

            if (options.IncludeAuthor.HasValue && options.IncludeAuthor.Value)
            {
                query = query.Include(a => a.Author);
            }

            query = query.Where(a => a.Author == user);


            int count = query.Count();

            query = query.OrderByDescending(a => a.CreatedAt);

            if (options.Offset.HasValue && options.Offset.Value > 0)
            {
                query = query.Skip(options.Offset.Value);
            }

            if (options.Limit.HasValue && options.Limit.Value > 0)
            {
                query = query.Take(options.Limit.Value);
            }

            int pageCount = options.Limit.Value > 0 ? ((count - 1) / options.Limit.Value) + 1 : 1;

            List <ArticleListElemDTO> list = query.Select <Article, ArticleListElemDTO>(x => new ArticleListElemDTO
            {
                Id            = x.Id,
                Name          = x.Name,
                Lead          = x.Lead,
                ImageCount    = x.Images.Count(),
                Author        = x.Author.Name,
                PublishedAt   = x.PublishedAt,
                IsPublished   = x.IsPublished,
                IsHighlighted = x.IsHighlighted,
                HighlightedAt = x.HighlightedAt,
                CreatedAt     = x.CreatedAt
            }
                                                                                        ).ToList();

            ArticleListDTO result = new ArticleListDTO {
                Limit     = options.Limit.Value,
                Page      = page,
                Count     = count,
                PageCount = pageCount,
                Articles  = list
            };

            return(Ok(result));
        }