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 RoadWidthDs : IRoadWidthDs { private PostgresDbContext _context; public RoadWidthDs(){ _context = new PostgresDbContext(); } public Task> GetIndexListAsync(PaginationDsM pagination){ return Task.Factory.StartNew(()=> { return GetAllRoadWidth(pagination); }); } private IList GetAllRoadWidth(PaginationDsM pagination) { var data = _context.RoadWidth.Select(RoadWidth => new RoadWidthEditDsM { Id = RoadWidth.Id, RoadId = RoadWidth.RoadId, RegionId = RoadWidth.RegionId, Begin = RoadWidth.Begin, End = RoadWidth.End, WidthRoadsideLeft = RoadWidth.WidthRoadsideLeft, WidthReverseRoad = RoadWidth.WidthReverseRoad, WidthStrip = RoadWidth.WidthStrip, WidthRoadwayForward = RoadWidth.WidthRoadwayForward, WidthRoadsideRight = RoadWidth.WidthRoadsideRight, CountLaneLeft = RoadWidth.CountLaneLeft, CountLaneRight = RoadWidth.CountLaneRight, }).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 CreateAsync(RoadWidthEditDsM data){ return Task.Factory.StartNew(()=> { return Create(data); }); } private RoadWidth Create(RoadWidthEditDsM data) { RoadWidth Model = InsertModel(data); _context.RoadWidth.Add(Model); _context.SaveChanges(); return Model; } public Task UpdateAsync(RoadWidthEditDsM data, int id){ return Task.Factory.StartNew(()=> { return Update(data, id); }); } private RoadWidth Update(RoadWidthEditDsM data, int id) { RoadWidth Model = InsertModel(data); Model.Id = id; _context.RoadWidth.Update(Model); _context.SaveChanges(); return Model; } public RoadWidth InsertModel(RoadWidthEditDsM data){ RoadWidth Model = new RoadWidth{ RoadId = data.RoadId, RegionId = data.RegionId, Begin = data.Begin, End = data.End, WidthRoadsideLeft = data.WidthRoadsideLeft, WidthReverseRoad = data.WidthReverseRoad, WidthStrip = data.WidthStrip, WidthRoadwayForward = data.WidthRoadwayForward, WidthRoadsideRight = data.WidthRoadsideRight, CountLaneLeft = data.CountLaneLeft, CountLaneRight = data.CountLaneRight, }; return Model; } public async Task DeleteAsync(int Id) { var RoadWidth = await _context.RoadWidth.SingleOrDefaultAsync(x => x.Id == Id); _context.RoadWidth.Remove(RoadWidth); return await _context.SaveChangesAsync(); } } }