using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using AutoMapper; using gehGassi.Common.Data; using gehGassi.Core.Interfaces; using gehGassi.Core.Services; using gehGassi.Permissions; using gehGassi.Web.Auth; using gehGassi.Web.Auth.Attributes; using gehGassi.Web.Helper; using gehGassi.Web.Models; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Localization; namespace gehGassi.Web.Controllers { /// /// Controller für die Verwaltung von Versandkosten /// [Authorize] public class ShipmentCostController : BaseController { private readonly IMapper _mapper; private readonly IStringLocalizer _localizer; private readonly IShipmentCostService _shipmentCostService; private readonly ICountryService _countryService; /// /// Erstellt eine Instanz /// /// Instanz eines IMapper /// Instanz eines IStringLocalizer /// Instanz eines IShipmentCostService /// Instanz eines ICountryService public ShipmentCostController(IMapper mapper, IStringLocalizer localizer, IShipmentCostService shipmentCostService, ICountryService countryService) { _mapper = mapper; _localizer = localizer; _shipmentCostService = shipmentCostService; _countryService = countryService; } /// /// Gibt einen View für die Verwaltung von Versandkosten zurück /// /// View [Authorize(Policy = Policies.PowerUserOnly)] [HasPermission(Permission.SettingsManage, Permission.SettingsShipment)] public IActionResult Index() { return View(); } /// /// Gibt eine Liste von Entitäten basierend auf Abfragekriterien zurück /// /// Abfragekriterien /// Liste von gefundenen Entitäten [Authorize(Policy = Policies.PowerUserOnly)] [HasPermission(Permission.SettingsManage, Permission.SettingsShipment)] [HttpPost] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult GetShipmentCosts([FromBody] DataManager dm) { if (dm != null) { var propList = new List(); dm.SetComplexProperties(propList); } var resultList = _shipmentCostService.Filter(dm?.SearchValue ?? ""); //Sortierung resultList = dm.ApplySorting(resultList); //Filter resultList = dm.ApplyFiltering(resultList, out var countFiltered); //Paging resultList = dm.ApplyPaging(resultList); var resultListVm = resultList.ToList().Select(item => _mapper.Map(item)).ToList(); //FilterPreview? if (!dm.RequiresCounts) return Json(resultListVm); return Json(new { result = resultListVm, count = countFiltered }); } /// /// Anlegen einer Versandkostenregel /// /// PartialView [Authorize(Policy = Policies.PowerUserOnly)] [HasPermission(Permission.SettingsManage, Permission.SettingsShipment)] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Create() { var model = new ShipmentCostCrudVm() { TargetCountryIso = "--", TargetCountryName = _localizer["Common_All"] }; return PartialView("_Create", model); } /// /// Anlegen einer Versandkostenregel /// /// Model /// Json [Authorize(Policy = Policies.PowerUserOnly)] [HasPermission(Permission.SettingsManage, Permission.SettingsShipment)] [ValidateAntiForgeryToken] [HttpPost] public async Task Create(ShipmentCostCrudVm model) { var result = new ResponseVm { Success = false }; if (ModelState.IsValid) { var item = _shipmentCostService.Create(); _mapper.Map(model, item); _shipmentCostService.Add(item); await _shipmentCostService.CommitAsync(User.Identity.Name); result.Data = item.ToCamelCaseJson(); result.Success = true; result.Html = string.Empty; return Json(result); } ModelState.Remove("TargetCountryName"); var country = _countryService.GetCountry(model.TargetCountryIso); model.TargetCountryName = country != null ? country.Name : ""; if (model.TargetCountryIso == "--") model.TargetCountryName = _localizer["Common_All"]; result.Html = await PartialView("_Create", model).ToStringAsync(ControllerContext); return Json(result); } /// /// Bearbeiten einer Versandkostenregel /// /// Id der Versandkostenregel /// PartialView [Authorize(Policy = Policies.PowerUserOnly)] [HasPermission(Permission.SettingsManage, Permission.SettingsShipment)] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public async Task Edit(int id) { var item = await _shipmentCostService.GetAsync(id); if (item != null) { var model = _mapper.Map(item); var country = _countryService.GetCountry(item.TargetCountryIso); model.TargetCountryName = country != null ? country.Name : ""; if (model.TargetCountryIso == "--") model.TargetCountryName = _localizer["Common_All"]; return PartialView("_Edit", model); } return PartialView("_Error"); } /// /// Bearbeiten einer Versandkostenregel /// /// Model /// Json [Authorize(Policy = Policies.PowerUserOnly)] [HasPermission(Permission.CustomersManage, Permission.SettingsShipment)] [ValidateAntiForgeryToken] [HttpPost] public async Task Edit(ShipmentCostCrudVm model) { var result = new ResponseVm { Success = false }; if (ModelState.IsValid) { var item = await _shipmentCostService.GetAsync(model.Id); if (item != null) { _mapper.Map(model, item); await _shipmentCostService.CommitAsync(User.Identity.Name); result.Data = item.ToCamelCaseJson(); result.Success = true; result.Html = string.Empty; return Json(new { result.Success, result.Html, result.Data }); } } ModelState.Remove("TargetCountryName"); var country = _countryService.GetCountry(model.TargetCountryIso); model.TargetCountryName = country != null ? country.Name : ""; if (model.TargetCountryIso == "--") model.TargetCountryName = _localizer["Common_All"]; result.Html = await PartialView("_Edit", model).ToStringAsync(ControllerContext); return Json(result); } /// /// Löschen einer Versandkostenregel /// /// Liste Id der Entität /// Json [Authorize(Policy = Policies.PowerUserOnly)] [HasPermission(Permission.SettingsManage, Permission.SettingsShipment)] [HttpPost] public async Task Delete(List ids) { var batchErrorHeader = $"

{_localizer["Common_BatchDelete_Failed"].Value}

"; var batchResult = new BatchResponseVm(batchErrorHeader) { Success = true }; foreach (var id in ids) { var item = await _shipmentCostService.GetAsync(id); if (item != null) { //await _customerService.ResetTypeAsync(item.Id); //Spezial zurücksetzen wenn nötig var usedCount = 0; if (usedCount == 0) { batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = item.Id.ToString(), Name = item.TargetCountryIso, Success = true, NotFound = false, ErrorMessage = "" }); _shipmentCostService.Remove(item); } else { if (usedCount != 0) { batchResult.Success = false; batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = id.ToString(), Name = item.TargetCountryIso, Success = false, NotFound = false, ErrorMessage = string.Format(_localizer["Common_BatchDelete_InUse"], $"{item.TargetCountryIso}", $"{usedCount}") + "
" }); } } } else { batchResult.Success = false; batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = id.ToString(), Name = "", Success = false, NotFound = true, ErrorMessage = string.Format(_localizer["Common_BatchDelete_NotFound"], $"{id}") + "
" }); } } if (batchResult.BatchResponseList.Any(c => c.Success)) await _shipmentCostService.CommitAsync(User.Identity.Name); return Json(batchResult); } #region Lookup /// /// Gibt eine Liste von Ländern für LookUp zurück - nur für Versandregeln /// /// Liste [Authorize(Policy = Policies.PowerUserOnly)] [HasPermission(Permission.SettingsManage, Permission.SettingsShipment)] public IActionResult LookupCountries([FromBody] DataManager dm) { var result = new List(); var filter = string.Empty; if (dm.Where?.FirstOrDefault() != null) { filter = dm.Where.First().value.ToString(); if (filter.IndexOf('(') > 0) { filter = filter.Substring(0, (filter.IndexOf('(') - 1)); filter = filter.TrimEnd(); } } var countries = _countryService.GetCountries().OrderBy(c => c.Name); var items = countries.Where(c => c.Name.ToLower().Contains(filter.ToLower())); foreach (var item in items) { result.Add(new LookupItemVm() { Id = item.Iso2.ToString(), Name = $"{item.Name}" }); } result.Insert(0, new LookupItemVm() { Id = "--", Name = _localizer["Common_All"].ToString() }); return Json(result); } #endregion } }