diff --git a/src/Maps/Controllers/RoadWidthController.cs b/src/Maps/Controllers/RoadWidthController.cs new file mode 100755 index 0000000..ef37973 --- /dev/null +++ b/src/Maps/Controllers/RoadWidthController.cs @@ -0,0 +1,100 @@ +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Rendering; +using Microsoft.EntityFrameworkCore; +using MapsDb; +using MapsDb.Interfaces; +using MapsDb.DataService; +using MapsModels.ViewModels; +using MapsModels.DsModels; +using System; + +namespace Maps.Controllers +{ + public class RoadWidthController : Controller + { + private readonly IRoadDs _roadDs; + private readonly IRegionDs _regionDs; + private readonly IRoadWidthDs _roadWidthDs; + public RoadWidthController( + IRoadWidthDs RoadWidthDs, + IRoadDs RoadDs, + IRegionDs RegionDs + ) + { + _roadDs = RoadDs; + _regionDs = RegionDs; + _roadWidthDs = RoadWidthDs; + } + + // GET: BusStop + [HttpGet] + public async Task Index([FromQuery] PaginationDsM data) + { + + try + { + var Data = await _roadWidthDs.GetIndexListAsync(data); + + RoadWidthListVm vm = new RoadWidthListVm + { + RoadWidthEditDsM = Data.ToList() + }; + + return Json(vm); + } + catch (NullReferenceException) + { + Response.StatusCode = 400; + return Json("There is no field with name " + data.sort); + } + catch (Exception) + { + return NotFound(); + } + } + + [HttpGet] + public async Task Directory(){ + var Road = await _roadDs.GetSelectListAsync(); + var Region = await _regionDs.GetSelectListAsync(); + + CatalogListVm vm = new CatalogListVm + { + RoadSelectListDsM = Road.ToList(), + RegionSelectListDsM = Region.ToList() + }; + return Json(vm); + } + + + [HttpPost] + public async Task Create([FromBody] RoadWidthEditDsM data) + { + var result = await _roadWidthDs.CreateAsync(data); + return Json(result); + } + + [HttpPost] + public async Task Update(int id, [FromBody] RoadWidthEditDsM data){ + await _roadWidthDs.UpdateAsync(data,id); + return Json(String.Empty); + } + + + [HttpDelete] + public async Task Delete(int id) + { + try + { + int data = await _roadWidthDs.DeleteAsync(id); + return Json(data); + } + catch (ArgumentNullException ) + { + return NotFound(); + } + } + } +} diff --git a/src/Maps/Startup.cs b/src/Maps/Startup.cs index 43a0287..bef6d8b 100644 --- a/src/Maps/Startup.cs +++ b/src/Maps/Startup.cs @@ -56,6 +56,7 @@ namespace Maps services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); // Add framework services. services.AddApplicationInsightsTelemetry(Configuration); diff --git a/src/MapsDb/DataService/RoadSurfaceDs.cs b/src/MapsDb/DataService/RoadSurfaceDs.cs index d73953d..0ae7b46 100644 --- a/src/MapsDb/DataService/RoadSurfaceDs.cs +++ b/src/MapsDb/DataService/RoadSurfaceDs.cs @@ -21,7 +21,15 @@ namespace MapsDb.DataService { var data = _context.RoadSurface.Select(RoadSurface => new RoadSurfaceEditDsM { - + Id = RoadSurface.Id, + RoadId = RoadSurface.RoadId, + RegionId = RoadSurface.RegionId, + RoadDirectionId = RoadSurface.RoadDirectionId, + Begin = RoadSurface.Begin, + End = RoadSurface.End, + SurfaceTypeId = RoadSurface.SurfaceTypeId, + SurfaceTreatmentId = RoadSurface.SurfaceTreatmentId, + StateCommonId = RoadSurface.StateCommonId, }).Skip(pagination.from).Take(pagination.perPage); switch (pagination.orderType()) { @@ -59,7 +67,15 @@ namespace MapsDb.DataService } public RoadSurface InsertModel(RoadSurfaceEditDsM data){ RoadSurface Model = new RoadSurface{ - + Id = data.Id, + RoadId = data.RoadId, + RegionId = data.RegionId, + RoadDirectionId = data.RoadDirectionId, + Begin = data.Begin, + End = data.End, + SurfaceTypeId = data.SurfaceTypeId, + SurfaceTreatmentId = data.SurfaceTreatmentId, + StateCommonId = data.StateCommonId, }; return Model; } diff --git a/src/MapsDb/DataService/RoadWidthDs.cs b/src/MapsDb/DataService/RoadWidthDs.cs new file mode 100644 index 0000000..99a7fea --- /dev/null +++ b/src/MapsDb/DataService/RoadWidthDs.cs @@ -0,0 +1,96 @@ +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Threading.Tasks; +using MapsDb.Interfaces; +using MapsDb.Models; +using MapsModels.DsModels; +using Microsoft.EntityFrameworkCore; +namespace MapsDb.DataService +{ + public class RoadWidthDs : IRoadWidthDs + { + private PostgresDbContext _context; + public RoadWidthDs(){ + _context = new PostgresDbContext(); + } + public Task> GetIndexListAsync(PaginationDsM pagination){ + return Task.Factory.StartNew(()=> { return GetAllRoadWidth(pagination); }); + } + private IList GetAllRoadWidth(PaginationDsM pagination) + { + var data = _context.RoadWidth.Select(RoadWidth => new RoadWidthEditDsM + { + Id = RoadWidth.Id, + RoadId = RoadWidth.RoadId, + RegionId = RoadWidth.RegionId, + Begin = RoadWidth.Begin, + End = RoadWidth.End, + WidthRoadsideLeft = RoadWidth.WidthRoadsideLeft, + WidthReverseRoad = RoadWidth.WidthReverseRoad, + WidthStrip = RoadWidth.WidthStrip, + WidthRoadwayForward = RoadWidth.WidthRoadwayForward, + WidthRoadsideRight = RoadWidth.WidthRoadsideRight, + CountLaneLeft = RoadWidth.CountLaneLeft, + CountLaneRight = RoadWidth.CountLaneRight, + }).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.OrderByDescending(i => i.Id).ToList(); + } + + } + + public Task CreateAsync(RoadWidthEditDsM data){ + return Task.Factory.StartNew(()=> { return Create(data); }); + } + private RoadWidth Create(RoadWidthEditDsM data) + { + + RoadWidth Model = InsertModel(data); + _context.RoadWidth.Add(Model); + _context.SaveChanges(); + return Model; + } + public Task UpdateAsync(RoadWidthEditDsM data, int id){ + return Task.Factory.StartNew(()=> { return Update(data, id); }); + } + private RoadWidth Update(RoadWidthEditDsM data, int id) + { + RoadWidth Model = InsertModel(data); + Model.Id = id; + _context.RoadWidth.Update(Model); + _context.SaveChanges(); + return Model; + } + public RoadWidth InsertModel(RoadWidthEditDsM data){ + RoadWidth Model = new RoadWidth{ + RoadId = data.RoadId, + RegionId = data.RegionId, + Begin = data.Begin, + End = data.End, + WidthRoadsideLeft = data.WidthRoadsideLeft, + WidthReverseRoad = data.WidthReverseRoad, + WidthStrip = data.WidthStrip, + WidthRoadwayForward = data.WidthRoadwayForward, + WidthRoadsideRight = data.WidthRoadsideRight, + CountLaneLeft = data.CountLaneLeft, + CountLaneRight = data.CountLaneRight, + }; + return Model; + } + public async Task DeleteAsync(int Id) + { + var RoadWidth = await _context.RoadWidth.SingleOrDefaultAsync(x => x.Id == Id); + _context.RoadWidth.Remove(RoadWidth); + return await _context.SaveChangesAsync(); + } + } +} \ No newline at end of file diff --git a/src/MapsDb/Interfaces/IRoadWidthDs.cs b/src/MapsDb/Interfaces/IRoadWidthDs.cs new file mode 100644 index 0000000..6528449 --- /dev/null +++ b/src/MapsDb/Interfaces/IRoadWidthDs.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using MapsModels.DsModels; +using MapsDb.Models; +namespace MapsDb.Interfaces +{ + public interface IRoadWidthDs + { + Task> GetIndexListAsync(PaginationDsM pagination); + Task CreateAsync(RoadWidthEditDsM data); + Task UpdateAsync(RoadWidthEditDsM data, int id); + Task DeleteAsync(int Id); + } +} \ No newline at end of file diff --git a/src/MapsDb/Models/RoadWidth.cs b/src/MapsDb/Models/RoadWidth.cs index 81dd83a..cc66a92 100644 --- a/src/MapsDb/Models/RoadWidth.cs +++ b/src/MapsDb/Models/RoadWidth.cs @@ -5,7 +5,7 @@ namespace MapsDb.Models { public partial class RoadWidth { - public int RoadWidthId { get; set; } + public int Id { get; set; } public int? RegionId { get; set; } public int? RoadId { get; set; } public double? Begin { get; set; } diff --git a/src/MapsDb/PostgresDbContext.cs b/src/MapsDb/PostgresDbContext.cs index 6302f30..a914a4d 100644 --- a/src/MapsDb/PostgresDbContext.cs +++ b/src/MapsDb/PostgresDbContext.cs @@ -525,7 +525,7 @@ namespace MapsDb { entity.ToTable("road_width"); - entity.Property(e => e.RoadWidthId).HasColumnName("road_width_id"); + entity.Property(e => e.Id).HasColumnName("id"); entity.Property(e => e.Begin).HasColumnName("begin"); diff --git a/src/MapsModels/DsModels/RoadWidthEditDsM.cs b/src/MapsModels/DsModels/RoadWidthEditDsM.cs new file mode 100644 index 0000000..0ac97dd --- /dev/null +++ b/src/MapsModels/DsModels/RoadWidthEditDsM.cs @@ -0,0 +1,20 @@ +namespace MapsModels.DsModels +{ + public class RoadWidthEditDsM + { + + public int Id { get; set; } + public int? RegionId { get; set; } + public int? RoadId { get; set; } + public double? Begin { get; set; } + public double? End { get; set; } + public double? WidthRoadsideLeft { get; set; } + public double? WidthReverseRoad { get; set; } + public double? WidthStrip { get; set; } + public double? WidthRoadwayForward { get; set; } + public double? WidthRoadsideRight { get; set; } + public double? CountLaneLeft { get; set; } + public double? CountLaneRight { get; set; } + + } +} \ No newline at end of file diff --git a/src/MapsModels/ViewModels/RoadWidthListVm.cs b/src/MapsModels/ViewModels/RoadWidthListVm.cs new file mode 100644 index 0000000..056375c --- /dev/null +++ b/src/MapsModels/ViewModels/RoadWidthListVm.cs @@ -0,0 +1,10 @@ +using System.Collections.Generic; +using MapsModels.DsModels; + +namespace MapsModels.ViewModels +{ + public class RoadWidthListVm + { + public List RoadWidthEditDsM { get; set; } + } +} -- libgit2 0.21.4