Blame view

src/MapsDb/DataService/BusStopDs.cs 2.72 KB
32912d0b   Administrator   first commit
1
2
  using System.Collections.Generic;
  using System.Linq;
db607025   Administrator   add pagination
3
  using System.Reflection;
32912d0b   Administrator   first commit
4
  using System.Threading.Tasks;
4f948d42   Yarik   Mapper
5
  using AutoMapper;
b9b3b8dd   Administrator   add deteils and c...
6
  using MapsDb.Interfaces;
32912d0b   Administrator   first commit
7
8
  using MapsDb.Models;
  using MapsModels.DsModels;
94ffda14   Yarik   Delete action
9
  using Microsoft.EntityFrameworkCore;
4f948d42   Yarik   Mapper
10
  
32912d0b   Administrator   first commit
11
12
13
14
15
16
17
  namespace MapsDb.DataService
  {
      public class BusStopDs : IBusStopDs
      {
          private PostgresDbContext _context;
          public BusStopDs(){
              _context = new PostgresDbContext();
4f948d42   Yarik   Mapper
18
19
20
21
              Mapper.Initialize(cnf => {
                  cnf.CreateMap<BusStop, BusStopEditDsM>();
                  cnf.CreateMap<BusStopEditDsM, BusStop>();
              });            
32912d0b   Administrator   first commit
22
          }
db607025   Administrator   add pagination
23
24
          public Task<IList<BusStopEditDsM>> GetIndexListAsync(PaginationDsM pagination){
              return Task.Factory.StartNew(()=> { return GetAllBusStop(pagination); });
32912d0b   Administrator   first commit
25
          }
db607025   Administrator   add pagination
26
          private IList<BusStopEditDsM> GetAllBusStop(PaginationDsM pagination)
32912d0b   Administrator   first commit
27
          {
4f948d42   Yarik   Mapper
28
              var data =  _context.BusStop.Select(BusStop => Mapper.Map<BusStopEditDsM>(BusStop)).Skip(pagination.from).Take(pagination.perPage);
db607025   Administrator   add pagination
29
30
31
32
33
34
35
36
37
              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:
8940e931   Administrator   add default sort
38
                      return data.OrderByDescending(i => i.Id).ToList();
db607025   Administrator   add pagination
39
40
              }     
              
32912d0b   Administrator   first commit
41
42
          }
  
e02ee314   Administrator   add update and cr...
43
44
          public Task<BusStop> CreateAsync(BusStopEditDsM data){
                return Task.Factory.StartNew(()=> { return Create(data); });
32912d0b   Administrator   first commit
45
          }
e02ee314   Administrator   add update and cr...
46
          private BusStop Create(BusStopEditDsM data)
abec55bf   Administrator   Finish busStop mo...
47
          {   
e02ee314   Administrator   add update and cr...
48
49
50
              
              BusStop Model = InsertModel(data);
              _context.BusStop.Add(Model);
abec55bf   Administrator   Finish busStop mo...
51
              _context.SaveChanges();
e02ee314   Administrator   add update and cr...
52
53
54
55
56
57
58
59
              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);
db607025   Administrator   add pagination
60
              Model.Id = id;
e02ee314   Administrator   add update and cr...
61
62
63
64
65
              _context.BusStop.Update(Model);
              _context.SaveChanges();
              return Model;
          }
          public BusStop InsertModel(BusStopEditDsM data){
4f948d42   Yarik   Mapper
66
              BusStop Model = Mapper.Map<BusStop>(data);
e02ee314   Administrator   add update and cr...
67
              return Model;
32912d0b   Administrator   first commit
68
          }
e02ee314   Administrator   add update and cr...
69
          public async Task<int> DeleteAsync(int Id)
94ffda14   Yarik   Delete action
70
          {
db607025   Administrator   add pagination
71
              var busStop = await _context.BusStop.SingleOrDefaultAsync(x => x.Id == Id);
94ffda14   Yarik   Delete action
72
73
74
              _context.BusStop.Remove(busStop);
              return await _context.SaveChangesAsync();
          }
32912d0b   Administrator   first commit
75
76
      }
  }