SettlementAddressLinkController.cs 3.49 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 SettlementAddressLinkController : Controller
    {
        private readonly ISettlementAddressLinkDs _settlementAddressLinkDs;
        private readonly IRoadDs _roadDs;
        private readonly ISettlementDs _settlementDs;
        private readonly IRegionDs _regionDs;
        private readonly ISettlementLocationDs _settlementLocationDs;

        public SettlementAddressLinkController(ISettlementAddressLinkDs SettlementAddressLinkDs, IRoadDs RoadDs, ISettlementDs SettlementDs, IRegionDs RegionDs, ISettlementLocationDs SettlementLocationDs)
        {
            _settlementAddressLinkDs = SettlementAddressLinkDs;
            _roadDs = RoadDs;    
            _settlementDs = SettlementDs;    
            _regionDs = RegionDs;
            _settlementLocationDs = SettlementLocationDs;
        }

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

                SettlementAddressLinkListVm vm = new SettlementAddressLinkListVm
                {
                    SettlementAddressLinkEditDsM = settlementAddressLinks.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 Settlement = await _settlementDs.GetSelectListAsync();
            var Road = await _roadDs.GetSelectListAsync();
            var Region = await _regionDs.GetSelectListAsync();
            var SettlementLocation = await _settlementLocationDs.GetSelectListAsync();

            CatalogListVm vm = new CatalogListVm
            {
                SettlementSelectListDsM = Settlement.ToList(),
                RoadSelectListDsM = Road.ToList(),
                RegionSelectListDsM = Region.ToList(),
                SettlementLocationSelectListDsM = SettlementLocation.ToList()
            };
            return Json(vm);
        }


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

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

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