BusStopDs.cs
8.79 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
186
187
188
189
190
191
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using AutoMapper;
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 filter = pagination.filter;
IQueryable<BusStop> data = _context.BusStop
.Include(d=>d.Road)
.Include(d=>d.Region)
.Include(d=>d.SurfaceType)
.Include(d=>d.StateCommon);
data = Filtering(pagination.filter, data);
//.Where(d => d.Region.Name == filter);
IQueryable<BusStopEditDsM> result = data
.Select(BusStop => Mapper.Map<BusStopEditDsM>(BusStop))
.Skip(pagination.from)
.Take(pagination.perPage);
switch (pagination.orderType())
{
case "ASC":
return result.OrderBy(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList();
case "DESC":
return result.OrderByDescending(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList();
default:
return result.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 = Mapper.Map<BusStop>(data);
return Model;
}
public IQueryable<BusStop> Filtering(string filters, IQueryable<BusStop> data){
if(filters != null){
string[] FilterWords = filters.Split(';').Where(x => !string.IsNullOrEmpty(x)).ToArray();
foreach(string word in FilterWords){
string[] filter = Regex.Split(word.Substring(1), "(.*)_(.*)");
if(filter.Length < 3 ){
continue;
}
string field = char.ToUpper(filter[1][0]) + filter[1].Substring(1);
string param = filter[2].ToLower();
if(word.StartsWith("$")){
data = FilterByEndWith(field, param, data);
}
else if(word.StartsWith("^")){
data = FilterByStartWith(field, param, data);
}
else if(word.StartsWith("*")){
data = FilterByComtains(field, param, data);
}
else if(word.StartsWith("!")){
data = FilterByNotEquals(field, param, data);
}
else if(word.StartsWith("=")){
data = FilterByEquals(field, param, data);
}
}
}
return data;
}
public IQueryable<BusStop> FilterByComtains(string field, string param, IQueryable<BusStop> data){
switch(field){
case "RoadId":
return data.Where(i => i.Road.Name != null && i.Road.Name.ToLower().Contains(param) );
case "RegionId":
return data.Where(i => i.Region.Name != null && i.Region.Name.ToLower().Contains(param) );
case "SurfaceTypeId":
return data.Where(i => i.SurfaceType.Name != null && i.SurfaceType.Name.ToLower().Contains(param) );
case "StateCommonId":
return data.Where(i => i.StateCommon.Value != null && i.StateCommon.Value.ToLower().Contains(param) );
default:
return data.Where(i => Convert.ToString(i.GetType().GetProperty(field).GetValue(i, null)).ToLower().Contains(param) );
}
}
public IQueryable<BusStop> FilterByEquals(string field, string param, IQueryable<BusStop> data){
switch(field){
case "RoadId":
return data.Where(i => i.Road.Name != null && i.Road.Name.ToLower() == param );
case "RegionId":
return data.Where(i => i.Region.Name != null && i.Region.Name.ToLower() == param );
case "SurfaceTypeId":
return data.Where(i => i.SurfaceType.Name != null && i.SurfaceType.Name.ToLower() == param );
case "StateCommonId":
return data.Where(i => i.StateCommon.Value != null && i.StateCommon.Value.ToLower() == param );
default:
return data.Where(i => Convert.ToString(i.GetType().GetProperty(field).GetValue(i, null)).ToLower() == param );
}
}
public IQueryable<BusStop> FilterByNotEquals(string field, string param, IQueryable<BusStop> data){
switch(field){
case "RoadId":
return data.Where(i => i.Road.Name != null && i.Road.Name.ToLower() != param );
case "RegionId":
return data.Where(i => i.Region.Name != null && i.Region.Name.ToLower() != param );
case "SurfaceTypeId":
return data.Where(i => i.SurfaceType.Name != null && i.SurfaceType.Name.ToLower() != param );
case "StateCommonId":
return data.Where(i => i.StateCommon.Value != null && i.StateCommon.Value.ToLower() != param );
default:
return data.Where(i => Convert.ToString(i.GetType().GetProperty(field).GetValue(i, null)).ToLower() != param );
}
}
public IQueryable<BusStop> FilterByStartWith(string field, string param, IQueryable<BusStop> data){
switch(field){
case "RoadId":
return data.Where(i => i.Road.Name != null && i.Road.Name.ToLower().StartsWith(param) );
case "RegionId":
return data.Where(i => i.Region.Name != null && i.Region.Name.ToLower().StartsWith(param) );
case "SurfaceTypeId":
return data.Where(i => i.SurfaceType.Name != null && i.SurfaceType.Name.ToLower().StartsWith(param) );
case "StateCommonId":
return data.Where(i => i.StateCommon.Value != null && i.StateCommon.Value.ToLower().StartsWith(param) );
default:
return data.Where(i => Convert.ToString(i.GetType().GetProperty(field).GetValue(i, null)).ToLower().StartsWith(param) );
}
}
public IQueryable<BusStop> FilterByEndWith(string field, string param, IQueryable<BusStop> data){
switch(field){
case "RoadId":
return data.Where(i => i.Road.Name != null && i.Road.Name.ToLower().EndsWith(param) );
case "RegionId":
return data.Where(i => i.Region.Name != null && i.Region.Name.ToLower().EndsWith(param) );
case "SurfaceTypeId":
return data.Where(i => i.SurfaceType.Name != null && i.SurfaceType.Name.ToLower().EndsWith(param) );
case "StateCommonId":
return data.Where(i => i.StateCommon.Value != null && i.StateCommon.Value.ToLower().EndsWith(param) );
default:
return data.Where(i => Convert.ToString(i.GetType().GetProperty(field).GetValue(i, null)).ToLower().EndsWith(param) );
}
}
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();
}
}
}