CrossSectionController.cs 3.3 KB
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 CrossSectionController : Controller
    {
        private readonly IRoadDs _roadDs;
        private readonly IRegionDs _regionDs;
        private readonly ISurfaceTypeDs _surfaceTypeDs;
        private readonly IStateCommonDs _stateCommonDs;
        private readonly ICrossSectionDs _crossSectionDs;
      

        public CrossSectionController(
            ICrossSectionDs CrossSectionDs, 
            IStateCommonDs StateCommonDs, 
            IRoadDs RoadDs, 
            IRegionDs RegionDs, 
            ISurfaceTypeDs SurfaceTypeDs
            )
        {
            _roadDs = RoadDs;    
            _surfaceTypeDs = SurfaceTypeDs;    
            _regionDs = RegionDs;    
            _stateCommonDs = StateCommonDs;     
            _crossSectionDs = CrossSectionDs;    
        }

        // GET: BusStop
        [HttpGet]
        public async Task<IActionResult> Index([FromQuery] PaginationDsM data)
        {

            try
            {
                var Data = await _crossSectionDs.GetIndexListAsync(data);

                CrossSectionListVm vm = new CrossSectionListVm
                {
                    CrossSectionEditDsM = Data.ToList()
                };

                return Json(vm);
            }
            catch (NullReferenceException)
            {
                Response.StatusCode = 400;
                return Json("There is no field with name " + data.sort);
            }
            catch (Exception)
            {
                return NotFound();
            }
        }

        [HttpGet]
        public async Task<IActionResult> Directory(){
            var Road = await _roadDs.GetSelectListAsync();
            var Region = await _regionDs.GetSelectListAsync();
            var SurfaceType = await _surfaceTypeDs.GetSelectListAsync();
            var StateCommon = await _stateCommonDs.GetSelectListAsync();

            CatalogListVm vm = new CatalogListVm
            {
                RoadSelectListDsM = Road.ToList(),
                RegionSelectListDsM = Region.ToList(),
                SurfaceTypeSelectListDsM = SurfaceType.ToList(),
                StateCommonSelectListDsM = StateCommon.ToList()
            };
            return Json(vm);
        }


        [HttpPost]
        public async Task<IActionResult> Create([FromBody] CrossSectionEditDsM data)
        {
            var result = await _crossSectionDs.CreateAsync(data);
            return Json(result);
        }

        [HttpPost]
        public async Task<IActionResult> Update(int id, [FromBody] CrossSectionEditDsM data){
                await _crossSectionDs.UpdateAsync(data,id);
                return Json(String.Empty);
        }

     
        [HttpDelete]
        public async Task<IActionResult> Delete(int id)
        {   
            try
            {
                 int data = await _crossSectionDs.DeleteAsync(id);
                 return Json(data);
            }
            catch (ArgumentNullException )
            {
                return NotFound();
            }
        }
    }
}