BusStopDs.cs 8.79 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using AutoMapper;
using MapsDb.Interfaces;
using MapsDb.Models;
using MapsModels.DsModels;
using Microsoft.EntityFrameworkCore;

namespace MapsDb.DataService
{
    public class BusStopDs : IBusStopDs
    {
        private PostgresDbContext _context;
        public BusStopDs(){
            _context = new PostgresDbContext();
        }
        public Task<IList<BusStopEditDsM>> GetIndexListAsync(PaginationDsM pagination){
            return Task.Factory.StartNew(()=> { return GetAllBusStop(pagination); });
        }
        private IList<BusStopEditDsM> GetAllBusStop(PaginationDsM pagination)
        {

            var filter =  pagination.filter;

            IQueryable<BusStop> data = _context.BusStop
            .Include(d=>d.Road)
            .Include(d=>d.Region)
            .Include(d=>d.SurfaceType)
            .Include(d=>d.StateCommon);

            data = Filtering(pagination.filter, data);
            //.Where(d => d.Region.Name == filter);

            IQueryable<BusStopEditDsM> result = data
            .Select(BusStop => Mapper.Map<BusStopEditDsM>(BusStop))
            .Skip(pagination.from)
            .Take(pagination.perPage);

            switch (pagination.orderType())
            {
                case "ASC":
                    return result.OrderBy(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList();
          
                case "DESC":
                    return result.OrderByDescending(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList();
            
                default:
                    return result.OrderByDescending(i => i.Id).ToList();
            }     
            
        }

        public Task<BusStop> CreateAsync(BusStopEditDsM data){
              return Task.Factory.StartNew(()=> { return Create(data); });
        }
        private BusStop Create(BusStopEditDsM data)
        {   
            
            BusStop Model = InsertModel(data);
            _context.BusStop.Add(Model);
            _context.SaveChanges();
            return Model;
        }
        public Task<BusStop> UpdateAsync(BusStopEditDsM data, int id){
            return Task.Factory.StartNew(()=> { return Update(data, id); });
        }
        private BusStop Update(BusStopEditDsM data, int id)
        {   
            BusStop Model = InsertModel(data);
            Model.Id = id;
            _context.BusStop.Update(Model);
            _context.SaveChanges();
            return Model;
        }
        public BusStop InsertModel(BusStopEditDsM data){
            BusStop Model = Mapper.Map<BusStop>(data);
            return Model;
        }
        public IQueryable<BusStop> Filtering(string filters, IQueryable<BusStop> data){
            if(filters != null){
               string[] FilterWords = filters.Split(';').Where(x => !string.IsNullOrEmpty(x)).ToArray();
                foreach(string word in FilterWords){
                    string[] filter = Regex.Split(word.Substring(1), "(.*)_(.*)");
                    if(filter.Length < 3 ){
                        continue;
                    }
                    string field = char.ToUpper(filter[1][0]) + filter[1].Substring(1);
                    string param = filter[2].ToLower();
                    if(word.StartsWith("$")){
                        data = FilterByEndWith(field, param, data);
                    }
                    else if(word.StartsWith("^")){
                        data = FilterByStartWith(field, param, data);
                    }
                    else if(word.StartsWith("*")){
                        data = FilterByComtains(field, param, data);
                    }
                    else if(word.StartsWith("!")){
                        data = FilterByNotEquals(field, param, data);
                    }
                    else if(word.StartsWith("=")){
                        data = FilterByEquals(field, param, data);
                    }
                }
            
            }
            return data;
        }
        public IQueryable<BusStop> FilterByComtains(string field, string param, IQueryable<BusStop> data){
            switch(field){
                case "RoadId":
                    return data.Where(i => i.Road.Name != null && i.Road.Name.ToLower().Contains(param) );
                case "RegionId":
                    return data.Where(i => i.Region.Name != null && i.Region.Name.ToLower().Contains(param) );
                case "SurfaceTypeId":
                    return data.Where(i => i.SurfaceType.Name != null && i.SurfaceType.Name.ToLower().Contains(param) );
                case "StateCommonId":
                    return data.Where(i => i.StateCommon.Value != null && i.StateCommon.Value.ToLower().Contains(param) );
                default:
                    return data.Where(i => Convert.ToString(i.GetType().GetProperty(field).GetValue(i, null)).ToLower().Contains(param) );
            }
        }
        public IQueryable<BusStop> FilterByEquals(string field, string param, IQueryable<BusStop> data){
            switch(field){
                case "RoadId":
                    return data.Where(i => i.Road.Name != null && i.Road.Name.ToLower() == param );
                case "RegionId":
                    return data.Where(i => i.Region.Name != null && i.Region.Name.ToLower() == param );
                case "SurfaceTypeId":
                    return data.Where(i => i.SurfaceType.Name != null && i.SurfaceType.Name.ToLower() == param );
                case "StateCommonId":
                    return data.Where(i => i.StateCommon.Value != null && i.StateCommon.Value.ToLower() == param );
                default:
                return data.Where(i => Convert.ToString(i.GetType().GetProperty(field).GetValue(i, null)).ToLower() == param );
            }

        }
        public IQueryable<BusStop> FilterByNotEquals(string field, string param, IQueryable<BusStop> data){
           switch(field){
                case "RoadId":
                    return data.Where(i => i.Road.Name != null && i.Road.Name.ToLower() != param );
                case "RegionId":
                    return data.Where(i => i.Region.Name != null && i.Region.Name.ToLower() != param );
                case "SurfaceTypeId":
                    return data.Where(i => i.SurfaceType.Name != null && i.SurfaceType.Name.ToLower() != param );
                case "StateCommonId":
                    return data.Where(i => i.StateCommon.Value != null && i.StateCommon.Value.ToLower() != param );
                default:
                return data.Where(i => Convert.ToString(i.GetType().GetProperty(field).GetValue(i, null)).ToLower() != param );
            }
        }
        public IQueryable<BusStop> FilterByStartWith(string field, string param, IQueryable<BusStop> data){
            switch(field){
                case "RoadId":
                    return data.Where(i => i.Road.Name != null && i.Road.Name.ToLower().StartsWith(param) );
                case "RegionId":
                    return data.Where(i => i.Region.Name != null && i.Region.Name.ToLower().StartsWith(param) );
                case "SurfaceTypeId":
                    return data.Where(i => i.SurfaceType.Name != null && i.SurfaceType.Name.ToLower().StartsWith(param) );
                case "StateCommonId":
                    return data.Where(i => i.StateCommon.Value != null && i.StateCommon.Value.ToLower().StartsWith(param) );
                default:
                return data.Where(i => Convert.ToString(i.GetType().GetProperty(field).GetValue(i, null)).ToLower().StartsWith(param) );
            }
        }
        public IQueryable<BusStop> FilterByEndWith(string field, string param, IQueryable<BusStop> data){
          switch(field){
                case "RoadId":
                    return data.Where(i => i.Road.Name != null && i.Road.Name.ToLower().EndsWith(param) );
                case "RegionId":
                    return data.Where(i => i.Region.Name != null && i.Region.Name.ToLower().EndsWith(param) );
                case "SurfaceTypeId":
                    return data.Where(i => i.SurfaceType.Name != null && i.SurfaceType.Name.ToLower().EndsWith(param) );
                case "StateCommonId":
                    return data.Where(i => i.StateCommon.Value != null && i.StateCommon.Value.ToLower().EndsWith(param) );
                default:
                return data.Where(i => Convert.ToString(i.GetType().GetProperty(field).GetValue(i, null)).ToLower().EndsWith(param) );
            }
        }
        public async Task<int> DeleteAsync(int Id)
        {
            var busStop = await _context.BusStop.SingleOrDefaultAsync(x => x.Id == Id);
            _context.BusStop.Remove(busStop);
            return await _context.SaveChangesAsync();
        }
    }
}