Commit 25539461a101921517ad7f09cceece888fd09118

Authored by Administrator
1 parent 8940e931

add pagination to service object

src/Maps/Controllers/ServiceObjectController.cs
... ... @@ -33,9 +33,9 @@ namespace Maps.Controllers
33 33  
34 34 // GET: BusStop
35 35 [HttpGet]
36   - public async Task<IActionResult> Index()
  36 + public async Task<IActionResult> Index([FromQuery] PaginationDsM data)
37 37 {
38   - var Data = await _serviceObjectDs.GetIndexListAsync();
  38 + var Data = await _serviceObjectDs.GetIndexListAsync(data);
39 39  
40 40 ServiceObjectListVm vm = new ServiceObjectListVm
41 41 {
... ...
src/MapsDb/DataService/ServiceObjectDs.cs
... ... @@ -4,6 +4,7 @@ using System.Threading.Tasks;
4 4 using MapsDb.Interfaces;
5 5 using MapsDb.Models;
6 6 using MapsModels.DsModels;
  7 +using System.Reflection;
7 8 using Microsoft.EntityFrameworkCore;
8 9 namespace MapsDb.DataService
9 10 {
... ... @@ -13,14 +14,14 @@ namespace MapsDb.DataService
13 14 public ServiceObjectDs(){
14 15 _context = new PostgresDbContext();
15 16 }
16   - public Task<IList<ServiceObjectEditDsM>> GetIndexListAsync(){
17   - return Task.Factory.StartNew(GetAllServiceObject);
  17 + public Task<IList<ServiceObjectEditDsM>> GetIndexListAsync(PaginationDsM pagination){
  18 + return Task.Factory.StartNew(()=> { return GetAllServiceObject(pagination); });
18 19 }
19   - private IList<ServiceObjectEditDsM> GetAllServiceObject()
  20 + private IList<ServiceObjectEditDsM> GetAllServiceObject(PaginationDsM pagination)
20 21 {
21   - return _context.ServiceObject.Select(ServiceObject => new ServiceObjectEditDsM
  22 + var data = _context.ServiceObject.Select(ServiceObject => new ServiceObjectEditDsM
22 23 {
23   - ServiceObjectId = ServiceObject.ServiceObjectId,
  24 + ServiceObjectId = ServiceObject.Id,
24 25 RoadId = ServiceObject.RoadId,
25 26 RegionId = ServiceObject.RegionId,
26 27 SettlementId = ServiceObject.SettlementId,
... ... @@ -32,7 +33,18 @@ namespace MapsDb.DataService
32 33 Distance = ServiceObject.Distance,
33 34 Capacity = ServiceObject.Capacity,
34 35 ArrangementElements = ServiceObject.ArrangementElements,
35   - }).OrderByDescending(ServiceObject => ServiceObject.ServiceObjectId).ToList();
  36 + }).Skip(pagination.from).Take(pagination.perPage);
  37 + switch (pagination.orderType())
  38 + {
  39 + case "ASC":
  40 + return data.OrderBy(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList();
  41 +
  42 + case "DESC":
  43 + return data.OrderByDescending(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList();
  44 +
  45 + default:
  46 + return data.OrderByDescending(i => i.Id).ToList();
  47 + }
36 48 }
37 49  
38 50 public Task<ServiceObject> CreateAsync(ServiceObjectEditDsM data){
... ... @@ -51,14 +63,14 @@ namespace MapsDb.DataService
51 63 private ServiceObject Update(ServiceObjectEditDsM data, int id)
52 64 {
53 65 ServiceObject Model = InsertModel(data);
54   - Model.ServiceObjectId = id;
  66 + Model.Id = id;
55 67 _context.ServiceObject.Update(Model);
56 68 _context.SaveChanges();
57 69 return Model;
58 70 }
59 71 public ServiceObject InsertModel(ServiceObjectEditDsM data){
60 72 ServiceObject Model = new ServiceObject{
61   - ServiceObjectId = data.ServiceObjectId,
  73 + Id = data.ServiceObjectId,
62 74 RoadId = data.RoadId,
63 75 RegionId = data.RegionId,
64 76 SettlementId = data.SettlementId,
... ... @@ -75,7 +87,7 @@ namespace MapsDb.DataService
75 87 }
76 88 public async Task<int> DeleteAsync(int Id)
77 89 {
78   - var ServiceObject = await _context.ServiceObject.SingleOrDefaultAsync(x => x.ServiceObjectId == Id);
  90 + var ServiceObject = await _context.ServiceObject.SingleOrDefaultAsync(x => x.Id == Id);
79 91 _context.ServiceObject.Remove(ServiceObject);
80 92 return await _context.SaveChangesAsync();
81 93 }
... ...
src/MapsDb/Interfaces/IServiceObjectDs.cs
... ... @@ -6,7 +6,7 @@ namespace MapsDb.Interfaces
6 6 {
7 7 public interface IServiceObjectDs
8 8 {
9   - Task<IList<ServiceObjectEditDsM>> GetIndexListAsync();
  9 + Task<IList<ServiceObjectEditDsM>> GetIndexListAsync(PaginationDsM pagination);
10 10 Task<ServiceObject> CreateAsync(ServiceObjectEditDsM serviceObject);
11 11 Task<ServiceObject> UpdateAsync(ServiceObjectEditDsM serviceObject, int id);
12 12 Task<int> DeleteAsync(int Id);
... ...
src/MapsDb/Models/ServiceObject.cs
... ... @@ -5,7 +5,7 @@ namespace MapsDb.Models
5 5 {
6 6 public partial class ServiceObject
7 7 {
8   - public int ServiceObjectId { get; set; }
  8 + public int Id { get; set; }
9 9 public int? RoadId { get; set; }
10 10 public int? RegionId { get; set; }
11 11 public int? ServiceObjectTypeId { get; set; }
... ...
src/MapsDb/PostgresDbContext.cs
... ... @@ -566,7 +566,7 @@ namespace MapsDb
566 566 {
567 567 entity.ToTable("service_object");
568 568  
569   - entity.Property(e => e.ServiceObjectId).HasColumnName("service_object_id");
  569 + entity.Property(e => e.Id).HasColumnName("id");
570 570  
571 571 entity.Property(e => e.ArrangementElements).HasColumnName("arrangement_elements");
572 572  
... ...
src/MapsModels/DsModels/ServiceObjectEditDsM.cs
... ... @@ -2,7 +2,7 @@ namespace MapsModels.DsModels
2 2 {
3 3 public class ServiceObjectEditDsM
4 4 {
5   - public int ServiceObjectId { get; set; }
  5 + public int Id { get; set; }
6 6 public int? RoadId { get; set; }
7 7 public int? RegionId { get; set; }
8 8 public int? ServiceObjectTypeId { get; set; }
... ...