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 BusStopDs : IBusStopDs { private PostgresDbContext _context; public BusStopDs(){ _context = new PostgresDbContext(); } public Task> GetIndexListAsync(PaginationDsM pagination){ return Task.Factory.StartNew(()=> { return GetAllBusStop(pagination); }); } private IList GetAllBusStop(PaginationDsM pagination) { var data = _context.BusStop.Select(busStop => new BusStopEditDsM { Id = busStop.Id, RoadId = busStop.RoadId, RegionId = busStop.RegionId, SettlementId = busStop.SettlementId, LocationLeft = busStop.LocationLeft, LocationRight = busStop.LocationRight, SurfaceTypeId = busStop.SurfaceTypeId, AreaStopAvailability = busStop.AreaStopAvailability, AreaLandAvailability = busStop.AreaLandAvailability, PocketAvailability = busStop.PocketAvailability, ToiletAvailability = busStop.ToiletAvailability, YearBuild = busStop.YearBuild, YearRepair = busStop.YearRepair, StateCommonId = busStop.StateCommonId }).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.OrderBy(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList(); } } public Task 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 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 = new BusStop{ RoadId = data.RoadId, RegionId = data.RegionId, SettlementId = data.SettlementId, LocationLeft = data.LocationLeft, LocationRight = data.LocationRight, SurfaceTypeId = data.SurfaceTypeId, AreaStopAvailability = data.AreaStopAvailability, AreaLandAvailability = data.AreaLandAvailability, PocketAvailability = data.PocketAvailability, ToiletAvailability = data.ToiletAvailability, YearBuild = data.YearBuild, YearRepair = data.YearRepair, StateCommonId = data.StateCommonId }; return Model; } public async Task DeleteAsync(int Id) { var busStop = await _context.BusStop.SingleOrDefaultAsync(x => x.Id == Id); _context.BusStop.Remove(busStop); return await _context.SaveChangesAsync(); } } }