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; using AutoMapper; namespace MapsDb.DataService { public class RoadToCategoryDs : IRoadToCategoryDs { private PostgresDbContext _context; public RoadToCategoryDs(){ _context = new PostgresDbContext(); } public Task> GetIndexListAsync(PaginationDsM pagination){ return Task.Factory.StartNew(()=> { return GetAllRoadToCategory(pagination); }); } private IList GetAllRoadToCategory(PaginationDsM pagination) { var data = _context.RoadToCategory .Select(RoadToCategory => Mapper.Map(RoadToCategory)) .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(RoadToCategoryEditDsM data){ return Task.Factory.StartNew(()=> { return Create(data); }); } private RoadToCategory Create(RoadToCategoryEditDsM data) { RoadToCategory Model = InsertModel(data); _context.RoadToCategory.Add(Model); _context.SaveChanges(); return Model; } public Task UpdateAsync(RoadToCategoryEditDsM data, int id){ return Task.Factory.StartNew(()=> { return Update(data, id); }); } private RoadToCategory Update(RoadToCategoryEditDsM data, int id) { RoadToCategory Model = InsertModel(data); Model.Id = id; _context.RoadToCategory.Update(Model); _context.SaveChanges(); return Model; } public RoadToCategory InsertModel(RoadToCategoryEditDsM data){ RoadToCategory Model = Mapper.Map(data); return Model; } public async Task DeleteAsync(int Id) { var RoadToCategory = await _context.RoadToCategory.SingleOrDefaultAsync(x => x.Id == Id); _context.RoadToCategory.Remove(RoadToCategory); return await _context.SaveChangesAsync(); } } }