public IActionResult EditAnnualReport(int id) { try { var annualReport = _repoWrapper.AnnualReports .FindByCondition(ar => ar.ID == id && ar.Status == AnnualReportStatus.Unconfirmed) .Include(ar => ar.City) .Include(ar => ar.CityManagement) .Include(ar => ar.MembersStatistic) .First(); var userId = _userManager.GetUserId(User); if (!_cityAccessManager.HasAccess(userId, annualReport.CityId)) { return RedirectToAction("HandleError", "Error", new { code = 403 }); } var cityMembers = _repoWrapper.User .FindByCondition(u => u.CityMembers.Any(cm => cm.City.ID == annualReport.CityId && cm.EndDate == null)) .Include(u => u.UserPlastDegrees); var annualReportVM = new AnnualReportViewModel { Operation = AnnualReportOperation.Editing, CityName = annualReport.City.Name, CityMembers = _annualReportVMCreator.GetCityMembers(cityMembers), CityLegalStatusTypes = _annualReportVMCreator.GetCityLegalStatusTypes(), AnnualReport = annualReport, }; return View("CreateEditAnnualReport", annualReportVM); } catch (Exception e) { _logger.LogError($"Exception: {e.Message}"); return RedirectToAction("HandleError", "Error", new { code = 500 }); } }
public IActionResult CreateAnnualReport(AnnualReport annualReport) { if (!_cityAccessManager.HasAccess(annualReport.UserId, annualReport.CityId)) return RedirectToAction("HandleError", "Error", new { code = 403 }); try { var city = _repoWrapper.City .FindByCondition(c => c.ID == annualReport.CityId) .First(); if (ModelState.IsValid) { var annualReportCheck = _repoWrapper.AnnualReports .FindByCondition(ar => ar.CityId == annualReport.CityId && ar.Date.Year == annualReport.Date.Year) .FirstOrDefault(); if (annualReportCheck == null) { _repoWrapper.AnnualReports.Create(annualReport); _repoWrapper.Save(); ViewData["Message"] = $"Звіт станиці {city.Name} за {annualReport.Date.Year} рік створено!"; } else { ViewData["ErrorMessage"] = $"Звіт станиці {city.Name} за {annualReport.Date.Year} рік вже існує!"; } return View("CreateEditAnnualReport"); } else { var cityMembers = _repoWrapper.User .FindByCondition(u => u.CityMembers.Any(cm => cm.City.ID == annualReport.CityId && cm.EndDate == null)) .Include(u => u.UserPlastDegrees); var annualReportViewModel = new AnnualReportViewModel { Operation = AnnualReportOperation.Creating, CityName = city.Name, CityMembers = _annualReportVMCreator.GetCityMembers(cityMembers), CityLegalStatusTypes = _annualReportVMCreator.GetCityLegalStatusTypes(), AnnualReport = annualReport }; ViewData["ErrorMessage"] = $"Звіт заповнений некоректно!"; return View("CreateEditAnnualReport", annualReportViewModel); } } catch (Exception e) { _logger.LogError($"Exception: {e.Message}"); return RedirectToAction("HandleError", "Error", new { code = 500 }); } }
public IActionResult EditAnnualReport(AnnualReport annualReport) { try { var annualReportCheck = _repoWrapper.AnnualReports .FindByCondition(ar => ar.ID == annualReport.ID && ar.CityId == annualReport.CityId && ar.UserId == annualReport.UserId && ar.Status == AnnualReportStatus.Unconfirmed) .Include(ar => ar.City) .First(); var userId = _userManager.GetUserId(User); if (!_cityAccessManager.HasAccess(userId, annualReport.CityId)) { return RedirectToAction("HandleError", "Error", new { code = 403 }); } if (ModelState.IsValid) { _repoWrapper.AnnualReports.Update(annualReport); _repoWrapper.Save(); ViewData["Message"] = $"Звіт станиці {annualReportCheck.City.Name} за {annualReportCheck.Date.Year} рік відредаговано!"; return View("CreateEditAnnualReport"); } else { var cityMembers = _repoWrapper.User .FindByCondition(u => u.CityMembers.Any(cm => cm.City.ID == annualReport.CityId && cm.EndDate == null)) .Include(u => u.UserPlastDegrees); var annualReportViewModel = new AnnualReportViewModel { Operation = AnnualReportOperation.Editing, CityName = annualReportCheck.City.Name, CityMembers = _annualReportVMCreator.GetCityMembers(cityMembers), CityLegalStatusTypes = _annualReportVMCreator.GetCityLegalStatusTypes(), AnnualReport = annualReport }; return View("CreateEditAnnualReport", annualReportViewModel); } } catch (Exception e) { _logger.LogError($"Exception: {e.Message}"); return RedirectToAction("HandleError", "Error", new { code = 500 }); } }
public IActionResult CreateAnnualReportLikeAdmin(int cityId) { var userId = _userManager.GetUserId(User); if (!_cityAccessManager.HasAccess(userId, cityId)) return RedirectToAction("HandleError", "Error", new { code = 403 }); try { var city = _repoWrapper.City .FindByCondition(c => c.ID == cityId) .First(); var annualReportCheck = _repoWrapper.AnnualReports .FindByCondition(ar => ar.CityId == city.ID && ar.Date.Year == DateTime.Now.Year) .FirstOrDefault(); if (annualReportCheck == null) { var cityMembers = _repoWrapper.User .FindByCondition(u => u.CityMembers.Any(cm => cm.City.ID == cityId && cm.EndDate == null)) .Include(u => u.UserPlastDegrees); var annualReportViewModel = new AnnualReportViewModel { Operation = AnnualReportOperation.Creating, CityName = city.Name, CityMembers = _annualReportVMCreator.GetCityMembers(cityMembers), CityLegalStatusTypes = _annualReportVMCreator.GetCityLegalStatusTypes(), AnnualReport = _annualReportVMCreator.GetAnnualReport(userId, city.ID, cityMembers) }; return View("CreateEditAnnualReport", annualReportViewModel); } else { ViewData["ErrorMessage"] = $"Звіт станиці {city.Name} за {DateTime.Now.Year} рік вже існує!"; return View("CreateEditAnnualReport"); } } catch (Exception e) { _logger.LogError($"Exception: {e.Message}"); return RedirectToAction("HandleError", "Error", new { code = 500 }); } }
public async Task <IActionResult> Create(AnnualReportViewModel annualReport) { try { if (ModelState.IsValid) { var annualReportDTO = _mapper.Map <AnnualReportViewModel, AnnualReportDTO>(annualReport); await _annualReportService.CreateAsync(User, annualReportDTO); ViewData["Message"] = "Річний звіт станиці успішно створено!"; return(View("CreateEdit")); } else { var cityDTO = await _cityService.GetByIdAsync(annualReport.CityId); var city = _mapper.Map <CityDTOs.CityDTO, CityVMs.CityViewModel>(cityDTO); ViewData["ErrorMessage"] = "Річний звіт заповнений некоректно!"; return(View("CreateEdit", await GetCreateEditViewModel(city, AnnualReportOperation.Creating, annualReport))); } } catch (InvalidOperationException e) { ViewData["ErrorMessage"] = e.Message; return(View("CreateEdit")); } catch (UnauthorizedAccessException e) { ViewData["ErrorMessage"] = e.Message; return(View("CreateEdit")); } catch (Exception e) { _loggerService.LogError($"Exception: {e.Message}"); return(RedirectToAction("HandleError", "Error", new { code = StatusCodes.Status500InternalServerError })); } }
private async Task <CreateEditAnnualReportViewModel> GetCreateEditViewModel(CityVMs.CityViewModel city, AnnualReportOperation operation, AnnualReportViewModel annualReport) { var createEditAnnualReportViewModel = await GetCreateEditViewModel(city, operation); createEditAnnualReportViewModel.AnnualReport = annualReport; return(createEditAnnualReportViewModel); }