ContractorController.cs 2.45 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 ContractorController : Controller
    {
        private readonly IContractorDs _contractorDs;
        private readonly IRoadDs _roadDs;
        private readonly ICrossSectionDs _crossSectionDs;

        public ContractorController(IContractorDs ContractorDs, IRoadDs RoadDs, ICrossSectionDs CrossSectionDs, IRegionDs RegionDs)
        {
            _contractorDs = ContractorDs;
            _roadDs = RoadDs;    
            _crossSectionDs = CrossSectionDs;       
        }

        // GET: Contractor
        [HttpGet]
        public async Task<IActionResult> Index([FromQuery] PaginationDsM data)
        {
     
                var contractors = await _contractorDs.GetIndexListAsync(data);

                ContractorListVm vm = new ContractorListVm
                {
                    ContractorEditDsM = contractors.ToList()
                };

                return Json(vm);
   


        }

        [HttpGet]
        public async Task<IActionResult> Directory(){
            var Road = await _roadDs.GetSelectListAsync();
            var CrossSection = await _crossSectionDs.GetSelectListAsync();

            CatalogListVm vm = new CatalogListVm
            {
                RoadSelectListDsM = Road.ToList(),
                CrossSectionSelectListDsM = CrossSection.ToList(),
            };
            return Json(vm);
        }


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

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

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