BusStopDs.cs
3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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<IList<BusStopEditDsM>> GetIndexListAsync(PaginationDsM pagination){
return Task.Factory.StartNew(()=> { return GetAllBusStop(pagination); });
}
private IList<BusStopEditDsM> 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.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 = 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<int> DeleteAsync(int Id)
{
var busStop = await _context.BusStop.SingleOrDefaultAsync(x => x.Id == Id);
_context.BusStop.Remove(busStop);
return await _context.SaveChangesAsync();
}
}
}