Commit f2ca21bc359dced3f5ee59ca7106cbe6935e119f

Authored by Yarik
2 parents 6b2a9454 43dfbc98

Merge branch 'master' of gitlab.artweb.com.ua:root/FirstFullmaps

src/Maps/Controllers/BusStopController.cs
@@ -35,8 +35,7 @@ namespace Maps.Controllers @@ -35,8 +35,7 @@ namespace Maps.Controllers
35 [HttpGet] 35 [HttpGet]
36 public async Task<IActionResult> Index([FromQuery] PaginationDsM data) 36 public async Task<IActionResult> Index([FromQuery] PaginationDsM data)
37 { 37 {
38 - try  
39 - { 38 +
40 var busStops = await _busStopDs.GetIndexListAsync(data); 39 var busStops = await _busStopDs.GetIndexListAsync(data);
41 40
42 BusStopListVm vm = new BusStopListVm 41 BusStopListVm vm = new BusStopListVm
@@ -45,16 +44,7 @@ namespace Maps.Controllers @@ -45,16 +44,7 @@ namespace Maps.Controllers
45 }; 44 };
46 45
47 return Json(vm); 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/MapsDb/DataService/BusStopDs.cs
  1 +using System;
1 using System.Collections.Generic; 2 using System.Collections.Generic;
2 using System.Linq; 3 using System.Linq;
3 using System.Reflection; 4 using System.Reflection;
  5 +using System.Text.RegularExpressions;
4 using System.Threading.Tasks; 6 using System.Threading.Tasks;
5 using AutoMapper; 7 using AutoMapper;
6 using MapsDb.Interfaces; 8 using MapsDb.Interfaces;
@@ -21,17 +23,32 @@ namespace MapsDb.DataService @@ -21,17 +23,32 @@ namespace MapsDb.DataService
21 } 23 }
22 private IList<BusStopEditDsM> GetAllBusStop(PaginationDsM pagination) 24 private IList<BusStopEditDsM> GetAllBusStop(PaginationDsM pagination)
23 { 25 {
24 - var data = _context.BusStop.Select(BusStop => Mapper.Map<BusStopEditDsM>(BusStop)).Skip(pagination.from).Take(pagination.perPage); 26 +
  27 + var filter = pagination.filter;
  28 +
  29 + IQueryable<BusStop> data = _context.BusStop
  30 + .Include(d=>d.Road)
  31 + .Include(d=>d.Region)
  32 + .Include(d=>d.SurfaceType)
  33 + .Include(d=>d.StateCommon);
  34 + data = Filtering(pagination.filter, data);
  35 + //.Where(d => d.Region.Name == filter);
  36 +
  37 + IQueryable<BusStopEditDsM> result = data
  38 + .Select(BusStop => Mapper.Map<BusStopEditDsM>(BusStop))
  39 + .Skip(pagination.from)
  40 + .Take(pagination.perPage);
  41 +
25 switch (pagination.orderType()) 42 switch (pagination.orderType())
26 { 43 {
27 case "ASC": 44 case "ASC":
28 - return data.OrderBy(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList(); 45 + return result.OrderBy(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList();
29 46
30 case "DESC": 47 case "DESC":
31 - return data.OrderByDescending(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList(); 48 + return result.OrderByDescending(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList();
32 49
33 default: 50 default:
34 - return data.OrderByDescending(i => i.Id).ToList(); 51 + return result.OrderByDescending(i => i.Id).ToList();
35 } 52 }
36 53
37 } 54 }
@@ -62,6 +79,106 @@ namespace MapsDb.DataService @@ -62,6 +79,106 @@ namespace MapsDb.DataService
62 BusStop Model = Mapper.Map<BusStop>(data); 79 BusStop Model = Mapper.Map<BusStop>(data);
63 return Model; 80 return Model;
64 } 81 }
  82 + public IQueryable<BusStop> Filtering(string filters, IQueryable<BusStop> data){
  83 + if(filters != null){
  84 + string[] FilterWords = filters.Split(';').Where(x => !string.IsNullOrEmpty(x)).ToArray();
  85 + foreach(string word in FilterWords){
  86 + string[] filter = Regex.Split(word.Substring(1), "(.*)_(.*)");
  87 + if(filter.Length < 3 ){
  88 + continue;
  89 + }
  90 + string field = char.ToUpper(filter[1][0]) + filter[1].Substring(1);
  91 + if(word.StartsWith("$")){
  92 + data = FilterByEndWith(field, filter[2], data);
  93 + }
  94 + else if(word.StartsWith("^")){
  95 + data = FilterByStartWith(field, filter[2], data);
  96 + }
  97 + else if(word.StartsWith("*")){
  98 + data = FilterByComtains(field, filter[2], data);
  99 + }
  100 + else if(word.StartsWith("!")){
  101 + data = FilterByNotEquals(field, filter[2], data);
  102 + }
  103 + else if(word.StartsWith("=")){
  104 + data = FilterByEquals(field, filter[2], data);
  105 + }
  106 + }
  107 +
  108 + }
  109 + return data;
  110 + }
  111 + public IQueryable<BusStop> FilterByComtains(string field, string param, IQueryable<BusStop> data){
  112 + switch(field){
  113 + case "RoadId":
  114 + return data.Where(i => i.Road.Name.Contains(param) );
  115 + case "RegionId":
  116 + return data.Where(i => i.Region.Name.Contains(param) );
  117 + case "SurfaceTypeId":
  118 + return data.Where(i => i.SurfaceType.Name.Contains(param) );
  119 + case "StateCommonId":
  120 + return data.Where(i => i.StateCommon.Value.Contains(param) );
  121 + default:
  122 + return data.Where(i => Convert.ToString(i.GetType().GetProperty(field).GetValue(i, null)).Contains(param) );
  123 + }
  124 + }
  125 + public IQueryable<BusStop> FilterByEquals(string field, string param, IQueryable<BusStop> data){
  126 + switch(field){
  127 + case "RoadId":
  128 + return data.Where(i => i.Road.Name == param );
  129 + case "RegionId":
  130 + return data.Where(i => i.Region.Name == param );
  131 + case "SurfaceTypeId":
  132 + return data.Where(i => i.SurfaceType.Name == param );
  133 + case "StateCommonId":
  134 + return data.Where(i => i.StateCommon.Value == param );
  135 + default:
  136 + return data.Where(i => Convert.ToString(i.GetType().GetProperty(field).GetValue(i, null)) == param );
  137 + }
  138 +
  139 + }
  140 + public IQueryable<BusStop> FilterByNotEquals(string field, string param, IQueryable<BusStop> data){
  141 + switch(field){
  142 + case "RoadId":
  143 + return data.Where(i => i.Road.Name != param );
  144 + case "RegionId":
  145 + return data.Where(i => i.Region.Name != param );
  146 + case "SurfaceTypeId":
  147 + return data.Where(i => i.SurfaceType.Name != param );
  148 + case "StateCommonId":
  149 + return data.Where(i => i.StateCommon.Value != param );
  150 + default:
  151 + return data.Where(i => Convert.ToString(i.GetType().GetProperty(field).GetValue(i, null)) != param );
  152 + }
  153 + }
  154 + public IQueryable<BusStop> FilterByStartWith(string field, string param, IQueryable<BusStop> data){
  155 + switch(field){
  156 + case "RoadId":
  157 + return data.Where(i => i.Road.Name.StartsWith(param) );
  158 + case "RegionId":
  159 + return data.Where(i => i.Region.Name.StartsWith(param) );
  160 + case "SurfaceTypeId":
  161 + return data.Where(i => i.SurfaceType.Name.StartsWith(param) );
  162 + case "StateCommonId":
  163 + return data.Where(i => i.StateCommon.Value.StartsWith(param) );
  164 + default:
  165 + return data.Where(i => Convert.ToString(i.GetType().GetProperty(field).GetValue(i, null)).StartsWith(param) );
  166 + }
  167 + }
  168 + public IQueryable<BusStop> FilterByEndWith(string field, string param, IQueryable<BusStop> data){
  169 + switch(field){
  170 + case "RoadId":
  171 + return data.Where(i => i.Road.Name.EndsWith(param) );
  172 + case "RegionId":
  173 + return data.Where(i => i.Region.Name.EndsWith(param) );
  174 + case "SurfaceTypeId":
  175 + return data.Where(i => i.SurfaceType.Name.EndsWith(param) );
  176 + case "StateCommonId":
  177 + return data.Where(i => i.StateCommon.Value.EndsWith(param) );
  178 + default:
  179 + return data.Where(i => Convert.ToString(i.GetType().GetProperty(field).GetValue(i, null)).EndsWith(param) );
  180 + }
  181 + }
65 public async Task<int> DeleteAsync(int Id) 182 public async Task<int> DeleteAsync(int Id)
66 { 183 {
67 var busStop = await _context.BusStop.SingleOrDefaultAsync(x => x.Id == Id); 184 var busStop = await _context.BusStop.SingleOrDefaultAsync(x => x.Id == Id);