diff --git a/src/Maps/Controllers/BusStopController.cs b/src/Maps/Controllers/BusStopController.cs index 3b3efe8..9f1c3db 100755 --- a/src/Maps/Controllers/BusStopController.cs +++ b/src/Maps/Controllers/BusStopController.cs @@ -33,16 +33,30 @@ namespace Maps.Controllers // GET: BusStop [HttpGet] - public async Task Index() + public async Task Index([FromQuery] PaginationDsM data) { - var busStops = await _busStopDs.GetIndexListAsync(); + try + { + var busStops = await _busStopDs.GetIndexListAsync(data); + + BusStopListVm vm = new BusStopListVm + { + BusStopEditDsM = busStops.ToList() + }; - BusStopListVm vm = new BusStopListVm + return Json(vm); + } + catch (NullReferenceException e) { - BusStopEditDsM = busStops.ToList() - }; + Response.StatusCode = 400; + return Json("There is no field with name " + data.sort); + } + catch (Exception e) + { + return NotFound(); + } + - return Json(vm); } [HttpGet] diff --git a/src/Maps/project.json b/src/Maps/project.json index 7d768a3..66d2df1 100644 --- a/src/Maps/project.json +++ b/src/Maps/project.json @@ -24,6 +24,7 @@ "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0", "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0", "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview4-final", + "System.Reflection.TypeExtensions": "4.3.0-preview1-24530-04", "Microsoft.VisualStudio.Web.CodeGeneration.Tools": { "version": "1.0.0-preview2-final", "type": "build" diff --git a/src/MapsDb/DataService/BusStopDs.cs b/src/MapsDb/DataService/BusStopDs.cs index 5da72b8..5e9a2e1 100644 --- a/src/MapsDb/DataService/BusStopDs.cs +++ b/src/MapsDb/DataService/BusStopDs.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Threading.Tasks; using MapsDb.Interfaces; using MapsDb.Models; @@ -13,14 +14,14 @@ namespace MapsDb.DataService public BusStopDs(){ _context = new PostgresDbContext(); } - public Task> GetIndexListAsync(){ - return Task.Factory.StartNew(GetAllBusStop); + public Task> GetIndexListAsync(PaginationDsM pagination){ + return Task.Factory.StartNew(()=> { return GetAllBusStop(pagination); }); } - private IList GetAllBusStop() + private IList GetAllBusStop(PaginationDsM pagination) { - return _context.BusStop.Select(busStop => new BusStopEditDsM + var data = _context.BusStop.Select(busStop => new BusStopEditDsM { - BusStopId = busStop.BusStopId, + Id = busStop.Id, RoadId = busStop.RoadId, RegionId = busStop.RegionId, SettlementId = busStop.SettlementId, @@ -34,7 +35,19 @@ namespace MapsDb.DataService YearBuild = busStop.YearBuild, YearRepair = busStop.YearRepair, StateCommonId = busStop.StateCommonId - }).OrderByDescending(BusStop => BusStop.BusStopId).ToList(); + }).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.OrderBy(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList(); + } + } public Task CreateAsync(BusStopEditDsM data){ @@ -54,7 +67,7 @@ namespace MapsDb.DataService private BusStop Update(BusStopEditDsM data, int id) { BusStop Model = InsertModel(data); - Model.BusStopId = id; + Model.Id = id; _context.BusStop.Update(Model); _context.SaveChanges(); return Model; @@ -79,7 +92,7 @@ namespace MapsDb.DataService } public async Task DeleteAsync(int Id) { - var busStop = await _context.BusStop.SingleOrDefaultAsync(x => x.BusStopId == Id); + var busStop = await _context.BusStop.SingleOrDefaultAsync(x => x.Id == Id); _context.BusStop.Remove(busStop); return await _context.SaveChangesAsync(); } diff --git a/src/MapsDb/Interfaces/IBusStopDs.cs b/src/MapsDb/Interfaces/IBusStopDs.cs index e4aeb64..16fedf7 100644 --- a/src/MapsDb/Interfaces/IBusStopDs.cs +++ b/src/MapsDb/Interfaces/IBusStopDs.cs @@ -6,7 +6,7 @@ namespace MapsDb.Interfaces { public interface IBusStopDs { - Task> GetIndexListAsync(); + Task> GetIndexListAsync(PaginationDsM pagination); Task CreateAsync(BusStopEditDsM busStop); Task UpdateAsync(BusStopEditDsM busStop, int id); Task DeleteAsync(int Id); diff --git a/src/MapsDb/Models/BusStop.cs b/src/MapsDb/Models/BusStop.cs index a739b86..392a0f5 100644 --- a/src/MapsDb/Models/BusStop.cs +++ b/src/MapsDb/Models/BusStop.cs @@ -5,7 +5,7 @@ namespace MapsDb.Models { public partial class BusStop { - public int BusStopId { get; set; } + public int Id { get; set; } public int? RoadId { get; set; } public int? RegionId { get; set; } public int? SettlementId { get; set; } diff --git a/src/MapsDb/PostgresDbContext.cs b/src/MapsDb/PostgresDbContext.cs index ff72b39..e6d9a4b 100644 --- a/src/MapsDb/PostgresDbContext.cs +++ b/src/MapsDb/PostgresDbContext.cs @@ -18,7 +18,7 @@ namespace MapsDb { entity.ToTable("bus_stop"); - entity.Property(e => e.BusStopId).HasColumnName("bus_stop_id"); + entity.Property(e => e.Id).HasColumnName("id"); entity.Property(e => e.AreaLandAvailability).HasColumnName("area_land_availability"); diff --git a/src/MapsModels/DsModels/BusStopEditDsM.cs b/src/MapsModels/DsModels/BusStopEditDsM.cs index fd71dad..c295a6f 100644 --- a/src/MapsModels/DsModels/BusStopEditDsM.cs +++ b/src/MapsModels/DsModels/BusStopEditDsM.cs @@ -2,7 +2,7 @@ namespace MapsModels.DsModels { public class BusStopEditDsM { - public int? BusStopId { get; set; } + public int? Id { get; set; } public int? RoadId { get; set; } public int? RegionId { get; set; } public int? SettlementId { get; set; } diff --git a/src/MapsModels/DsModels/PaginationDsM.cs b/src/MapsModels/DsModels/PaginationDsM.cs new file mode 100644 index 0000000..538d45e --- /dev/null +++ b/src/MapsModels/DsModels/PaginationDsM.cs @@ -0,0 +1,18 @@ +namespace MapsModels.DsModels +{ + public class PaginationDsM + { + public const string ASC = "ASC"; + public const string DESC = "DESC"; + public int from { get; set; } + public int perPage { get; set; } + public string sort { get; set; } + public string orderType(){ + if(sort.StartsWith("-")){ + sort = sort.Substring(1); + return DESC; + } + return ASC; + } + } +} \ No newline at end of file -- libgit2 0.21.4