Blame view

src/MapsDb/DataService/BusStopDs.cs 2.54 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();
32912d0b   Administrator   first commit
18
          }
db607025   Administrator   add pagination
19
20
          public Task<IList<BusStopEditDsM>> GetIndexListAsync(PaginationDsM pagination){
              return Task.Factory.StartNew(()=> { return GetAllBusStop(pagination); });
32912d0b   Administrator   first commit
21
          }
db607025   Administrator   add pagination
22
          private IList<BusStopEditDsM> GetAllBusStop(PaginationDsM pagination)
32912d0b   Administrator   first commit
23
          {
4f948d42   Yarik   Mapper
24
              var data =  _context.BusStop.Select(BusStop => Mapper.Map<BusStopEditDsM>(BusStop)).Skip(pagination.from).Take(pagination.perPage);
db607025   Administrator   add pagination
25
26
27
28
29
30
31
32
33
              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
34
                      return data.OrderByDescending(i => i.Id).ToList();
db607025   Administrator   add pagination
35
36
              }     
              
32912d0b   Administrator   first commit
37
38
          }
  
e02ee314   Administrator   add update and cr...
39
40
          public Task<BusStop> CreateAsync(BusStopEditDsM data){
                return Task.Factory.StartNew(()=> { return Create(data); });
32912d0b   Administrator   first commit
41
          }
e02ee314   Administrator   add update and cr...
42
          private BusStop Create(BusStopEditDsM data)
abec55bf   Administrator   Finish busStop mo...
43
          {   
e02ee314   Administrator   add update and cr...
44
45
46
              
              BusStop Model = InsertModel(data);
              _context.BusStop.Add(Model);
abec55bf   Administrator   Finish busStop mo...
47
              _context.SaveChanges();
e02ee314   Administrator   add update and cr...
48
49
50
51
52
53
54
55
              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
56
              Model.Id = id;
e02ee314   Administrator   add update and cr...
57
58
59
60
61
              _context.BusStop.Update(Model);
              _context.SaveChanges();
              return Model;
          }
          public BusStop InsertModel(BusStopEditDsM data){
4f948d42   Yarik   Mapper
62
              BusStop Model = Mapper.Map<BusStop>(data);
e02ee314   Administrator   add update and cr...
63
              return Model;
32912d0b   Administrator   first commit
64
          }
e02ee314   Administrator   add update and cr...
65
          public async Task<int> DeleteAsync(int Id)
94ffda14   Yarik   Delete action
66
          {
db607025   Administrator   add pagination
67
              var busStop = await _context.BusStop.SingleOrDefaultAsync(x => x.Id == Id);
94ffda14   Yarik   Delete action
68
69
70
              _context.BusStop.Remove(busStop);
              return await _context.SaveChangesAsync();
          }
32912d0b   Administrator   first commit
71
72
      }
  }