RoadServiceController.cs 3.25 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 RoadServiceController : Controller
    {
        private readonly IRoadServiceDs _roadServiceDs;
        private readonly IRoadDs _roadDs;
        private readonly IRegionDs _regionDs;
        private readonly IOrganizationDs _organizationDs;
        private readonly IRoadDirectionDs _roadDirectionDs;

        public RoadServiceController(IRoadServiceDs RoadServiceDs, IRoadDs RoadDs, IRegionDs RegionDs, IOrganizationDs OrganizationDs, IRoadDirectionDs RoadDirectionDs)
        {
            _roadServiceDs = RoadServiceDs;
            _roadDs = RoadDs;    
            _regionDs = RegionDs;
            _organizationDs = OrganizationDs;
            _roadDirectionDs = RoadDirectionDs;
        }

        // GET: RoadService
        [HttpGet]
        public async Task<IActionResult> Index([FromQuery] PaginationDsM data)
        {
            try
            {
                var roadServices = await _roadServiceDs.GetIndexListAsync(data);

                RoadServiceListVm vm = new RoadServiceListVm
                {
                    RoadServiceEditDsM = roadServices.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 Organization = await _organizationDs.GetSelectListAsync();
            var RoadDirection = await _roadDirectionDs.GetSelectListAsync();

            CatalogListVm vm = new CatalogListVm
            {
                RoadSelectListDsM = Road.ToList(),
                RegionSelectListDsM = Region.ToList(),
                OrganizationSelectListDsM = Organization.ToList(),
                RoadDirectionSelectListDsM = RoadDirection.ToList(),
            };
            return Json(vm);
        }


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

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

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