From 59f80a4613260e0c6ec7140098e4c0797455b6f9 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Tue, 14 Feb 2017 12:58:11 +0200 Subject: [PATCH] add cross section --- src/Maps/Controllers/CrossSectionController.cs | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/Maps/Startup.cs | 1 + src/MapsDb/DataService/CrossSectionDs.cs | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/MapsDb/Interfaces/ICrossSectionDs.cs | 15 +++++++++++++++ src/MapsDb/Models/CrossSection.cs | 2 +- src/MapsDb/PostgresDbContext.cs | 2 +- src/MapsModels/DsModels/CrossSectionEditDsM.cs | 23 +++++++++++++++++++++++ src/MapsModels/ViewModels/CrossSectionListVm.cs | 10 ++++++++++ 8 files changed, 268 insertions(+), 2 deletions(-) create mode 100755 src/Maps/Controllers/CrossSectionController.cs create mode 100644 src/MapsDb/DataService/CrossSectionDs.cs create mode 100644 src/MapsDb/Interfaces/ICrossSectionDs.cs create mode 100644 src/MapsModels/DsModels/CrossSectionEditDsM.cs create mode 100644 src/MapsModels/ViewModels/CrossSectionListVm.cs diff --git a/src/Maps/Controllers/CrossSectionController.cs b/src/Maps/Controllers/CrossSectionController.cs new file mode 100755 index 0000000..abbfb37 --- /dev/null +++ b/src/Maps/Controllers/CrossSectionController.cs @@ -0,0 +1,112 @@ +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 CrossSectionController : Controller + { + private readonly IRoadDs _roadDs; + private readonly IRegionDs _regionDs; + private readonly ISurfaceTypeDs _surfaceTypeDs; + private readonly IStateCommonDs _stateCommonDs; + private readonly ICrossSectionDs _crossSectionDs; + + + public CrossSectionController( + ICrossSectionDs CrossSectionDs, + IStateCommonDs StateCommonDs, + IRoadDs RoadDs, + IRegionDs RegionDs, + ISurfaceTypeDs SurfaceTypeDs + ) + { + _roadDs = RoadDs; + _surfaceTypeDs = SurfaceTypeDs; + _regionDs = RegionDs; + _stateCommonDs = StateCommonDs; + _crossSectionDs = CrossSectionDs; + } + + // GET: BusStop + [HttpGet] + public async Task Index([FromQuery] PaginationDsM data) + { + + try + { + var Data = await _crossSectionDs.GetIndexListAsync(data); + + CrossSectionListVm vm = new CrossSectionListVm + { + CrossSectionEditDsM = 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(); + var SurfaceType = await _surfaceTypeDs.GetSelectListAsync(); + var StateCommon = await _stateCommonDs.GetSelectListAsync(); + + CatalogListVm vm = new CatalogListVm + { + RoadSelectListDsM = Road.ToList(), + RegionSelectListDsM = Region.ToList(), + SurfaceTypeSelectListDsM = SurfaceType.ToList(), + StateCommonSelectListDsM = StateCommon.ToList() + }; + return Json(vm); + } + + + [HttpPost] + public async Task Create([FromBody] CrossSectionEditDsM data) + { + var result = await _crossSectionDs.CreateAsync(data); + return Json(result); + } + + [HttpPost] + public async Task Update(int id, [FromBody] CrossSectionEditDsM data){ + await _crossSectionDs.UpdateAsync(data,id); + return Json(String.Empty); + } + + + [HttpDelete] + public async Task Delete(int id) + { + try + { + int data = await _crossSectionDs.DeleteAsync(id); + return Json(data); + } + catch (ArgumentNullException ) + { + return NotFound(); + } + } + } +} diff --git a/src/Maps/Startup.cs b/src/Maps/Startup.cs index bef6d8b..2a825fb 100644 --- a/src/Maps/Startup.cs +++ b/src/Maps/Startup.cs @@ -57,6 +57,7 @@ namespace Maps services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); // Add framework services. services.AddApplicationInsightsTelemetry(Configuration); diff --git a/src/MapsDb/DataService/CrossSectionDs.cs b/src/MapsDb/DataService/CrossSectionDs.cs new file mode 100644 index 0000000..833d253 --- /dev/null +++ b/src/MapsDb/DataService/CrossSectionDs.cs @@ -0,0 +1,105 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using MapsDb.Interfaces; +using MapsDb.Models; +using MapsModels.DsModels; +using System.Reflection; +using Microsoft.EntityFrameworkCore; +namespace MapsDb.DataService +{ + public class CrossSectionDs : ICrossSectionDs + { + private PostgresDbContext _context; + public CrossSectionDs(){ + _context = new PostgresDbContext(); + } + public Task> GetIndexListAsync(PaginationDsM pagination){ + return Task.Factory.StartNew(()=> { return GetAllCrossSection(pagination); }); + } + private IList GetAllCrossSection(PaginationDsM pagination) + { + var data = _context.CrossSection.Select(CrossSection => new CrossSectionEditDsM + { + Id = CrossSection.Id, + RoadId = CrossSection.RoadId, + RegionId = CrossSection.RegionId, + StateCommonId = CrossSection.StateCommonId, + YearBuild = CrossSection.YearBuild, + YearRepair = CrossSection.YearRepair, + SafetyAvailability = CrossSection.SafetyAvailability, + TubeAvailability = CrossSection.TubeAvailability, + Angle = CrossSection.Angle, + Width = CrossSection.Width, + DistanceEdge = CrossSection.DistanceEdge, + LengthSurface = CrossSection.LengthSurface, + LengthSection = CrossSection.LengthSection, + SurfaceTypeId = CrossSection.SurfaceTypeId, + Direction = CrossSection.Direction, + LocationRight = CrossSection.LocationRight, + LocationLeft = CrossSection.LocationLeft, + }).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(CrossSectionEditDsM data){ + return Task.Factory.StartNew(()=> { return Create(data); }); + } + private CrossSection Create(CrossSectionEditDsM data) + { + CrossSection Model = InsertModel(data); + _context.CrossSection.Add(Model); + _context.SaveChanges(); + return Model; + } + public Task UpdateAsync(CrossSectionEditDsM data, int id){ + return Task.Factory.StartNew(()=> { return Update(data, id); }); + } + private CrossSection Update(CrossSectionEditDsM data, int id) + { + CrossSection Model = InsertModel(data); + Model.Id = id; + _context.CrossSection.Update(Model); + _context.SaveChanges(); + return Model; + } + public CrossSection InsertModel(CrossSectionEditDsM data){ + CrossSection Model = new CrossSection{ + Id = data.Id, + RoadId = data.RoadId, + RegionId = data.RegionId, + StateCommonId = data.StateCommonId, + YearBuild = data.YearBuild, + YearRepair = data.YearRepair, + SafetyAvailability = data.SafetyAvailability, + TubeAvailability = data.TubeAvailability, + Angle = data.Angle, + Width = data.Width, + DistanceEdge = data.DistanceEdge, + LengthSurface = data.LengthSurface, + LengthSection = data.LengthSection, + SurfaceTypeId = data.SurfaceTypeId, + Direction = data.Direction, + LocationRight = data.LocationRight, + LocationLeft = data.LocationLeft, + }; + return Model; + } + public async Task DeleteAsync(int Id) + { + var CrossSection = await _context.CrossSection.SingleOrDefaultAsync(x => x.Id == Id); + _context.CrossSection.Remove(CrossSection); + return await _context.SaveChangesAsync(); + } + } +} \ No newline at end of file diff --git a/src/MapsDb/Interfaces/ICrossSectionDs.cs b/src/MapsDb/Interfaces/ICrossSectionDs.cs new file mode 100644 index 0000000..7e088e2 --- /dev/null +++ b/src/MapsDb/Interfaces/ICrossSectionDs.cs @@ -0,0 +1,15 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using MapsModels.DsModels; +using MapsDb.Models; +namespace MapsDb.Interfaces +{ + public interface ICrossSectionDs + { + Task> GetIndexListAsync(PaginationDsM pagination); + Task CreateAsync(CrossSectionEditDsM CrossSection); + Task UpdateAsync(CrossSectionEditDsM CrossSection, int id); + Task DeleteAsync(int Id); + + } +} \ No newline at end of file diff --git a/src/MapsDb/Models/CrossSection.cs b/src/MapsDb/Models/CrossSection.cs index 713dff6..3dd0b00 100644 --- a/src/MapsDb/Models/CrossSection.cs +++ b/src/MapsDb/Models/CrossSection.cs @@ -5,7 +5,7 @@ namespace MapsDb.Models { public partial class CrossSection { - public int CrossSectionId { get; set; } + public int Id { get; set; } public int? RegionId { get; set; } public int? RoadId { get; set; } public double? LocationLeft { get; set; } diff --git a/src/MapsDb/PostgresDbContext.cs b/src/MapsDb/PostgresDbContext.cs index a914a4d..4edb530 100644 --- a/src/MapsDb/PostgresDbContext.cs +++ b/src/MapsDb/PostgresDbContext.cs @@ -99,7 +99,7 @@ namespace MapsDb { entity.ToTable("cross_section"); - entity.Property(e => e.CrossSectionId).HasColumnName("cross_section_id"); + entity.Property(e => e.Id).HasColumnName("id"); entity.Property(e => e.Angle).HasColumnName("angle"); diff --git a/src/MapsModels/DsModels/CrossSectionEditDsM.cs b/src/MapsModels/DsModels/CrossSectionEditDsM.cs new file mode 100644 index 0000000..7b385ca --- /dev/null +++ b/src/MapsModels/DsModels/CrossSectionEditDsM.cs @@ -0,0 +1,23 @@ +namespace MapsModels.DsModels +{ + public class CrossSectionEditDsM + { + public int Id { get; set; } + public int? RegionId { get; set; } + public int? RoadId { get; set; } + public double? LocationLeft { get; set; } + public double? LocationRight { get; set; } + public string Direction { get; set; } + public int? SurfaceTypeId { get; set; } + public double? LengthSection { get; set; } + public double? LengthSurface { get; set; } + public double? DistanceEdge { get; set; } + public double? Width { get; set; } + public double? Angle { get; set; } + public int? TubeAvailability { get; set; } + public int? SafetyAvailability { get; set; } + public int? YearBuild { get; set; } + public int? YearRepair { get; set; } + public int? StateCommonId { get; set; } + } +} \ No newline at end of file diff --git a/src/MapsModels/ViewModels/CrossSectionListVm.cs b/src/MapsModels/ViewModels/CrossSectionListVm.cs new file mode 100644 index 0000000..1ee04c1 --- /dev/null +++ b/src/MapsModels/ViewModels/CrossSectionListVm.cs @@ -0,0 +1,10 @@ +using System.Collections.Generic; +using MapsModels.DsModels; + +namespace MapsModels.ViewModels +{ + public class CrossSectionListVm + { + public List CrossSectionEditDsM { get; set; } + } +} -- libgit2 0.21.4