From 6b2a94542f79026661188417c38ce0bc73e7f65f Mon Sep 17 00:00:00 2001 From: yarik Date: Wed, 15 Feb 2017 17:38:11 +0200 Subject: [PATCH] SettlementAddressLink --- src/Maps/Controllers/SettlementAddressLinkController.cs | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/Maps/Startup.cs | 4 ++++ src/MapsDb/DataService/SettlementAddressLinkDs.cs | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/MapsDb/DataService/SettlementLocationDs.cs | 28 ++++++++++++++++++++++++++++ src/MapsDb/Interfaces/ISettlementAddressLinkDs.cs | 14 ++++++++++++++ src/MapsDb/Interfaces/ISettlementLocationDs.cs | 11 +++++++++++ src/MapsDb/Models/SettlementAddressLink.cs | 2 +- src/MapsDb/Models/SettlementLocation.cs | 2 +- src/MapsDb/PostgresDbContext.cs | 4 ++-- src/MapsModels/DsModels/SettlementAddressLinkEditDsM.cs | 15 +++++++++++++++ src/MapsModels/DsModels/SettlementLocationSelectListDsM.cs | 8 ++++++++ src/MapsModels/ViewModels/CatalogListVm.cs | 1 + src/MapsModels/ViewModels/SettlementAddressLinkListVm.cs | 10 ++++++++++ 13 files changed, 273 insertions(+), 4 deletions(-) create mode 100755 src/Maps/Controllers/SettlementAddressLinkController.cs create mode 100644 src/MapsDb/DataService/SettlementAddressLinkDs.cs create mode 100644 src/MapsDb/DataService/SettlementLocationDs.cs create mode 100644 src/MapsDb/Interfaces/ISettlementAddressLinkDs.cs create mode 100644 src/MapsDb/Interfaces/ISettlementLocationDs.cs create mode 100644 src/MapsModels/DsModels/SettlementAddressLinkEditDsM.cs create mode 100644 src/MapsModels/DsModels/SettlementLocationSelectListDsM.cs create mode 100644 src/MapsModels/ViewModels/SettlementAddressLinkListVm.cs diff --git a/src/Maps/Controllers/SettlementAddressLinkController.cs b/src/Maps/Controllers/SettlementAddressLinkController.cs new file mode 100755 index 0000000..811cdce --- /dev/null +++ b/src/Maps/Controllers/SettlementAddressLinkController.cs @@ -0,0 +1,106 @@ +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 SettlementAddressLinkController : Controller + { + private readonly ISettlementAddressLinkDs _settlementAddressLinkDs; + private readonly IRoadDs _roadDs; + private readonly ISettlementDs _settlementDs; + private readonly IRegionDs _regionDs; + private readonly ISettlementLocationDs _settlementLocationDs; + + public SettlementAddressLinkController(ISettlementAddressLinkDs SettlementAddressLinkDs, IRoadDs RoadDs, ISettlementDs SettlementDs, IRegionDs RegionDs, ISettlementLocationDs SettlementLocationDs) + { + _settlementAddressLinkDs = SettlementAddressLinkDs; + _roadDs = RoadDs; + _settlementDs = SettlementDs; + _regionDs = RegionDs; + _settlementLocationDs = SettlementLocationDs; + } + + // GET: SettlementAddressLink + [HttpGet] + public async Task Index([FromQuery] PaginationDsM data) + { + try + { + var settlementAddressLinks = await _settlementAddressLinkDs.GetIndexListAsync(data); + + SettlementAddressLinkListVm vm = new SettlementAddressLinkListVm + { + SettlementAddressLinkEditDsM = settlementAddressLinks.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 SettlementLocation = await _settlementLocationDs.GetSelectListAsync(); + + CatalogListVm vm = new CatalogListVm + { + SettlementSelectListDsM = Settlement.ToList(), + RoadSelectListDsM = Road.ToList(), + RegionSelectListDsM = Region.ToList(), + SettlementLocationSelectListDsM = SettlementLocation.ToList() + }; + return Json(vm); + } + + + [HttpPost] + public async Task Create([FromBody] SettlementAddressLinkEditDsM data) + { + var result = await _settlementAddressLinkDs.CreateAsync(data); + return Json(result); + } + + [HttpPost] + public async Task Update(int id, [FromBody] SettlementAddressLinkEditDsM data){ + await _settlementAddressLinkDs.UpdateAsync(data,id); + return Json(String.Empty); + } + + + [HttpDelete] + public async Task Delete(int id) + { + try + { + int settlementAddressLink = await _settlementAddressLinkDs.DeleteAsync(id); + return Json(settlementAddressLink); + } + catch (ArgumentNullException ) + { + return NotFound(); + } + } + } +} diff --git a/src/Maps/Startup.cs b/src/Maps/Startup.cs index 214db70..90e749b 100644 --- a/src/Maps/Startup.cs +++ b/src/Maps/Startup.cs @@ -53,6 +53,8 @@ namespace Maps cnf.CreateMap(); cnf.CreateMap(); cnf.CreateMap(); + cnf.CreateMap(); + cnf.CreateMap(); }); services.AddScoped(); @@ -73,6 +75,8 @@ namespace Maps services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); + services.AddScoped(); // Add framework services. services.AddApplicationInsightsTelemetry(Configuration); diff --git a/src/MapsDb/DataService/SettlementAddressLinkDs.cs b/src/MapsDb/DataService/SettlementAddressLinkDs.cs new file mode 100644 index 0000000..3c9bf93 --- /dev/null +++ b/src/MapsDb/DataService/SettlementAddressLinkDs.cs @@ -0,0 +1,72 @@ +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Threading.Tasks; +using AutoMapper; +using MapsDb.Interfaces; +using MapsDb.Models; +using MapsModels.DsModels; +using Microsoft.EntityFrameworkCore; + +namespace MapsDb.DataService +{ + public class SettlementAddressLinkDs : ISettlementAddressLinkDs + { + private PostgresDbContext _context; + public SettlementAddressLinkDs(){ + _context = new PostgresDbContext(); + } + public Task> GetIndexListAsync(PaginationDsM pagination){ + return Task.Factory.StartNew(()=> { return GetAllSettlementAddressLink(pagination); }); + } + private IList GetAllSettlementAddressLink(PaginationDsM pagination) + { + var data = _context.SettlementAddressLink.Select(SettlementAddressLink => Mapper.Map(SettlementAddressLink)).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(SettlementAddressLinkEditDsM data){ + return Task.Factory.StartNew(()=> { return Create(data); }); + } + private SettlementAddressLink Create(SettlementAddressLinkEditDsM data) + { + + SettlementAddressLink Model = InsertModel(data); + _context.SettlementAddressLink.Add(Model); + _context.SaveChanges(); + return Model; + } + public Task UpdateAsync(SettlementAddressLinkEditDsM data, int id){ + return Task.Factory.StartNew(()=> { return Update(data, id); }); + } + private SettlementAddressLink Update(SettlementAddressLinkEditDsM data, int id) + { + SettlementAddressLink Model = InsertModel(data); + Model.Id = id; + _context.SettlementAddressLink.Update(Model); + _context.SaveChanges(); + return Model; + } + public SettlementAddressLink InsertModel(SettlementAddressLinkEditDsM data){ + SettlementAddressLink Model = Mapper.Map(data); + return Model; + } + public async Task DeleteAsync(int Id) + { + var settlementAddressLink = await _context.SettlementAddressLink.SingleOrDefaultAsync(x => x.Id == Id); + _context.SettlementAddressLink.Remove(settlementAddressLink); + return await _context.SaveChangesAsync(); + } + } +} \ No newline at end of file diff --git a/src/MapsDb/DataService/SettlementLocationDs.cs b/src/MapsDb/DataService/SettlementLocationDs.cs new file mode 100644 index 0000000..603b488 --- /dev/null +++ b/src/MapsDb/DataService/SettlementLocationDs.cs @@ -0,0 +1,28 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using MapsDb.Interfaces; +using MapsDb.Models; +using MapsModels.DsModels; +namespace MapsDb.DataService +{ + public class SettlementLocationDs : ISettlementLocationDs + { + private PostgresDbContext _context; + public SettlementLocationDs(){ + _context = new PostgresDbContext(); + } + public Task> GetSelectListAsync(){ + return Task.Factory.StartNew(GetSelectList); + } + private IList GetSelectList() + { + return _context.SettlementLocation.Select(x => new SettlementLocationSelectListDsM + { + Id = x.Id, + Name = x.Value + }).ToList(); + } + + } +} \ No newline at end of file diff --git a/src/MapsDb/Interfaces/ISettlementAddressLinkDs.cs b/src/MapsDb/Interfaces/ISettlementAddressLinkDs.cs new file mode 100644 index 0000000..10e2bb3 --- /dev/null +++ b/src/MapsDb/Interfaces/ISettlementAddressLinkDs.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using MapsModels.DsModels; +using MapsDb.Models; +namespace MapsDb.Interfaces +{ + public interface ISettlementAddressLinkDs + { + Task> GetIndexListAsync(PaginationDsM pagination); + Task CreateAsync(SettlementAddressLinkEditDsM settlementAddressLink); + Task UpdateAsync(SettlementAddressLinkEditDsM settlementAddressLink, int id); + Task DeleteAsync(int Id); + } +} \ No newline at end of file diff --git a/src/MapsDb/Interfaces/ISettlementLocationDs.cs b/src/MapsDb/Interfaces/ISettlementLocationDs.cs new file mode 100644 index 0000000..c1b31c3 --- /dev/null +++ b/src/MapsDb/Interfaces/ISettlementLocationDs.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using MapsModels.DsModels; +using MapsDb.Models; +namespace MapsDb.Interfaces +{ + public interface ISettlementLocationDs + { + Task> GetSelectListAsync(); + } +} \ No newline at end of file diff --git a/src/MapsDb/Models/SettlementAddressLink.cs b/src/MapsDb/Models/SettlementAddressLink.cs index 7402c79..256a607 100644 --- a/src/MapsDb/Models/SettlementAddressLink.cs +++ b/src/MapsDb/Models/SettlementAddressLink.cs @@ -5,7 +5,7 @@ namespace MapsDb.Models { public partial class SettlementAddressLink { - public int SettlementAddressLinkId { get; set; } + public int Id { get; set; } public int? RoadId { get; set; } public int? RegionId { get; set; } public int? SettlementLocationId { get; set; } diff --git a/src/MapsDb/Models/SettlementLocation.cs b/src/MapsDb/Models/SettlementLocation.cs index fbb556e..98eaef1 100644 --- a/src/MapsDb/Models/SettlementLocation.cs +++ b/src/MapsDb/Models/SettlementLocation.cs @@ -10,7 +10,7 @@ namespace MapsDb.Models SettlementAddressLink = new HashSet(); } - public int SettlementLocationId { get; set; } + public int Id { get; set; } public string Value { get; set; } public virtual ICollection SettlementAddressLink { get; set; } diff --git a/src/MapsDb/PostgresDbContext.cs b/src/MapsDb/PostgresDbContext.cs index d4fda95..b25379e 100644 --- a/src/MapsDb/PostgresDbContext.cs +++ b/src/MapsDb/PostgresDbContext.cs @@ -728,7 +728,7 @@ namespace MapsDb { entity.ToTable("settlement_address_link"); - entity.Property(e => e.SettlementAddressLinkId).HasColumnName("settlement_address_link_id"); + entity.Property(e => e.Id).HasColumnName("id"); entity.Property(e => e.Begin).HasColumnName("begin"); @@ -773,7 +773,7 @@ namespace MapsDb { entity.ToTable("settlement_location"); - entity.Property(e => e.SettlementLocationId).HasColumnName("settlement_location_id"); + entity.Property(e => e.Id).HasColumnName("id"); entity.Property(e => e.Value) .IsRequired() diff --git a/src/MapsModels/DsModels/SettlementAddressLinkEditDsM.cs b/src/MapsModels/DsModels/SettlementAddressLinkEditDsM.cs new file mode 100644 index 0000000..51460e2 --- /dev/null +++ b/src/MapsModels/DsModels/SettlementAddressLinkEditDsM.cs @@ -0,0 +1,15 @@ +namespace MapsModels.DsModels +{ + public class SettlementAddressLinkEditDsM + { + public int Id { get; set; } + public int? RoadId { get; set; } + public int? RegionId { get; set; } + public int? SettlementLocationId { get; set; } + public int? SettlementId { get; set; } + public double? Begin { get; set; } + public double? End { get; set; } + public double? Distance { get; set; } + + } +} \ No newline at end of file diff --git a/src/MapsModels/DsModels/SettlementLocationSelectListDsM.cs b/src/MapsModels/DsModels/SettlementLocationSelectListDsM.cs new file mode 100644 index 0000000..38ed02a --- /dev/null +++ b/src/MapsModels/DsModels/SettlementLocationSelectListDsM.cs @@ -0,0 +1,8 @@ +namespace MapsModels.DsModels +{ + public class SettlementLocationSelectListDsM + { + public int Id { get; set; } + public string Name { get; set; } + } +} \ No newline at end of file diff --git a/src/MapsModels/ViewModels/CatalogListVm.cs b/src/MapsModels/ViewModels/CatalogListVm.cs index 9196297..7af4bad 100644 --- a/src/MapsModels/ViewModels/CatalogListVm.cs +++ b/src/MapsModels/ViewModels/CatalogListVm.cs @@ -16,5 +16,6 @@ namespace MapsModels.ViewModels public List SurfaceTreatmentSelectListDsM { get; set; } public List RoadTypeSelectListDsM { get; set; } public List OrganizationSelectListDsM { get; set; } + public List SettlementLocationSelectListDsM { get; set; } } } diff --git a/src/MapsModels/ViewModels/SettlementAddressLinkListVm.cs b/src/MapsModels/ViewModels/SettlementAddressLinkListVm.cs new file mode 100644 index 0000000..07901d9 --- /dev/null +++ b/src/MapsModels/ViewModels/SettlementAddressLinkListVm.cs @@ -0,0 +1,10 @@ +using System.Collections.Generic; +using MapsModels.DsModels; + +namespace MapsModels.ViewModels +{ + public class SettlementAddressLinkListVm + { + public List SettlementAddressLinkEditDsM { get; set; } + } +} -- libgit2 0.21.4