Commit db607025a1a1700e16d22a77022da9c0b539c27e

Authored by Administrator
1 parent e02ee314

add pagination

src/Maps/Controllers/BusStopController.cs
... ... @@ -33,16 +33,30 @@ 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 busStops = await _busStopDs.GetIndexListAsync();
  38 + try
  39 + {
  40 + var busStops = await _busStopDs.GetIndexListAsync(data);
  41 +
  42 + BusStopListVm vm = new BusStopListVm
  43 + {
  44 + BusStopEditDsM = busStops.ToList()
  45 + };
39 46  
40   - BusStopListVm vm = new BusStopListVm
  47 + return Json(vm);
  48 + }
  49 + catch (NullReferenceException e)
41 50 {
42   - BusStopEditDsM = busStops.ToList()
43   - };
  51 + Response.StatusCode = 400;
  52 + return Json("There is no field with name " + data.sort);
  53 + }
  54 + catch (Exception e)
  55 + {
  56 + return NotFound();
  57 + }
  58 +
44 59  
45   - return Json(vm);
46 60 }
47 61  
48 62 [HttpGet]
... ...
src/Maps/project.json
... ... @@ -24,6 +24,7 @@
24 24 "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
25 25 "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
26 26 "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview4-final",
  27 + "System.Reflection.TypeExtensions": "4.3.0-preview1-24530-04",
27 28 "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
28 29 "version": "1.0.0-preview2-final",
29 30 "type": "build"
... ...
src/MapsDb/DataService/BusStopDs.cs
1 1 using System.Collections.Generic;
2 2 using System.Linq;
  3 +using System.Reflection;
3 4 using System.Threading.Tasks;
4 5 using MapsDb.Interfaces;
5 6 using MapsDb.Models;
... ... @@ -13,14 +14,14 @@ namespace MapsDb.DataService
13 14 public BusStopDs(){
14 15 _context = new PostgresDbContext();
15 16 }
16   - public Task<IList<BusStopEditDsM>> GetIndexListAsync(){
17   - return Task.Factory.StartNew(GetAllBusStop);
  17 + public Task<IList<BusStopEditDsM>> GetIndexListAsync(PaginationDsM pagination){
  18 + return Task.Factory.StartNew(()=> { return GetAllBusStop(pagination); });
18 19 }
19   - private IList<BusStopEditDsM> GetAllBusStop()
  20 + private IList<BusStopEditDsM> GetAllBusStop(PaginationDsM pagination)
20 21 {
21   - return _context.BusStop.Select(busStop => new BusStopEditDsM
  22 + var data = _context.BusStop.Select(busStop => new BusStopEditDsM
22 23 {
23   - BusStopId = busStop.BusStopId,
  24 + Id = busStop.Id,
24 25 RoadId = busStop.RoadId,
25 26 RegionId = busStop.RegionId,
26 27 SettlementId = busStop.SettlementId,
... ... @@ -34,7 +35,19 @@ namespace MapsDb.DataService
34 35 YearBuild = busStop.YearBuild,
35 36 YearRepair = busStop.YearRepair,
36 37 StateCommonId = busStop.StateCommonId
37   - }).OrderByDescending(BusStop => BusStop.BusStopId).ToList();
  38 + }).Skip(pagination.from).Take(pagination.perPage);
  39 + switch (pagination.orderType())
  40 + {
  41 + case "ASC":
  42 + return data.OrderBy(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList();
  43 +
  44 + case "DESC":
  45 + return data.OrderByDescending(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList();
  46 +
  47 + default:
  48 + return data.OrderBy(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList();
  49 + }
  50 +
38 51 }
39 52  
40 53 public Task<BusStop> CreateAsync(BusStopEditDsM data){
... ... @@ -54,7 +67,7 @@ namespace MapsDb.DataService
54 67 private BusStop Update(BusStopEditDsM data, int id)
55 68 {
56 69 BusStop Model = InsertModel(data);
57   - Model.BusStopId = id;
  70 + Model.Id = id;
58 71 _context.BusStop.Update(Model);
59 72 _context.SaveChanges();
60 73 return Model;
... ... @@ -79,7 +92,7 @@ namespace MapsDb.DataService
79 92 }
80 93 public async Task<int> DeleteAsync(int Id)
81 94 {
82   - var busStop = await _context.BusStop.SingleOrDefaultAsync(x => x.BusStopId == Id);
  95 + var busStop = await _context.BusStop.SingleOrDefaultAsync(x => x.Id == Id);
83 96 _context.BusStop.Remove(busStop);
84 97 return await _context.SaveChangesAsync();
85 98 }
... ...
src/MapsDb/Interfaces/IBusStopDs.cs
... ... @@ -6,7 +6,7 @@ namespace MapsDb.Interfaces
6 6 {
7 7 public interface IBusStopDs
8 8 {
9   - Task<IList<BusStopEditDsM>> GetIndexListAsync();
  9 + Task<IList<BusStopEditDsM>> GetIndexListAsync(PaginationDsM pagination);
10 10 Task<BusStop> CreateAsync(BusStopEditDsM busStop);
11 11 Task<BusStop> UpdateAsync(BusStopEditDsM busStop, int id);
12 12 Task<int> DeleteAsync(int Id);
... ...
src/MapsDb/Models/BusStop.cs
... ... @@ -5,7 +5,7 @@ namespace MapsDb.Models
5 5 {
6 6 public partial class BusStop
7 7 {
8   - public int BusStopId { 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? SettlementId { get; set; }
... ...
src/MapsDb/PostgresDbContext.cs
... ... @@ -18,7 +18,7 @@ namespace MapsDb
18 18 {
19 19 entity.ToTable("bus_stop");
20 20  
21   - entity.Property(e => e.BusStopId).HasColumnName("bus_stop_id");
  21 + entity.Property(e => e.Id).HasColumnName("id");
22 22  
23 23 entity.Property(e => e.AreaLandAvailability).HasColumnName("area_land_availability");
24 24  
... ...
src/MapsModels/DsModels/BusStopEditDsM.cs
... ... @@ -2,7 +2,7 @@ namespace MapsModels.DsModels
2 2 {
3 3 public class BusStopEditDsM
4 4 {
5   - public int? BusStopId { 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? SettlementId { get; set; }
... ...
src/MapsModels/DsModels/PaginationDsM.cs 0 → 100644
  1 +namespace MapsModels.DsModels
  2 +{
  3 + public class PaginationDsM
  4 + {
  5 + public const string ASC = "ASC";
  6 + public const string DESC = "DESC";
  7 + public int from { get; set; }
  8 + public int perPage { get; set; }
  9 + public string sort { get; set; }
  10 + public string orderType(){
  11 + if(sort.StartsWith("-")){
  12 + sort = sort.Substring(1);
  13 + return DESC;
  14 + }
  15 + return ASC;
  16 + }
  17 + }
  18 +}
0 19 \ No newline at end of file
... ...