CrossSectionDs.cs 4.33 KB
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MapsDb.Interfaces;
using MapsDb.Models;
using MapsModels.DsModels;
using System.Reflection;
using Microsoft.EntityFrameworkCore;
namespace MapsDb.DataService
{
    public class CrossSectionDs : ICrossSectionDs
    {
        private PostgresDbContext _context;
        public CrossSectionDs(){
            _context = new PostgresDbContext();
        }
        public Task<IList<CrossSectionEditDsM>> GetIndexListAsync(PaginationDsM pagination){
            return Task.Factory.StartNew(()=> { return GetAllCrossSection(pagination); });
        }
        private IList<CrossSectionEditDsM> GetAllCrossSection(PaginationDsM pagination)
        {
            var data =  _context.CrossSection.Select(CrossSection => new CrossSectionEditDsM
            {
                Id = CrossSection.Id,
                RoadId = CrossSection.RoadId,
                RegionId = CrossSection.RegionId,
                StateCommonId = CrossSection.StateCommonId,
                YearBuild = CrossSection.YearBuild,
                YearRepair = CrossSection.YearRepair,
                SafetyAvailability = CrossSection.SafetyAvailability,
                TubeAvailability = CrossSection.TubeAvailability,
                Angle = CrossSection.Angle,
                Width = CrossSection.Width,
                DistanceEdge = CrossSection.DistanceEdge,
                LengthSurface = CrossSection.LengthSurface,
                LengthSection = CrossSection.LengthSection,
                SurfaceTypeId = CrossSection.SurfaceTypeId,
                Direction = CrossSection.Direction,
                LocationRight = CrossSection.LocationRight,
                LocationLeft = CrossSection.LocationLeft,
              }).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<CrossSection> CreateAsync(CrossSectionEditDsM data){
            return Task.Factory.StartNew(()=> { return Create(data); });
        }
        private CrossSection Create(CrossSectionEditDsM data)
        {   
            CrossSection Model = InsertModel(data);
            _context.CrossSection.Add(Model);
            _context.SaveChanges();
            return Model;
        }
        public Task<CrossSection> UpdateAsync(CrossSectionEditDsM data, int id){
            return Task.Factory.StartNew(()=> { return Update(data, id); });
        }
        private CrossSection Update(CrossSectionEditDsM data, int id)
        {   
            CrossSection Model = InsertModel(data);
            Model.Id = id;
            _context.CrossSection.Update(Model);
            _context.SaveChanges();
            return Model;
        }
        public CrossSection InsertModel(CrossSectionEditDsM data){
         CrossSection Model = new CrossSection{
                Id = data.Id,
                RoadId = data.RoadId,
                RegionId = data.RegionId,
                StateCommonId = data.StateCommonId,
                YearBuild = data.YearBuild,
                YearRepair = data.YearRepair,
                SafetyAvailability = data.SafetyAvailability,
                TubeAvailability = data.TubeAvailability,
                Angle = data.Angle,
                Width = data.Width,
                DistanceEdge = data.DistanceEdge,
                LengthSurface = data.LengthSurface,
                LengthSection = data.LengthSection,
                SurfaceTypeId = data.SurfaceTypeId,
                Direction = data.Direction,
                LocationRight = data.LocationRight,
                LocationLeft = data.LocationLeft,
            };
            return Model;
        }
        public async Task<int> DeleteAsync(int Id)
        {
            var CrossSection = await _context.CrossSection.SingleOrDefaultAsync(x => x.Id == Id);
            _context.CrossSection.Remove(CrossSection);
            return await _context.SaveChangesAsync();
        }
    }
}