using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using MVC_SimpleCRUD_Layered.Application.Models; using MVC_SimpleCRUD_Layered.Application.People; namespace MVC_SimpleCRUD_Layered.Web.Controllers; public class PeopleController : Controller { private readonly IPeopleService peopleService; public PeopleController(IPeopleService peopleService) { this.peopleService = peopleService; } public async Task Index(int page = 1, int pageSize = 0, string? sorts = null, string? searchText = null) { PagedList model = await peopleService.GetPagedListAsync(new PeopleIndexRequest { Page = page, PageSize = pageSize, Sorts = sorts, SearchText = searchText }); return View(model); } [HttpGet] public async Task Create(int page = 1, int pageSize = 0, string? sorts = null, string? searchText = null) { var model = new PersonForm { Page = page, PageSize = pageSize, Sorts = sorts, SearchText = searchText }; await PrepareDropDownLists(model.CountryCode); return View(model); } [HttpPost] [ValidateAntiForgeryToken] public async Task Create(PersonForm model) { if (!ModelState.IsValid) { await PrepareDropDownLists(model.CountryCode); return View(model); } var result = await peopleService.CreateAsync(model); if (result.Success) { ShowToast($"Person {result.PersonName} added."); return RedirectToIndex(model); } ModelState.AddModelError(string.Empty, result.ErrorMessage ?? "Person could not be saved."); await PrepareDropDownLists(model.CountryCode); return View(model); } [HttpGet] public async Task Edit(int id, int page = 1, int pageSize = 0, string? sorts = null, string? searchText = null) { var model = await peopleService.GetFormForEditAsync(id, new PeopleIndexRequest { Page = page, PageSize = pageSize, Sorts = sorts, SearchText = searchText }); if (model is null) { ShowToast($"Person with id {id} was not found.", Constants.ToastVariants.Danger, Constants.ToastTitles.Error); return RedirectToAction(nameof(Index), new { page, pageSize, sorts, searchText }); } await PrepareDropDownLists(model.CountryCode); return View(model); } [HttpPost] [ValidateAntiForgeryToken] public async Task Edit(int id, PersonForm model) { if (model.Id != id) { return BadRequest(); } if (!ModelState.IsValid) { await PrepareDropDownLists(model.CountryCode); return View(model); } var result = await peopleService.UpdateAsync(id, model); if (result.Success) { ShowToast($"Person {result.PersonName} updated."); return RedirectToIndex(model); } if (string.Equals(result.ErrorMessage, $"Person with id {id} was not found.", StringComparison.Ordinal)) { ShowToast(result.ErrorMessage!, Constants.ToastVariants.Danger, Constants.ToastTitles.Error); return RedirectToIndex(model); } ModelState.AddModelError(string.Empty, result.ErrorMessage ?? "Person could not be saved."); await PrepareDropDownLists(model.CountryCode); return View(model); } [HttpPost] [ValidateAntiForgeryToken] public async Task Delete(int id, int page = 1, int pageSize = 0, string? sorts = null, string? searchText = null) { var result = await peopleService.DeleteAsync(id); if (!result.Found) { ShowToast($"Person with id {id} was not found.", Constants.ToastVariants.Danger, Constants.ToastTitles.Error); return RedirectToAction(nameof(Index), new { page, pageSize, sorts, searchText }); } if (result.Success) { ShowToast($"Person {result.PersonName} deleted."); } else { ShowToast($"Person {result.PersonName} could not be deleted. {result.ErrorMessage}", Constants.ToastVariants.Danger, Constants.ToastTitles.Error); } return RedirectToAction(nameof(Index), new { page, pageSize, sorts, searchText }); } private async Task PrepareDropDownLists(string? selectedCountryCode = null) { var countries = await peopleService.GetCountryOptionsAsync(); ViewBag.Countries = new SelectList(countries, nameof(CountryOption.Code), nameof(CountryOption.Name), selectedCountryCode); } private RedirectToActionResult RedirectToIndex(PersonForm model) { return RedirectToAction(nameof(Index), new { page = model.Page, pageSize = model.PageSize, sorts = model.Sorts, searchText = model.SearchText }); } private void ShowToast(string message, string variant = Constants.ToastVariants.Success, string title = Constants.ToastTitles.Notification) { TempData[Constants.TempDataKeys.ToastMessage] = message; TempData[Constants.TempDataKeys.ToastVariant] = variant; TempData[Constants.TempDataKeys.ToastTitle] = title; } }