ServiceObjectDs.cs 4.07 KB
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MapsDb.Interfaces;
using MapsDb.Models;
using MapsModels.DsModels;
using Microsoft.EntityFrameworkCore;
namespace MapsDb.DataService
{
    public class ServiceObjectDs : IServiceObjectDs
    {
        private PostgresDbContext _context;
        public ServiceObjectDs(){
            _context = new PostgresDbContext();
        }
        public Task<IList<ServiceObjectEditDsM>> GetIndexListAsync(){
            return Task.Factory.StartNew(GetAllServiceObject);
        }
        private IList<ServiceObjectEditDsM> GetAllServiceObject()
        {
           return _context.ServiceObject.Select(ServiceObject => new ServiceObjectEditDsM
            {
                ServiceObjectId = ServiceObject.ServiceObjectId,
                RoadId = ServiceObject.RoadId,
                RegionId = ServiceObject.RegionId,
                SettlementId = ServiceObject.SettlementId,
                LocationLeft = ServiceObject.LocationLeft,
                LocationRight = ServiceObject.LocationRight,
                ServiceObjectTypeId = ServiceObject.ServiceObjectTypeId,
                DepartmentAffiliationId = ServiceObject.DepartmentAffiliationId,
                LocationAxis = ServiceObject.LocationAxis,
                Distance = ServiceObject.Distance,
                Capacity = ServiceObject.Capacity,
                ArrangementElements = ServiceObject.ArrangementElements,
            }).OrderByDescending(ServiceObject => ServiceObject.ServiceObjectId).ToList();
        }

        public Task<ServiceObject> SaveAsync(ServiceObjectEditDsM serviceObject, int? id = null){
              return Task.Factory.StartNew(()=> { return Save(serviceObject, id); });
        }
        private ServiceObject Save(ServiceObjectEditDsM serviceObject, int? id)
        {   
            ServiceObject Data = new ServiceObject{
                ServiceObjectId = serviceObject.ServiceObjectId,
                RoadId = serviceObject.RoadId,
                RegionId = serviceObject.RegionId,
                SettlementId = serviceObject.SettlementId,
                LocationLeft = serviceObject.LocationLeft,
                LocationRight = serviceObject.LocationRight,
                ServiceObjectTypeId = serviceObject.ServiceObjectTypeId,
                DepartmentAffiliationId = serviceObject.DepartmentAffiliationId,
                LocationAxis = serviceObject.LocationAxis,
                Distance = serviceObject.Distance,
                Capacity = serviceObject.Capacity,
                ArrangementElements = serviceObject.ArrangementElements,
            };
            var ServiceObjectFromDb = _context.ServiceObject.FirstOrDefault(x => x.ServiceObjectId == id);
            if(ServiceObjectFromDb != null)
            {
                ServiceObjectFromDb.ServiceObjectId = Data.ServiceObjectId;
                ServiceObjectFromDb.RoadId = Data.RoadId;
                ServiceObjectFromDb.RegionId = Data.RegionId;
                ServiceObjectFromDb.SettlementId = Data.SettlementId;
                ServiceObjectFromDb.LocationLeft = Data.LocationLeft;
                ServiceObjectFromDb.LocationRight = Data.LocationRight;
                ServiceObjectFromDb.ServiceObjectTypeId = Data.ServiceObjectTypeId;
                ServiceObjectFromDb.DepartmentAffiliationId = Data.DepartmentAffiliationId;
                ServiceObjectFromDb.LocationAxis = Data.LocationAxis;
                ServiceObjectFromDb.Distance = Data.Distance;
                ServiceObjectFromDb.Capacity = Data.Capacity;
                ServiceObjectFromDb.ArrangementElements = Data.ArrangementElements;
            }
            else
            {
                _context.ServiceObject.Add(Data);
            }
            _context.SaveChanges();
            return Data;
        }
        public async Task<int> DeleteAsync(int? Id)
        {
            var ServiceObject = await _context.ServiceObject.SingleOrDefaultAsync(x => x.ServiceObjectId == Id);
            _context.ServiceObject.Remove(ServiceObject);
            return await _context.SaveChangesAsync();
        }
    }
}