FlowIntensityDs.cs 5.93 KB
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using MapsDb.Interfaces;
using MapsDb.Models;
using MapsModels.DsModels;
using Microsoft.EntityFrameworkCore;
namespace MapsDb.DataService
{
    public class FlowIntensityDs : IFlowIntensityDs
    {
        private PostgresDbContext _context;
        public FlowIntensityDs(){
            _context = new PostgresDbContext();
        }
        public Task<IList<FlowIntensityEditDsM>> GetIndexListAsync(PaginationDsM pagination){
            return Task.Factory.StartNew(()=> { return GetAllFlowIntensity(pagination); });
        }
        private IList<FlowIntensityEditDsM> GetAllFlowIntensity(PaginationDsM pagination)
        {
            var data =  _context.FlowIntensity.Select(FlowIntensity => new FlowIntensityEditDsM
            {
                Id = FlowIntensity.Id,
                RoadId = FlowIntensity.RoadId,
                RegionId = FlowIntensity.RegionId,
                Location = FlowIntensity.Location,
                Begin = FlowIntensity.Begin,
                End = FlowIntensity.End,
                RoadDirectionId = FlowIntensity.RoadDirectionId,
                SettlementId = FlowIntensity.SettlementId,
                IntensityTotal = FlowIntensity.IntensityTotal,
                IntensityIncrease = FlowIntensity.IntensityIncrease,
                IntensityMoto = FlowIntensity.IntensityMoto,
                IntensityMotoSidecar = FlowIntensity.IntensityMotoSidecar,
                IntensityCar = FlowIntensity.IntensityCar,
                IntensityTruckTwo = FlowIntensity.IntensityTruckTwo,
                IntensityTruckTwoSix = FlowIntensity.IntensityTruckTwoSix,
                IntensityTruckSixEight = FlowIntensity.IntensityTruckSixEight,
                IntensityTruckEightFourteen = FlowIntensity.IntensityTruckEightFourteen,
                IntensityTruckFourteen = FlowIntensity.IntensityTruckFourteen,
                IntensityLorryTwelve = FlowIntensity.IntensityLorryTwelve,
                IntensityLorryTwelveTwenty = FlowIntensity.IntensityLorryTwelveTwenty,
                IntensityLorryTwentyThirty = FlowIntensity.IntensityLorryTwentyThirty,
                IntensityLorryThirty = FlowIntensity.IntensityLorryThirty,
                IntensityTractorUnderTen = FlowIntensity.IntensityTractorUnderTen,
                IntensityTractorOverTen = FlowIntensity.IntensityTractorOverTen,
                IntensityBus = FlowIntensity.IntensityBus,
                IntensityBusCoupled = FlowIntensity.IntensityBusCoupled,
                DateAdd = FlowIntensity.DateAdd,
            }).Skip(pagination.from).Take(pagination.perPage);
            switch (pagination.orderType())
            {
                case "ASC":
                    return data.OrderBy(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList();
          
                case "DESC":
                    return data.OrderByDescending(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList();
            
                default:
                    return data.OrderByDescending(i => i.Id).ToList();
            }     
            
        }

        public Task<FlowIntensity> CreateAsync(FlowIntensityEditDsM data){
              return Task.Factory.StartNew(()=> { return Create(data); });
        }
        private FlowIntensity Create(FlowIntensityEditDsM data)
        {   
            
            FlowIntensity Model = InsertModel(data);
            _context.FlowIntensity.Add(Model);
            _context.SaveChanges();
            return Model;
        }
        public Task<FlowIntensity> UpdateAsync(FlowIntensityEditDsM data, int id){
            return Task.Factory.StartNew(()=> { return Update(data, id); });
        }
        private FlowIntensity Update(FlowIntensityEditDsM data, int id)
        {   
            FlowIntensity Model = InsertModel(data);
            Model.Id = id;
            _context.FlowIntensity.Update(Model);
            _context.SaveChanges();
            return Model;
        }
        public FlowIntensity InsertModel(FlowIntensityEditDsM data){
            FlowIntensity Model = new FlowIntensity{
                RoadId = data.RoadId,
                RegionId = data.RegionId,
                Location = data.Location,
                Begin = data.Begin,
                End = data.End,
                RoadDirectionId = data.RoadDirectionId,
                SettlementId = data.SettlementId,
                IntensityTotal = data.IntensityTotal,
                IntensityIncrease = data.IntensityIncrease,
                IntensityMoto = data.IntensityMoto,
                IntensityMotoSidecar = data.IntensityMotoSidecar,
                IntensityCar = data.IntensityCar,
                IntensityTruckTwo = data.IntensityTruckTwo,
                IntensityTruckTwoSix = data.IntensityTruckTwoSix,
                IntensityTruckSixEight = data.IntensityTruckSixEight,
                IntensityTruckEightFourteen = data.IntensityTruckEightFourteen,
                IntensityTruckFourteen = data.IntensityTruckFourteen,
                IntensityLorryTwelve = data.IntensityLorryTwelve,
                IntensityLorryTwelveTwenty = data.IntensityLorryTwelveTwenty,
                IntensityLorryTwentyThirty = data.IntensityLorryTwentyThirty,
                IntensityLorryThirty = data.IntensityLorryThirty,
                IntensityTractorUnderTen = data.IntensityTractorUnderTen,
                IntensityTractorOverTen = data.IntensityTractorOverTen,
                IntensityBus = data.IntensityBus,
                IntensityBusCoupled = data.IntensityBusCoupled,
                DateAdd = data.DateAdd,
            };
            return Model;
        }
        public async Task<int> DeleteAsync(int Id)
        {
            var flowIntensity = await _context.FlowIntensity.SingleOrDefaultAsync(x => x.Id == Id);
            _context.FlowIntensity.Remove(flowIntensity);
            return await _context.SaveChangesAsync();
        }
    }
}