using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; using MapsDb; using MapsDb.Interfaces; using MapsDb.DataService; using MapsModels.ViewModels; using MapsModels.DsModels; using System; namespace Maps.Controllers { public class BusStopController : Controller { private readonly IBusStopDs _busStopDs; private readonly IRoadDs _roadDs; private readonly ISettlementDs _settlementDs; private readonly IStateCommonDs _stateCommonDs; private readonly IRegionDs _regionDs; private readonly ISurfaceTypeDs _surfaceTypeDs; public BusStopController(IBusStopDs BusStopDs, IRoadDs RoadDs, ISettlementDs SettlementDs, IRegionDs RegionDs, IStateCommonDs StateCommonDs, ISurfaceTypeDs SurfaceTypeDs) { _busStopDs = BusStopDs; _roadDs = RoadDs; _settlementDs = SettlementDs; _stateCommonDs = StateCommonDs; _regionDs = RegionDs; _surfaceTypeDs = SurfaceTypeDs; } // GET: BusStop [HttpGet] public async Task Index([FromQuery] PaginationDsM data) { try { var busStops = await _busStopDs.GetIndexListAsync(data); BusStopListVm vm = new BusStopListVm { BusStopEditDsM = busStops.ToList() }; return Json(vm); } catch (NullReferenceException e) { Response.StatusCode = 400; return Json("There is no field with name " + data.sort); } catch (Exception e) { return NotFound(); } } [HttpGet] public async Task Directory(){ var SurfaceType = await _surfaceTypeDs.GetSelectListAsync(); var StateCommon = await _stateCommonDs.GetSelectListAsync(); var Settlement = await _settlementDs.GetSelectListAsync(); var Road = await _roadDs.GetSelectListAsync(); var Region = await _regionDs.GetSelectListAsync(); CatalogListVm vm = new CatalogListVm { SurfaceTypeSelectListDsM = SurfaceType.ToList(), StateCommonSelectListDsM = StateCommon.ToList(), SettlementSelectListDsM = Settlement.ToList(), RoadSelectListDsM = Road.ToList(), RegionSelectListDsM = Region.ToList() }; return Json(vm); } [HttpPost] public async Task Create([FromBody] BusStopEditDsM data) { var result = await _busStopDs.CreateAsync(data); return Json(result); } [HttpPost] public async Task Update(int id, [FromBody] BusStopEditDsM data){ await _busStopDs.UpdateAsync(data,id); return Json(String.Empty); } [HttpDelete] public async Task Delete(int id) { try { int busStop = await _busStopDs.DeleteAsync(id); return Json(busStop); } catch (ArgumentNullException ) { return NotFound(); } } } }