using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Threading.Tasks; using AutoMapper; using MapsDb.Interfaces; using MapsDb.Models; using MapsModels.DsModels; using Microsoft.EntityFrameworkCore; namespace MapsDb.DataService { public class SettlementAddressLinkDs : ISettlementAddressLinkDs { private PostgresDbContext _context; public SettlementAddressLinkDs(){ _context = new PostgresDbContext(); } public Task> GetIndexListAsync(PaginationDsM pagination){ return Task.Factory.StartNew(()=> { return GetAllSettlementAddressLink(pagination); }); } private IList GetAllSettlementAddressLink(PaginationDsM pagination) { var data = _context.SettlementAddressLink.Select(SettlementAddressLink => Mapper.Map(SettlementAddressLink)).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(SettlementAddressLinkEditDsM data){ return Task.Factory.StartNew(()=> { return Create(data); }); } private SettlementAddressLink Create(SettlementAddressLinkEditDsM data) { SettlementAddressLink Model = InsertModel(data); _context.SettlementAddressLink.Add(Model); _context.SaveChanges(); return Model; } public Task UpdateAsync(SettlementAddressLinkEditDsM data, int id){ return Task.Factory.StartNew(()=> { return Update(data, id); }); } private SettlementAddressLink Update(SettlementAddressLinkEditDsM data, int id) { SettlementAddressLink Model = InsertModel(data); Model.Id = id; _context.SettlementAddressLink.Update(Model); _context.SaveChanges(); return Model; } public SettlementAddressLink InsertModel(SettlementAddressLinkEditDsM data){ SettlementAddressLink Model = Mapper.Map(data); return Model; } public async Task DeleteAsync(int Id) { var settlementAddressLink = await _context.SettlementAddressLink.SingleOrDefaultAsync(x => x.Id == Id); _context.SettlementAddressLink.Remove(settlementAddressLink); return await _context.SaveChangesAsync(); } } }