From da981eb590e4acbe6bec661aba52d1f9c545933a Mon Sep 17 00:00:00 2001 From: yarik Date: Tue, 14 Feb 2017 13:29:02 +0200 Subject: [PATCH] FlowIntensity --- src/Maps/Controllers/FlowIntensityController.cs | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/Maps/Startup.cs | 1 + src/MapsDb/DataService/FlowIntensityDs.cs | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/MapsDb/Interfaces/IFlowIntensityDs.cs | 14 ++++++++++++++ src/MapsDb/Models/FlowIntensity.cs | 2 +- src/MapsModels/DsModels/FlowIntensityEditDsM.cs | 34 ++++++++++++++++++++++++++++++++++ 6 files changed, 280 insertions(+), 1 deletion(-) create mode 100755 src/Maps/Controllers/FlowIntensityController.cs create mode 100644 src/MapsDb/DataService/FlowIntensityDs.cs create mode 100644 src/MapsDb/Interfaces/IFlowIntensityDs.cs create mode 100644 src/MapsModels/DsModels/FlowIntensityEditDsM.cs diff --git a/src/Maps/Controllers/FlowIntensityController.cs b/src/Maps/Controllers/FlowIntensityController.cs new file mode 100755 index 0000000..7506199 --- /dev/null +++ b/src/Maps/Controllers/FlowIntensityController.cs @@ -0,0 +1,104 @@ +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 FlowIntensityController : Controller + { + private readonly IFlowIntensityDs _flowIntensityDs; + private readonly IRoadDs _roadDs; + private readonly IRegionDs _regionDs; + private readonly IRoadDirectionDs _roadDirectionDs; + private readonly ISettlementDs _settlementDs; + + public FlowIntensityController(IFlowIntensityDs FlowIntensityDs, IRoadDs RoadDs, ISettlementDs SettlementDs, IRegionDs RegionDs, IRoadDirectionDs RoadDirectionDs) + { + _flowIntensityDs = FlowIntensityDs; + _roadDs = RoadDs; + _settlementDs = SettlementDs; + _regionDs = RegionDs; + _roadDirectionDs = RoadDirectionDs; + } + + // GET: FlowIntensity + [HttpGet] + public async Task Index([FromQuery] PaginationDsM data) + { + try + { + var flowIntensitys = await _flowIntensityDs.GetIndexListAsync(data); + + FlowIntensityListVm vm = new FlowIntensityListVm + { + FlowIntensityEditDsM = flowIntensitys.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 Settlement = await _settlementDs.GetSelectListAsync(); + var Road = await _roadDs.GetSelectListAsync(); + var Region = await _regionDs.GetSelectListAsync(); + var RoadDirection = await _roadDirectionDs.GetSelectListAsync(); + + CatalogListVm vm = new CatalogListVm + { + SettlementSelectListDsM = Settlement.ToList(), + RoadSelectListDsM = Road.ToList(), + RegionSelectListDsM = Region.ToList(), + RoadDirectionSelectListDsM = RoadDirection.ToList(), + }; + return Json(vm); + } + + + [HttpPost] + public async Task Create([FromBody] FlowIntensityEditDsM data) + { + var result = await _flowIntensityDs.CreateAsync(data); + return Json(result); + } + + [HttpPost] + public async Task Update(int id, [FromBody] FlowIntensityEditDsM data){ + await _flowIntensityDs.UpdateAsync(data,id); + return Json(String.Empty); + } + + + [HttpDelete] + public async Task Delete(int id) + { + try + { + int flowIntensity = await _flowIntensityDs.DeleteAsync(id); + return Json(flowIntensity); + } + catch (ArgumentNullException ) + { + return NotFound(); + } + } + } +} diff --git a/src/Maps/Startup.cs b/src/Maps/Startup.cs index 2a825fb..1d15f79 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(); services.AddScoped(); // Add framework services. services.AddApplicationInsightsTelemetry(Configuration); diff --git a/src/MapsDb/DataService/FlowIntensityDs.cs b/src/MapsDb/DataService/FlowIntensityDs.cs new file mode 100644 index 0000000..010428f --- /dev/null +++ b/src/MapsDb/DataService/FlowIntensityDs.cs @@ -0,0 +1,126 @@ +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 FlowIntensityDs : IFlowIntensityDs + { + private PostgresDbContext _context; + public FlowIntensityDs(){ + _context = new PostgresDbContext(); + } + public Task> GetIndexListAsync(PaginationDsM pagination){ + return Task.Factory.StartNew(()=> { return GetAllFlowIntensity(pagination); }); + } + private IList GetAllFlowIntensity(PaginationDsM pagination) + { + var data = _context.FlowIntensity.Select(FlowIntensity => new FlowIntensityEditDsM + { + Id = FlowIntensity.Id, + RoadId = FlowIntensity.RoadId, + RegionId = FlowIntensity.RegionId, + Location = FlowIntensity.Location, + Begin = FlowIntensity.Begin, + End = FlowIntensity.End, + RoadDirectionId = FlowIntensity.RoadDirectionId, + SettlementId = FlowIntensity.SettlementId, + IntensityTotal = FlowIntensity.IntensityTotal, + IntensityIncrease = FlowIntensity.IntensityIncrease, + IntensityMoto = FlowIntensity.IntensityMoto, + IntensityMotoSidecar = FlowIntensity.IntensityMotoSidecar, + IntensityCar = FlowIntensity.IntensityCar, + IntensityTruckTwo = FlowIntensity.IntensityTruckTwo, + IntensityTruckTwoSix = FlowIntensity.IntensityTruckTwoSix, + IntensityTruckSixEight = FlowIntensity.IntensityTruckSixEight, + IntensityTruckEightFourteen = FlowIntensity.IntensityTruckEightFourteen, + IntensityTruckFourteen = FlowIntensity.IntensityTruckFourteen, + IntensityLorryTwelve = FlowIntensity.IntensityLorryTwelve, + IntensityLorryTwelveTwenty = FlowIntensity.IntensityLorryTwelveTwenty, + IntensityLorryTwentyThirty = FlowIntensity.IntensityLorryTwentyThirty, + IntensityLorryThirty = FlowIntensity.IntensityLorryThirty, + IntensityTractorUnderTen = FlowIntensity.IntensityTractorUnderTen, + IntensityTractorOverTen = FlowIntensity.IntensityTractorOverTen, + IntensityBus = FlowIntensity.IntensityBus, + IntensityBusCoupled = FlowIntensity.IntensityBusCoupled, + DateAdd = FlowIntensity.DateAdd, + }).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(FlowIntensityEditDsM data){ + return Task.Factory.StartNew(()=> { return Create(data); }); + } + private FlowIntensity Create(FlowIntensityEditDsM data) + { + + FlowIntensity Model = InsertModel(data); + _context.FlowIntensity.Add(Model); + _context.SaveChanges(); + return Model; + } + public Task UpdateAsync(FlowIntensityEditDsM data, int id){ + return Task.Factory.StartNew(()=> { return Update(data, id); }); + } + private FlowIntensity Update(FlowIntensityEditDsM data, int id) + { + FlowIntensity Model = InsertModel(data); + Model.Id = id; + _context.FlowIntensity.Update(Model); + _context.SaveChanges(); + return Model; + } + public FlowIntensity InsertModel(FlowIntensityEditDsM data){ + FlowIntensity Model = new FlowIntensity{ + RoadId = data.RoadId, + RegionId = data.RegionId, + Location = data.Location, + Begin = data.Begin, + End = data.End, + RoadDirectionId = data.RoadDirectionId, + SettlementId = data.SettlementId, + IntensityTotal = data.IntensityTotal, + IntensityIncrease = data.IntensityIncrease, + IntensityMoto = data.IntensityMoto, + IntensityMotoSidecar = data.IntensityMotoSidecar, + IntensityCar = data.IntensityCar, + IntensityTruckTwo = data.IntensityTruckTwo, + IntensityTruckTwoSix = data.IntensityTruckTwoSix, + IntensityTruckSixEight = data.IntensityTruckSixEight, + IntensityTruckEightFourteen = data.IntensityTruckEightFourteen, + IntensityTruckFourteen = data.IntensityTruckFourteen, + IntensityLorryTwelve = data.IntensityLorryTwelve, + IntensityLorryTwelveTwenty = data.IntensityLorryTwelveTwenty, + IntensityLorryTwentyThirty = data.IntensityLorryTwentyThirty, + IntensityLorryThirty = data.IntensityLorryThirty, + IntensityTractorUnderTen = data.IntensityTractorUnderTen, + IntensityTractorOverTen = data.IntensityTractorOverTen, + IntensityBus = data.IntensityBus, + IntensityBusCoupled = data.IntensityBusCoupled, + DateAdd = data.DateAdd, + }; + return Model; + } + public async Task DeleteAsync(int Id) + { + var flowIntensity = await _context.FlowIntensity.SingleOrDefaultAsync(x => x.Id == Id); + _context.FlowIntensity.Remove(flowIntensity); + return await _context.SaveChangesAsync(); + } + } +} \ No newline at end of file diff --git a/src/MapsDb/Interfaces/IFlowIntensityDs.cs b/src/MapsDb/Interfaces/IFlowIntensityDs.cs new file mode 100644 index 0000000..1bb6530 --- /dev/null +++ b/src/MapsDb/Interfaces/IFlowIntensityDs.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using MapsModels.DsModels; +using MapsDb.Models; +namespace MapsDb.Interfaces +{ + public interface IFlowIntensityDs + { + Task> GetIndexListAsync(PaginationDsM pagination); + Task CreateAsync(FlowIntensityEditDsM flowIntensity); + Task UpdateAsync(FlowIntensityEditDsM flowIntensity, int id); + Task DeleteAsync(int Id); + } +} \ No newline at end of file diff --git a/src/MapsDb/Models/FlowIntensity.cs b/src/MapsDb/Models/FlowIntensity.cs index 4870539..a0cc1f9 100644 --- a/src/MapsDb/Models/FlowIntensity.cs +++ b/src/MapsDb/Models/FlowIntensity.cs @@ -5,7 +5,7 @@ namespace MapsDb.Models { public partial class FlowIntensity { - public int FlowIntensityId { get; set; } + public int Id { get; set; } public int? RoadId { get; set; } public int? RegionId { get; set; } public double? Location { get; set; } diff --git a/src/MapsModels/DsModels/FlowIntensityEditDsM.cs b/src/MapsModels/DsModels/FlowIntensityEditDsM.cs new file mode 100644 index 0000000..cb9eb6b --- /dev/null +++ b/src/MapsModels/DsModels/FlowIntensityEditDsM.cs @@ -0,0 +1,34 @@ +namespace MapsModels.DsModels +{ + public class FlowIntensityEditDsM + { + public int Id { get; set; } + public int? RoadId { get; set; } + public int? RegionId { get; set; } + public double? Location { get; set; } + public double? Begin { get; set; } + public double? End { get; set; } + public int? RoadDirectionId { get; set; } + public int? SettlementId { get; set; } + public int? IntensityTotal { get; set; } + public int? IntensityIncrease { get; set; } + public int? IntensityMoto { get; set; } + public int? IntensityMotoSidecar { get; set; } + public int? IntensityCar { get; set; } + public int? IntensityTruckTwo { get; set; } + public int? IntensityTruckTwoSix { get; set; } + public int? IntensityTruckSixEight { get; set; } + public int? IntensityTruckEightFourteen { get; set; } + public int? IntensityTruckFourteen { get; set; } + public int? IntensityLorryTwelve { get; set; } + public int? IntensityLorryTwelveTwenty { get; set; } + public int? IntensityLorryTwentyThirty { get; set; } + public int? IntensityLorryThirty { get; set; } + public int? IntensityTractorUnderTen { get; set; } + public int? IntensityTractorOverTen { get; set; } + public int? IntensityBus { get; set; } + public int? IntensityBusCoupled { get; set; } + public int? DateAdd { get; set; } + + } +} \ No newline at end of file -- libgit2 0.21.4