Blame view

src/MapsDb/DataService/AuthorityDs.cs 7.18 KB
c386d9eb   Yarik   Big commit
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
  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 AuthorityDs : IAuthorityDs
      {
          private PostgresDbContext _context;
          public AuthorityDs(){
              _context = new PostgresDbContext();
          }
          public Task<IList<AuthorityEditDsM>> GetIndexListAsync(PaginationDsM pagination){
              return Task.Factory.StartNew(()=> { return GetAllAuthority(pagination); });
          }
          private IList<AuthorityEditDsM> GetAllAuthority(PaginationDsM pagination)
          {
  
              var filter =  pagination.filter;
  
              IQueryable<Authority> data = _context.Authority
              .Include(d=>d.Road)
              .Include(d=>d.CrossSection);
  
97a3f7a5   Yarik   Authority and Con...
33
34
              data = Filtering(pagination.filter, data);
  
c386d9eb   Yarik   Big commit
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
              IQueryable<AuthorityEditDsM> result = data
              .Select(Authority => Mapper.Map<AuthorityEditDsM>(Authority))
              .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<Authority> CreateAsync(AuthorityEditDsM data){
                return Task.Factory.StartNew(()=> { return Create(data); });
          }
          private Authority Create(AuthorityEditDsM data)
          {   
              
              Authority Model = InsertModel(data);
              _context.Authority.Add(Model);
              _context.SaveChanges();
              return Model;
          }
          public Task<Authority> UpdateAsync(AuthorityEditDsM data, int id){
              return Task.Factory.StartNew(()=> { return Update(data, id); });
          }
          private Authority Update(AuthorityEditDsM data, int id)
          {   
              Authority Model = InsertModel(data);
              Model.Id = id;
              _context.Authority.Update(Model);
              _context.SaveChanges();
              return Model;
          }
          public Authority InsertModel(AuthorityEditDsM data){
              Authority Model = Mapper.Map<Authority>(data);
              return Model;
          }
          public async Task<int> DeleteAsync(int Id)
          {
              var authority = await _context.Authority.SingleOrDefaultAsync(x => x.Id == Id);
              _context.Authority.Remove(authority);
              return await _context.SaveChangesAsync();
          }
97a3f7a5   Yarik   Authority and Con...
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
          public IQueryable<Authority> Filtering(string filters, IQueryable<Authority> 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<Authority> FilterByComtains(string field, string param, IQueryable<Authority> data){
              switch(field){
                  case "RoadId":
                      return data.Where(i => i.Road.Name != null && i.Road.Name.ToLower().Contains(param) );
                  case "CrossSectionId":
                      return data.Where(i => i.CrossSection.Id.ToString().ToLower().Contains(param) );
                  default:
                      return data.Where(i => Convert.ToString(i.GetType().GetProperty(field).GetValue(i, null)).ToLower().Contains(param) );
              }
          }
          public IQueryable<Authority> FilterByEquals(string field, string param, IQueryable<Authority> data){
              switch(field){
                  case "RoadId":
                      return data.Where(i => i.Road.Id.ToString().ToLower() == param );
                  case "CrossSectionId":
                      return data.Where(i => i.CrossSection.Id.ToString().ToLower() == param );
                  default:
                  return data.Where(i => Convert.ToString(i.GetType().GetProperty(field).GetValue(i, null)).ToLower() == param );
              }
  
          }
          public IQueryable<Authority> FilterByNotEquals(string field, string param, IQueryable<Authority> data){
             switch(field){
                  case "RoadId":
                      return data.Where(i => i.Road.Name != null && i.Road.Name.ToLower() != param );
                  case "CrossSectionId":
                      return data.Where(i => i.CrossSection.Id.ToString().ToLower() != param );
                  default:
                  return data.Where(i => Convert.ToString(i.GetType().GetProperty(field).GetValue(i, null)).ToLower() != param );
              }
          }
          public IQueryable<Authority> FilterByStartWith(string field, string param, IQueryable<Authority> data){
              switch(field){
                  case "RoadId":
                      return data.Where(i => i.Road.Name != null && i.Road.Name.ToLower().StartsWith(param) );
                  case "CrossSectionId":
                      return data.Where(i => i.CrossSection.Id.ToString().ToLower().StartsWith(param) );
                  default:
                  return data.Where(i => Convert.ToString(i.GetType().GetProperty(field).GetValue(i, null)).ToLower().StartsWith(param) );
              }
          }
          public IQueryable<Authority> FilterByEndWith(string field, string param, IQueryable<Authority> data){
            switch(field){
                  case "RoadId":
                      return data.Where(i => i.Road.Name != null && i.Road.Name.ToLower().EndsWith(param) );
                  case "CrossSectionId":
                      return data.Where(i => i.CrossSection.Id.ToString().ToLower().EndsWith(param) );
                  default:
                  return data.Where(i => Convert.ToString(i.GetType().GetProperty(field).GetValue(i, null)).ToLower().EndsWith(param) );
              }
          }
c386d9eb   Yarik   Big commit
167
168
      }
  }