BusStopDs.cs
3.94 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
using System.Collections.Generic;
using System.Linq;
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(){
return Task.Factory.StartNew(GetAllBusStop);
}
private IList<BusStopEditDsM> GetAllBusStop()
{
return _context.BusStop.Select(busStop => new BusStopEditDsM
{
BusStopId = busStop.BusStopId,
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
}).OrderByDescending(BusStop => BusStop.BusStopId).ToList();
}
public Task<BusStop> SaveAsync(BusStopEditDsM busStop, int? id = null){
return Task.Factory.StartNew(()=> { return Save(busStop, id); });
}
private BusStop Save(BusStopEditDsM busStop, int? id)
{
BusStop Bs = new BusStop{
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
};
var busStopFromDb = _context.BusStop.FirstOrDefault(x => x.BusStopId == id);
if(busStopFromDb != null)
{
busStopFromDb.RoadId = busStop.RoadId;
busStopFromDb.RegionId = busStop.RegionId;
busStopFromDb.SettlementId = busStop.SettlementId;
busStopFromDb.LocationLeft = busStop.LocationLeft;
busStopFromDb.LocationRight = busStop.LocationRight;
busStopFromDb.SurfaceTypeId = busStop.SurfaceTypeId;
busStopFromDb.AreaStopAvailability = busStop.AreaStopAvailability;
busStopFromDb.AreaLandAvailability = busStop.AreaLandAvailability;
busStopFromDb.PocketAvailability = busStop.PocketAvailability;
busStopFromDb.ToiletAvailability = busStop.ToiletAvailability;
busStopFromDb.YearBuild = busStop.YearBuild;
busStopFromDb.YearRepair = busStop.YearRepair;
busStopFromDb.StateCommonId = busStop.StateCommonId;
}
else
{
_context.BusStop.Add(Bs);
}
_context.SaveChanges();
return Bs;
}
public async Task<int> DeleteAsync(int? Id)
{
var busStop = await _context.BusStop.SingleOrDefaultAsync(x => x.BusStopId == Id);
_context.BusStop.Remove(busStop);
return await _context.SaveChangesAsync();
}
}
}