RoadDs.cs
7.02 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MapsDb.Interfaces;
using MapsDb.Models;
using MapsModels.DsModels;
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using MapsModels.ViewModels;
namespace MapsDb.DataService
{
public class Test {
public int Id {get; set;}
public IEnumerable<Node> Points {get; set;}
}
public class RoadDs : IRoadDs
{
private PostgresDbContext _context;
public RoadDs(){
_context = new PostgresDbContext();
}
public Task<IList<RoadSelectListDsM>> GetSelectListAsync(){
return Task.Factory.StartNew(GetSelectList);
}
private IList<RoadSelectListDsM> GetSelectList()
{
return _context.Road.Select(x => new RoadSelectListDsM
{
RoadId = x.Id,
Name = x.Name
}).ToList();
}
public Task<IList<RoadEditDsM>> GetIndexListAsync(PaginationDsM pagination){
return Task.Factory.StartNew(()=> { return GetAllRoad(pagination); });
}
private IList<RoadEditDsM> GetAllRoad(PaginationDsM pagination)
{
var data = _context.Road.Select(Road => new RoadEditDsM
{
Id = Road.Id,
Name = Road.Name,
Value = Road.Value,
Length = Road.Length,
HistoricalBackground = Road.HistoricalBackground,
EconomicValue = Road.EconomicValue,
LawDoc = Road.LawDoc,
AcceptTransferDoc = Road.AcceptTransferDoc,
AcceptanceDoc = Road.AcceptanceDoc,
AuthorityAct = Road.AuthorityAct,
RoadTypeId = Road.RoadTypeId,
Index = Road.Index
}).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<Road> CreateAsync(RoadEditDsM data){
return Task.Factory.StartNew(()=> { return Create(data); });
}
private Road Create(RoadEditDsM data)
{
Road Model = InsertModel(data);
_context.Road.Add(Model);
_context.SaveChanges();
return Model;
}
public Task<Road> UpdateAsync(RoadEditDsM data, int id){
return Task.Factory.StartNew(()=> { return Update(data, id); });
}
private Road Update(RoadEditDsM data, int id)
{
Road Model = InsertModel(data);
Model.Id = id;
_context.Road.Update(Model);
_context.SaveChanges();
return Model;
}
public Road InsertModel(RoadEditDsM data){
Road Model = new Road{
Name = data.Name,
Value = data.Value,
Length = data.Length,
HistoricalBackground = data.HistoricalBackground,
EconomicValue = data.EconomicValue,
LawDoc = data.LawDoc,
AcceptTransferDoc = data.AcceptTransferDoc,
AcceptanceDoc = data.AcceptanceDoc,
AuthorityAct = data.AuthorityAct,
RoadTypeId = data.RoadTypeId,
Index = data.Index
};
return Model;
}
public async Task<int> DeleteAsync(int Id)
{
var road = await _context.Road.SingleOrDefaultAsync(x => x.Id == Id);
_context.Road.Remove(road);
return await _context.SaveChangesAsync();
}
public List<WayLook> GetRelationAsync(int Id)
{
IQueryable<Way> ways = _context.Way.Where(x => x.WayToRelation.Any(y => y.Relation.RelationToRoad.Any(z => z.RoadId == Id)));
List<NodeUnsortLook> list = (from way in ways
join nodeToWay in _context.NodeToWay on way.Id equals nodeToWay.WayId
join node in _context.Node on nodeToWay.NodeId equals node.Id
orderby way.Id, nodeToWay.Index
select new NodeUnsortLook {
NodeId = node.Id,
Lat = node.Lat,
Lon = node.Lon,
Index = nodeToWay.Index,
WayId = way.Id
}).ToList();
List<WayLook> result = new List<WayLook>();
foreach (NodeUnsortLook nodeUnsort in list) {
WayLook item = result.SingleOrDefault(x => x.Id == nodeUnsort.WayId);
if (item == null) {
item = new WayLook() {
Id = nodeUnsort.WayId,
Nodes = new List<NodeLook>()
};
result.Add(item);
}
item.Nodes.Add(new NodeLook() {
Id = nodeUnsort.NodeId,
Index = nodeUnsort.Index,
Lat = nodeUnsort.Lat,
Lon = nodeUnsort.Lon
});
}
return result;
}
public RoadVm GetByWay(int Id)
{
RoadVm road = _context.Road.Where(x => x.RelationToRoad.Any(y => y.Relation.WayToRelation.Any(z => z.WayId == Id))).Include(t => t.RoadType).Select(r => new RoadVm{
Id = r.Id,
Name = r.Name,
Value = r.Value,
Length = r.Length,
HistoricalBackground = r.HistoricalBackground,
EconomicValue = r.EconomicValue,
LawDoc = r.LawDoc,
AcceptTransferDoc = r.AcceptTransferDoc,
AcceptanceDoc = r.AcceptanceDoc,
AuthorityAct = r.AuthorityAct,
RoadType = r.RoadType.Value,
Index = r.Index
}).FirstOrDefault();
return road;
}
public RoadVm GetByNode(int Id)
{
RoadVm road = _context.Road.Where(x => x.RelationToRoad.Any(y => y.Relation.WayToRelation.Any(z => z.Way.NodeToWay.Any(a => a.NodeId == Id)))).Include(t => t.RoadType).Select(r => new RoadVm{
Id = r.Id,
Name = r.Name,
Value = r.Value,
Length = r.Length,
HistoricalBackground = r.HistoricalBackground,
EconomicValue = r.EconomicValue,
LawDoc = r.LawDoc,
AcceptTransferDoc = r.AcceptTransferDoc,
AcceptanceDoc = r.AcceptanceDoc,
AuthorityAct = r.AuthorityAct,
RoadType = r.RoadType.Value,
Index = r.Index
}).FirstOrDefault();
return road;
}
}
}