Example #1
0
        public IActionResult Authors([FromServices] INewsRepository newsRepository, [FromQuery] int humanId)
        {
            if (humanId == 0)
            {
                var humans     = _repository.GetAllHumans();
                var viewModels = new List <HumanAuthorsViewModel>();

                foreach (var human in humans)
                {
                    var viewModel = new HumanAuthorsViewModel();
                    viewModel.FirstName = human.FirstName;
                    viewModel.LastName  = human.LastName;
                    viewModel.AuthorId  = human.Id;
                    viewModel.NewsCount = newsRepository.GetAllNews().Count(news => news.AuthorId == human.Id);

                    viewModels.Add(viewModel);
                }

                return(View(viewModels));
            }
            else
            {
                var human      = _repository.GetHuman(humanId);
                var viewModels = new List <HumanAuthorsViewModel>();
                var viewModel  = new HumanAuthorsViewModel();
                viewModel.FirstName = human[0].FirstName;
                viewModel.LastName  = human[0].LastName;
                viewModel.NewsCount = newsRepository.GetAllNews().Count(news => news.AuthorId == human[0].Id);

                viewModels.Add(viewModel);


                return(View(viewModels));
            }
        }
Example #2
0
        public IActionResult Authors([FromServices] INewsRepository newsRepository, int AuthorsId = 0)
        {
            var humans     = _iHumanRepository.GetAllHumans().ToList();
            var news       = newsRepository.GetAllNews().ToList();
            var viewModels = new List <HumanAuthorsViewModel>();

            if (AuthorsId != 0)
            {
                var human     = humans.FirstOrDefault(_human => _human.Id == AuthorsId);
                var viewModel = new HumanAuthorsViewModel();
                viewModel.AuthorId  = human.Id;
                viewModel.FirstName = human.FirstName;
                viewModel.LastName  = human.LastName;

                viewModel.NewsCount = news.Count(news => news.AuthorId == human.Id);

                viewModels.Add(viewModel);


                return(View(viewModels));
            }

            foreach (var human in humans)
            {
                var viewModel = new HumanAuthorsViewModel();
                viewModel.AuthorId  = human.Id;
                viewModel.FirstName = human.FirstName;
                viewModel.LastName  = human.LastName;

                viewModel.NewsCount = news.Count(news => news.AuthorId == human.Id);

                viewModels.Add(viewModel);
            }

            return(View(viewModels));
        }