Blame view

src/MapsDb/DataService/RoadServiceDs.cs 2.66 KB
8d5b2697   Yarik   RoadService
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
  using System.Collections.Generic;
  using System.Linq;
  using System.Reflection;
  using System.Threading.Tasks;
  using AutoMapper;
  using MapsDb.Interfaces;
  using MapsDb.Models;
  using MapsModels.DsModels;
  using Microsoft.EntityFrameworkCore;
  
  namespace MapsDb.DataService
  {
      public class RoadServiceDs : IRoadServiceDs
      {
          private PostgresDbContext _context;
          public RoadServiceDs(){
              _context = new PostgresDbContext();
          }
          public Task<IList<RoadServiceEditDsM>> GetIndexListAsync(PaginationDsM pagination){
              return Task.Factory.StartNew(()=> { return GetAllRoadService(pagination); });
          }
          private IList<RoadServiceEditDsM> GetAllRoadService(PaginationDsM pagination)
          {
              var data =  _context.RoadService.Select(RoadService => Mapper.Map<RoadServiceEditDsM>(RoadService)).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<RoadService> CreateAsync(RoadServiceEditDsM data){
                return Task.Factory.StartNew(()=> { return Create(data); });
          }
          private RoadService Create(RoadServiceEditDsM data)
          {   
              
              RoadService Model = InsertModel(data);
              _context.RoadService.Add(Model);
              _context.SaveChanges();
              return Model;
          }
          public Task<RoadService> UpdateAsync(RoadServiceEditDsM data, int id){
              return Task.Factory.StartNew(()=> { return Update(data, id); });
          }
          private RoadService Update(RoadServiceEditDsM data, int id)
          {   
              RoadService Model = InsertModel(data);
              Model.Id = id;
              _context.RoadService.Update(Model);
              _context.SaveChanges();
              return Model;
          }
          public RoadService InsertModel(RoadServiceEditDsM data){
              RoadService Model = Mapper.Map<RoadService>(data);
              return Model;
          }
          public async Task<int> DeleteAsync(int Id)
          {
              var roadService = await _context.RoadService.SingleOrDefaultAsync(x => x.Id == Id);
              _context.RoadService.Remove(roadService);
              return await _context.SaveChangesAsync();
          }
      }
  }