using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using gehGassi.Common.Data;
using gehGassi.Core.Services;
using gehGassi.Web.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
namespace gehGassi.Web.Controllers
{
///
/// Controller für die Abfrage von Ländern
///
[Authorize]
public class CountryController : BaseController
{
private readonly ICountryService _countryService;
private readonly IMapper _mapper;
private readonly IStringLocalizer _localizer;
///
/// Erstellt eine Instanz
///
/// Instanz eines ICountryService
/// Instanz eines IMapper
/// Instanz eines IStringLocalizer
public CountryController(ICountryService countryService, IMapper mapper, IStringLocalizer localizer)
{
_countryService = countryService;
_mapper = mapper;
_localizer = localizer;
}
///
/// Gibt eine Liste von Ländern für LookUp zurück
///
/// Liste
public IActionResult LookupCountries([FromBody] DataManager dm, bool includeAll = false)
{
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}" });
}
if (includeAll)
result.Insert(0, new LookupItemVm() { Id = "", Name = _localizer["Common_All"].ToString() });
//result.Insert(0, new LookupItemVm() { Id = "", Name = _localizer["Common_Select"].ToString() });
return Json(result);
}
///
/// Gibt eine Liste von Bundes-Ländern für LookUp zurück
///
/// DataManager
/// ISO2 des Landes
///
public IActionResult LookupStates([FromBody] DataManager dm, string countryIso, bool includeAll = false)
{
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 states = _countryService.GetStates(countryIso).OrderBy(c => c.Name);
var items = states.Where(c => c.Name.ToLower().Contains(filter.ToLower()));
foreach (var item in items)
{
result.Add(new LookupItemVm() { Id = item.Code.ToString(), Name = $"{item.Name}" });
}
if (includeAll)
result.Insert(0, new LookupItemVm() { Id = "", Name = _localizer["Common_All"].ToString() });
//result.Insert(0, new LookupItemVm() { Id = "", Name = _localizer["Common_Select"].ToString() });
return Json(result);
}
}
}