RoadSurfaceController.cs 3.89 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 RoadSurfaceController : Controller
    {
        private readonly IRoadDs _roadDs;
        private readonly IRegionDs _regionDs;
        private readonly ISurfaceTypeDs _surfaceTypeDs;
        private readonly IStateCommonDs _stateCommonDs;
        private readonly IRoadSurfaceDs _roadSurfaceDs;
        private readonly IRoadDirectionDs _roadDirectionDs;
        private readonly ISurfaceTreatmentDs _surfaceTreatmentDs;
        public RoadSurfaceController(
            IRoadSurfaceDs RoadSurfaceDs, 
            IStateCommonDs StateCommonDs, 
            IRoadDs RoadDs, 
            IRegionDs RegionDs, 
            ISurfaceTypeDs SurfaceTypeDs,
            ISurfaceTreatmentDs SurfaceTreatmentDs,
            IRoadDirectionDs RoadDirectionDs
            )
        {
            _roadDs = RoadDs;    
            _surfaceTypeDs = SurfaceTypeDs;    
            _regionDs = RegionDs;    
            _stateCommonDs = StateCommonDs;     
            _roadSurfaceDs = RoadSurfaceDs;
            _surfaceTreatmentDs = SurfaceTreatmentDs;
            _roadDirectionDs = RoadDirectionDs;     
        }

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

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

                RoadSurfaceListVm vm = new RoadSurfaceListVm
                {
                    RoadSurfaceEditDsM = 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();
            var SurfaceTreatment = await _surfaceTreatmentDs.GetSelectListAsync();
            var RoadDirection = await _roadDirectionDs.GetSelectListAsync();

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


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

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

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