Commit 21446fef908f286ef6fd5666ac61e4b1183f4652
add marge commit
Showing
5 changed files
with
136 additions
and
51 deletions
Show diff stats
src/Maps/Controllers/BusStopController.cs
... | ... | @@ -35,8 +35,7 @@ namespace Maps.Controllers |
35 | 35 | [HttpGet] |
36 | 36 | public async Task<IActionResult> Index([FromQuery] PaginationDsM data) |
37 | 37 | { |
38 | - try | |
39 | - { | |
38 | + | |
40 | 39 | var busStops = await _busStopDs.GetIndexListAsync(data); |
41 | 40 | |
42 | 41 | BusStopListVm vm = new BusStopListVm |
... | ... | @@ -45,16 +44,7 @@ namespace Maps.Controllers |
45 | 44 | }; |
46 | 45 | |
47 | 46 | return Json(vm); |
48 | - } | |
49 | - catch (NullReferenceException) | |
50 | - { | |
51 | - Response.StatusCode = 400; | |
52 | - return Json("There is no field with name " + data.sort); | |
53 | - } | |
54 | - catch (Exception) | |
55 | - { | |
56 | - return NotFound(); | |
57 | - } | |
47 | + | |
58 | 48 | |
59 | 49 | |
60 | 50 | } | ... | ... |
src/Maps/Startup.cs
... | ... | @@ -12,6 +12,7 @@ using MapsDb; |
12 | 12 | using MapsDb.Interfaces; |
13 | 13 | using MapsDb.DataService; |
14 | 14 | using MapsModels; |
15 | + | |
15 | 16 | namespace Maps |
16 | 17 | { |
17 | 18 | public class Startup |
... | ... | @@ -59,6 +60,7 @@ namespace Maps |
59 | 60 | services.AddScoped<IRoadWidthDs, RoadWidthDs>(); |
60 | 61 | services.AddScoped<IFlowIntensityDs, FlowIntensityDs>(); |
61 | 62 | services.AddScoped<ICrossSectionDs, CrossSectionDs>(); |
63 | + services.AddScoped<IRoadTypeDs, RoadTypeDs>(); | |
62 | 64 | // Add framework services. |
63 | 65 | services.AddApplicationInsightsTelemetry(Configuration); |
64 | 66 | ... | ... |
src/Maps/project.json
src/MapsDb/DataService/BusStopDs.cs
1 | +using System; | |
1 | 2 | using System.Collections.Generic; |
2 | 3 | using System.Linq; |
3 | 4 | using System.Reflection; |
5 | +using System.Text.RegularExpressions; | |
4 | 6 | using System.Threading.Tasks; |
7 | +using AutoMapper; | |
5 | 8 | using MapsDb.Interfaces; |
6 | 9 | using MapsDb.Models; |
7 | 10 | using MapsModels.DsModels; |
8 | 11 | using Microsoft.EntityFrameworkCore; |
12 | + | |
9 | 13 | namespace MapsDb.DataService |
10 | 14 | { |
11 | 15 | public class BusStopDs : IBusStopDs |
... | ... | @@ -13,41 +17,42 @@ namespace MapsDb.DataService |
13 | 17 | private PostgresDbContext _context; |
14 | 18 | public BusStopDs(){ |
15 | 19 | _context = new PostgresDbContext(); |
20 | + Mapper.Initialize(cnf => { | |
21 | + cnf.CreateMap<BusStop, BusStopEditDsM>(); | |
22 | + cnf.CreateMap<BusStopEditDsM, BusStop>(); | |
23 | + }); | |
16 | 24 | } |
17 | 25 | public Task<IList<BusStopEditDsM>> GetIndexListAsync(PaginationDsM pagination){ |
18 | 26 | return Task.Factory.StartNew(()=> { return GetAllBusStop(pagination); }); |
19 | 27 | } |
20 | 28 | private IList<BusStopEditDsM> GetAllBusStop(PaginationDsM pagination) |
21 | 29 | { |
22 | - var filter = "ะััะผ"; | |
23 | - var data = _context.BusStop.Where(x => x.Region.Name == filter ).Select(BusStop => new BusStopEditDsM | |
24 | - { | |
25 | - Id = BusStop.Id, | |
26 | - RoadId = BusStop.RoadId, | |
27 | - RegionId = BusStop.RegionId, | |
28 | - SettlementId = BusStop.SettlementId, | |
29 | - LocationLeft = BusStop.LocationLeft, | |
30 | - LocationRight = BusStop.LocationRight, | |
31 | - SurfaceTypeId = BusStop.SurfaceTypeId, | |
32 | - AreaStopAvailability = BusStop.AreaStopAvailability, | |
33 | - AreaLandAvailability = BusStop.AreaLandAvailability, | |
34 | - PocketAvailability = BusStop.PocketAvailability, | |
35 | - ToiletAvailability = BusStop.ToiletAvailability, | |
36 | - YearBuild = BusStop.YearBuild, | |
37 | - YearRepair = BusStop.YearRepair, | |
38 | - StateCommonId = BusStop.StateCommonId | |
39 | - }).Skip(pagination.from).Take(pagination.perPage); | |
40 | - | |
30 | + | |
31 | + var filter = pagination.filter; | |
32 | + | |
33 | + IQueryable<BusStop> data = _context.BusStop | |
34 | + .Include(d=>d.Road) | |
35 | + .Include(d=>d.Region) | |
36 | + .Include(d=>d.SurfaceType) | |
37 | + .Include(d=>d.StateCommon); | |
38 | + data = Filtering(pagination.filter, data); | |
39 | + //.Where(d => d.Region.Name == filter); | |
40 | + | |
41 | + IQueryable<BusStopEditDsM> result = data | |
42 | + .Select(BusStop => Mapper.Map<BusStopEditDsM>(BusStop)) | |
43 | + .Skip(pagination.from) | |
44 | + .Take(pagination.perPage); | |
45 | + | |
41 | 46 | switch (pagination.orderType()) |
42 | 47 | { |
43 | 48 | case "ASC": |
44 | - return data.OrderBy(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList(); | |
49 | + return result.OrderBy(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList(); | |
45 | 50 | |
46 | 51 | case "DESC": |
47 | - return data.OrderByDescending(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList(); | |
52 | + return result.OrderByDescending(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList(); | |
48 | 53 | |
49 | 54 | default: |
50 | - return data.OrderByDescending(i => i.Id).ToList(); | |
55 | + return result.OrderByDescending(i => i.Id).ToList(); | |
51 | 56 | } |
52 | 57 | |
53 | 58 | } |
... | ... | @@ -75,23 +80,109 @@ namespace MapsDb.DataService |
75 | 80 | return Model; |
76 | 81 | } |
77 | 82 | public BusStop InsertModel(BusStopEditDsM data){ |
78 | - BusStop Model = new BusStop{ | |
79 | - RoadId = data.RoadId, | |
80 | - RegionId = data.RegionId, | |
81 | - SettlementId = data.SettlementId, | |
82 | - LocationLeft = data.LocationLeft, | |
83 | - LocationRight = data.LocationRight, | |
84 | - SurfaceTypeId = data.SurfaceTypeId, | |
85 | - AreaStopAvailability = data.AreaStopAvailability, | |
86 | - AreaLandAvailability = data.AreaLandAvailability, | |
87 | - PocketAvailability = data.PocketAvailability, | |
88 | - ToiletAvailability = data.ToiletAvailability, | |
89 | - YearBuild = data.YearBuild, | |
90 | - YearRepair = data.YearRepair, | |
91 | - StateCommonId = data.StateCommonId | |
92 | - }; | |
83 | + BusStop Model = Mapper.Map<BusStop>(data); | |
93 | 84 | return Model; |
94 | 85 | } |
86 | + public IQueryable<BusStop> Filtering(string filters, IQueryable<BusStop> data){ | |
87 | + if(filters != null){ | |
88 | + string[] FilterWords = filters.Split(';').Where(x => !string.IsNullOrEmpty(x)).ToArray(); | |
89 | + foreach(string word in FilterWords){ | |
90 | + string[] filter = Regex.Split(word.Substring(1), "(.*)_(.*)"); | |
91 | + if(filter.Length < 3 ){ | |
92 | + continue; | |
93 | + } | |
94 | + string field = char.ToUpper(filter[1][0]) + filter[1].Substring(1); | |
95 | + if(word.StartsWith("$")){ | |
96 | + data = FilterByEndWith(field, filter[2], data); | |
97 | + } | |
98 | + else if(word.StartsWith("^")){ | |
99 | + data = FilterByStartWith(field, filter[2], data); | |
100 | + } | |
101 | + else if(word.StartsWith("*")){ | |
102 | + data = FilterByComtains(field, filter[2], data); | |
103 | + } | |
104 | + else if(word.StartsWith("!")){ | |
105 | + data = FilterByNotEquals(field, filter[2], data); | |
106 | + } | |
107 | + else if(word.StartsWith("=")){ | |
108 | + data = FilterByEquals(field, filter[2], data); | |
109 | + } | |
110 | + } | |
111 | + | |
112 | + } | |
113 | + return data; | |
114 | + } | |
115 | + public IQueryable<BusStop> FilterByComtains(string field, string param, IQueryable<BusStop> data){ | |
116 | + switch(field){ | |
117 | + case "RoadId": | |
118 | + return data.Where(i => i.Road.Name.Contains(param) ); | |
119 | + case "RegionId": | |
120 | + return data.Where(i => i.Region.Name.Contains(param) ); | |
121 | + case "SurfaceTypeId": | |
122 | + return data.Where(i => i.SurfaceType.Name.Contains(param) ); | |
123 | + case "StateCommonId": | |
124 | + return data.Where(i => i.StateCommon.Value.Contains(param) ); | |
125 | + default: | |
126 | + return data.Where(i => Convert.ToString(i.GetType().GetProperty(field).GetValue(i, null)).Contains(param) ); | |
127 | + } | |
128 | + } | |
129 | + public IQueryable<BusStop> FilterByEquals(string field, string param, IQueryable<BusStop> data){ | |
130 | + switch(field){ | |
131 | + case "RoadId": | |
132 | + return data.Where(i => i.Road.Name == param ); | |
133 | + case "RegionId": | |
134 | + return data.Where(i => i.Region.Name == param ); | |
135 | + case "SurfaceTypeId": | |
136 | + return data.Where(i => i.SurfaceType.Name == param ); | |
137 | + case "StateCommonId": | |
138 | + return data.Where(i => i.StateCommon.Value == param ); | |
139 | + default: | |
140 | + return data.Where(i => Convert.ToString(i.GetType().GetProperty(field).GetValue(i, null)) == param ); | |
141 | + } | |
142 | + | |
143 | + } | |
144 | + public IQueryable<BusStop> FilterByNotEquals(string field, string param, IQueryable<BusStop> data){ | |
145 | + switch(field){ | |
146 | + case "RoadId": | |
147 | + return data.Where(i => i.Road.Name != param ); | |
148 | + case "RegionId": | |
149 | + return data.Where(i => i.Region.Name != param ); | |
150 | + case "SurfaceTypeId": | |
151 | + return data.Where(i => i.SurfaceType.Name != param ); | |
152 | + case "StateCommonId": | |
153 | + return data.Where(i => i.StateCommon.Value != param ); | |
154 | + default: | |
155 | + return data.Where(i => Convert.ToString(i.GetType().GetProperty(field).GetValue(i, null)) != param ); | |
156 | + } | |
157 | + } | |
158 | + public IQueryable<BusStop> FilterByStartWith(string field, string param, IQueryable<BusStop> data){ | |
159 | + switch(field){ | |
160 | + case "RoadId": | |
161 | + return data.Where(i => i.Road.Name.StartsWith(param) ); | |
162 | + case "RegionId": | |
163 | + return data.Where(i => i.Region.Name.StartsWith(param) ); | |
164 | + case "SurfaceTypeId": | |
165 | + return data.Where(i => i.SurfaceType.Name.StartsWith(param) ); | |
166 | + case "StateCommonId": | |
167 | + return data.Where(i => i.StateCommon.Value.StartsWith(param) ); | |
168 | + default: | |
169 | + return data.Where(i => Convert.ToString(i.GetType().GetProperty(field).GetValue(i, null)).StartsWith(param) ); | |
170 | + } | |
171 | + } | |
172 | + public IQueryable<BusStop> FilterByEndWith(string field, string param, IQueryable<BusStop> data){ | |
173 | + switch(field){ | |
174 | + case "RoadId": | |
175 | + return data.Where(i => i.Road.Name.EndsWith(param) ); | |
176 | + case "RegionId": | |
177 | + return data.Where(i => i.Region.Name.EndsWith(param) ); | |
178 | + case "SurfaceTypeId": | |
179 | + return data.Where(i => i.SurfaceType.Name.EndsWith(param) ); | |
180 | + case "StateCommonId": | |
181 | + return data.Where(i => i.StateCommon.Value.EndsWith(param) ); | |
182 | + default: | |
183 | + return data.Where(i => Convert.ToString(i.GetType().GetProperty(field).GetValue(i, null)).EndsWith(param) ); | |
184 | + } | |
185 | + } | |
95 | 186 | public async Task<int> DeleteAsync(int Id) |
96 | 187 | { |
97 | 188 | var busStop = await _context.BusStop.SingleOrDefaultAsync(x => x.Id == Id); | ... | ... |
src/MapsDb/project.json