Exemplo n.º 1
0
 public IActionResult ViewAnnualReports()
 {
     try
     {
         var userId = _userManager.GetUserId(User);
         var cities = _cityAccessManager.GetCities(userId);
         var annualReports = _repoWrapper.AnnualReports
             .FindAll()
             .Include(ar => ar.City)
                 .ThenInclude(c => c.Region)
             .Include(ar => ar.User)
             .ToList();
         annualReports.RemoveAll(ar => !cities.Any(c => c.ID == ar.CityId));
         var viewAnnualReportsViewModel = new ViewAnnualReportsViewModel
         {
             AnnualReports = annualReports,
             Cities = _viewAnnualReportsVMInitializer.GetCities(cities)
         };
         return View(viewAnnualReportsViewModel);
     }
     catch (Exception e)
     {
         _logger.LogError($"Exception: {e.Message}");
         return RedirectToAction("HandleError", "Error", new { code = 500 });
     }
 }
Exemplo n.º 2
0
        public async Task GetAllAsyncCorrect()
        {
            // Arrange
            var cities = new List <CityViewModel>
            {
                new CityViewModel {
                    ID = 1, Name = "Львів"
                }
            };
            var citiesVMs = new List <CityVMs.CityViewModel>
            {
                new CityVMs.CityViewModel {
                    ID = 1, Name = "Львів"
                }
            };
            var annualReports = new List <AnnualReportViewModel>
            {
                new AnnualReportViewModel {
                    ID = 1, CityId = cities.First().ID, City = cities.First(), UserId = "1", Date = DateTime.Now
                }
            };
            var expectedViewModel = new ViewAnnualReportsViewModel(citiesVMs)
            {
                Cities = new List <SelectListItem>
                {
                    new SelectListItem {
                        Value = cities.First().ID.ToString(), Text = cities.First().Name
                    }
                },
                AnnualReports = annualReports
            };

            _mapper.Setup(m => m.Map <IEnumerable <CityDTOs.CityDTO>, IEnumerable <CityVMs.CityViewModel> >(It.IsAny <IEnumerable <CityDTOs.CityDTO> >()))
            .Returns(citiesVMs);
            _mapper.Setup(m => m.Map <IEnumerable <AnnualReportDTO>, IEnumerable <AnnualReportViewModel> >(It.IsAny <IEnumerable <AnnualReportDTO> >()))
            .Returns(annualReports);

            // Act
            var result = await controller.GetAll();

            // Assert
            var viewResult      = Assert.IsType <ViewResult>(result);
            var actualViewModel = Assert.IsType <ViewAnnualReportsViewModel>(viewResult.Model);

            Assert.Equal(JsonConvert.SerializeObject(expectedViewModel), JsonConvert.SerializeObject(actualViewModel));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> GetAll()
        {
            try
            {
                var citiesDTO = await _cityAccessService.GetCitiesAsync(User);

                var annualReportsDTO = await _annualReportService.GetAllAsync(User);

                var cities        = _mapper.Map <IEnumerable <CityDTOs.CityDTO>, IEnumerable <CityVMs.CityViewModel> >(citiesDTO);
                var annualReports = _mapper.Map <IEnumerable <AnnualReportDTO>, IEnumerable <AnnualReportViewModel> >(annualReportsDTO);
                var viewAnnualReportsViewModel = new ViewAnnualReportsViewModel(cities)
                {
                    AnnualReports = annualReports
                };
                return(View(viewAnnualReportsViewModel));
            }
            catch (Exception e)
            {
                _loggerService.LogError($"Exception: {e.Message}");
                return(RedirectToAction("HandleError", "Error", new { code = StatusCodes.Status500InternalServerError }));
            }
        }