Commit 6b2a94542f79026661188417c38ce0bc73e7f65f

Authored by Yarik
1 parent 8d5b2697

SettlementAddressLink

src/Maps/Controllers/SettlementAddressLinkController.cs 0 → 100755
  1 +using System.Linq;
  2 +using System.Threading.Tasks;
  3 +using Microsoft.AspNetCore.Mvc;
  4 +using Microsoft.AspNetCore.Mvc.Rendering;
  5 +using Microsoft.EntityFrameworkCore;
  6 +using MapsDb;
  7 +using MapsDb.Interfaces;
  8 +using MapsDb.DataService;
  9 +using MapsModels.ViewModels;
  10 +using MapsModels.DsModels;
  11 +using System;
  12 +
  13 +namespace Maps.Controllers
  14 +{
  15 + public class SettlementAddressLinkController : Controller
  16 + {
  17 + private readonly ISettlementAddressLinkDs _settlementAddressLinkDs;
  18 + private readonly IRoadDs _roadDs;
  19 + private readonly ISettlementDs _settlementDs;
  20 + private readonly IRegionDs _regionDs;
  21 + private readonly ISettlementLocationDs _settlementLocationDs;
  22 +
  23 + public SettlementAddressLinkController(ISettlementAddressLinkDs SettlementAddressLinkDs, IRoadDs RoadDs, ISettlementDs SettlementDs, IRegionDs RegionDs, ISettlementLocationDs SettlementLocationDs)
  24 + {
  25 + _settlementAddressLinkDs = SettlementAddressLinkDs;
  26 + _roadDs = RoadDs;
  27 + _settlementDs = SettlementDs;
  28 + _regionDs = RegionDs;
  29 + _settlementLocationDs = SettlementLocationDs;
  30 + }
  31 +
  32 + // GET: SettlementAddressLink
  33 + [HttpGet]
  34 + public async Task<IActionResult> Index([FromQuery] PaginationDsM data)
  35 + {
  36 + try
  37 + {
  38 + var settlementAddressLinks = await _settlementAddressLinkDs.GetIndexListAsync(data);
  39 +
  40 + SettlementAddressLinkListVm vm = new SettlementAddressLinkListVm
  41 + {
  42 + SettlementAddressLinkEditDsM = settlementAddressLinks.ToList()
  43 + };
  44 +
  45 + return Json(vm);
  46 + }
  47 + catch (NullReferenceException)
  48 + {
  49 + Response.StatusCode = 400;
  50 + return Json("There is no field with name " + data.sort);
  51 + }
  52 + catch (Exception)
  53 + {
  54 + return NotFound();
  55 + }
  56 +
  57 +
  58 + }
  59 +
  60 + [HttpGet]
  61 + public async Task<IActionResult> Directory(){
  62 + var Settlement = await _settlementDs.GetSelectListAsync();
  63 + var Road = await _roadDs.GetSelectListAsync();
  64 + var Region = await _regionDs.GetSelectListAsync();
  65 + var SettlementLocation = await _settlementLocationDs.GetSelectListAsync();
  66 +
  67 + CatalogListVm vm = new CatalogListVm
  68 + {
  69 + SettlementSelectListDsM = Settlement.ToList(),
  70 + RoadSelectListDsM = Road.ToList(),
  71 + RegionSelectListDsM = Region.ToList(),
  72 + SettlementLocationSelectListDsM = SettlementLocation.ToList()
  73 + };
  74 + return Json(vm);
  75 + }
  76 +
  77 +
  78 + [HttpPost]
  79 + public async Task<IActionResult> Create([FromBody] SettlementAddressLinkEditDsM data)
  80 + {
  81 + var result = await _settlementAddressLinkDs.CreateAsync(data);
  82 + return Json(result);
  83 + }
  84 +
  85 + [HttpPost]
  86 + public async Task<IActionResult> Update(int id, [FromBody] SettlementAddressLinkEditDsM data){
  87 + await _settlementAddressLinkDs.UpdateAsync(data,id);
  88 + return Json(String.Empty);
  89 + }
  90 +
  91 +
  92 + [HttpDelete]
  93 + public async Task<IActionResult> Delete(int id)
  94 + {
  95 + try
  96 + {
  97 + int settlementAddressLink = await _settlementAddressLinkDs.DeleteAsync(id);
  98 + return Json(settlementAddressLink);
  99 + }
  100 + catch (ArgumentNullException )
  101 + {
  102 + return NotFound();
  103 + }
  104 + }
  105 + }
  106 +}
... ...
src/Maps/Startup.cs
... ... @@ -53,6 +53,8 @@ namespace Maps
53 53 cnf.CreateMap<BusStopEditDsM, BusStop>();
54 54 cnf.CreateMap<RoadService, RoadServiceEditDsM>();
55 55 cnf.CreateMap<RoadServiceEditDsM, RoadService>();
  56 + cnf.CreateMap<SettlementAddressLink, SettlementAddressLinkEditDsM>();
  57 + cnf.CreateMap<SettlementAddressLinkEditDsM, SettlementAddressLink>();
56 58 });
57 59  
58 60 services.AddScoped<IBusStopDs, BusStopDs>();
... ... @@ -73,6 +75,8 @@ namespace Maps
73 75 services.AddScoped<IRoadTypeDs, RoadTypeDs>();
74 76 services.AddScoped<IOrganizationDs, OrganizationDs>();
75 77 services.AddScoped<IRoadServiceDs, RoadServiceDs>();
  78 + services.AddScoped<ISettlementLocationDs, SettlementLocationDs>();
  79 + services.AddScoped<ISettlementAddressLinkDs, SettlementAddressLinkDs>();
76 80 // Add framework services.
77 81 services.AddApplicationInsightsTelemetry(Configuration);
78 82  
... ...
src/MapsDb/DataService/SettlementAddressLinkDs.cs 0 → 100644
  1 +using System.Collections.Generic;
  2 +using System.Linq;
  3 +using System.Reflection;
  4 +using System.Threading.Tasks;
  5 +using AutoMapper;
  6 +using MapsDb.Interfaces;
  7 +using MapsDb.Models;
  8 +using MapsModels.DsModels;
  9 +using Microsoft.EntityFrameworkCore;
  10 +
  11 +namespace MapsDb.DataService
  12 +{
  13 + public class SettlementAddressLinkDs : ISettlementAddressLinkDs
  14 + {
  15 + private PostgresDbContext _context;
  16 + public SettlementAddressLinkDs(){
  17 + _context = new PostgresDbContext();
  18 + }
  19 + public Task<IList<SettlementAddressLinkEditDsM>> GetIndexListAsync(PaginationDsM pagination){
  20 + return Task.Factory.StartNew(()=> { return GetAllSettlementAddressLink(pagination); });
  21 + }
  22 + private IList<SettlementAddressLinkEditDsM> GetAllSettlementAddressLink(PaginationDsM pagination)
  23 + {
  24 + var data = _context.SettlementAddressLink.Select(SettlementAddressLink => Mapper.Map<SettlementAddressLinkEditDsM>(SettlementAddressLink)).Skip(pagination.from).Take(pagination.perPage);
  25 + switch (pagination.orderType())
  26 + {
  27 + case "ASC":
  28 + return data.OrderBy(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList();
  29 +
  30 + case "DESC":
  31 + return data.OrderByDescending(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList();
  32 +
  33 + default:
  34 + return data.OrderByDescending(i => i.Id).ToList();
  35 + }
  36 +
  37 + }
  38 +
  39 + public Task<SettlementAddressLink> CreateAsync(SettlementAddressLinkEditDsM data){
  40 + return Task.Factory.StartNew(()=> { return Create(data); });
  41 + }
  42 + private SettlementAddressLink Create(SettlementAddressLinkEditDsM data)
  43 + {
  44 +
  45 + SettlementAddressLink Model = InsertModel(data);
  46 + _context.SettlementAddressLink.Add(Model);
  47 + _context.SaveChanges();
  48 + return Model;
  49 + }
  50 + public Task<SettlementAddressLink> UpdateAsync(SettlementAddressLinkEditDsM data, int id){
  51 + return Task.Factory.StartNew(()=> { return Update(data, id); });
  52 + }
  53 + private SettlementAddressLink Update(SettlementAddressLinkEditDsM data, int id)
  54 + {
  55 + SettlementAddressLink Model = InsertModel(data);
  56 + Model.Id = id;
  57 + _context.SettlementAddressLink.Update(Model);
  58 + _context.SaveChanges();
  59 + return Model;
  60 + }
  61 + public SettlementAddressLink InsertModel(SettlementAddressLinkEditDsM data){
  62 + SettlementAddressLink Model = Mapper.Map<SettlementAddressLink>(data);
  63 + return Model;
  64 + }
  65 + public async Task<int> DeleteAsync(int Id)
  66 + {
  67 + var settlementAddressLink = await _context.SettlementAddressLink.SingleOrDefaultAsync(x => x.Id == Id);
  68 + _context.SettlementAddressLink.Remove(settlementAddressLink);
  69 + return await _context.SaveChangesAsync();
  70 + }
  71 + }
  72 +}
0 73 \ No newline at end of file
... ...
src/MapsDb/DataService/SettlementLocationDs.cs 0 → 100644
  1 +using System.Collections.Generic;
  2 +using System.Linq;
  3 +using System.Threading.Tasks;
  4 +using MapsDb.Interfaces;
  5 +using MapsDb.Models;
  6 +using MapsModels.DsModels;
  7 +namespace MapsDb.DataService
  8 +{
  9 + public class SettlementLocationDs : ISettlementLocationDs
  10 + {
  11 + private PostgresDbContext _context;
  12 + public SettlementLocationDs(){
  13 + _context = new PostgresDbContext();
  14 + }
  15 + public Task<IList<SettlementLocationSelectListDsM>> GetSelectListAsync(){
  16 + return Task.Factory.StartNew(GetSelectList);
  17 + }
  18 + private IList<SettlementLocationSelectListDsM> GetSelectList()
  19 + {
  20 + return _context.SettlementLocation.Select(x => new SettlementLocationSelectListDsM
  21 + {
  22 + Id = x.Id,
  23 + Name = x.Value
  24 + }).ToList();
  25 + }
  26 +
  27 + }
  28 +}
0 29 \ No newline at end of file
... ...
src/MapsDb/Interfaces/ISettlementAddressLinkDs.cs 0 → 100644
  1 +using System.Collections.Generic;
  2 +using System.Threading.Tasks;
  3 +using MapsModels.DsModels;
  4 +using MapsDb.Models;
  5 +namespace MapsDb.Interfaces
  6 +{
  7 + public interface ISettlementAddressLinkDs
  8 + {
  9 + Task<IList<SettlementAddressLinkEditDsM>> GetIndexListAsync(PaginationDsM pagination);
  10 + Task<SettlementAddressLink> CreateAsync(SettlementAddressLinkEditDsM settlementAddressLink);
  11 + Task<SettlementAddressLink> UpdateAsync(SettlementAddressLinkEditDsM settlementAddressLink, int id);
  12 + Task<int> DeleteAsync(int Id);
  13 + }
  14 +}
0 15 \ No newline at end of file
... ...
src/MapsDb/Interfaces/ISettlementLocationDs.cs 0 → 100644
  1 +using System.Collections.Generic;
  2 +using System.Threading.Tasks;
  3 +using MapsModels.DsModels;
  4 +using MapsDb.Models;
  5 +namespace MapsDb.Interfaces
  6 +{
  7 + public interface ISettlementLocationDs
  8 + {
  9 + Task<IList<SettlementLocationSelectListDsM>> GetSelectListAsync();
  10 + }
  11 +}
0 12 \ No newline at end of file
... ...
src/MapsDb/Models/SettlementAddressLink.cs
... ... @@ -5,7 +5,7 @@ namespace MapsDb.Models
5 5 {
6 6 public partial class SettlementAddressLink
7 7 {
8   - public int SettlementAddressLinkId { get; set; }
  8 + public int Id { get; set; }
9 9 public int? RoadId { get; set; }
10 10 public int? RegionId { get; set; }
11 11 public int? SettlementLocationId { get; set; }
... ...
src/MapsDb/Models/SettlementLocation.cs
... ... @@ -10,7 +10,7 @@ namespace MapsDb.Models
10 10 SettlementAddressLink = new HashSet<SettlementAddressLink>();
11 11 }
12 12  
13   - public int SettlementLocationId { get; set; }
  13 + public int Id { get; set; }
14 14 public string Value { get; set; }
15 15  
16 16 public virtual ICollection<SettlementAddressLink> SettlementAddressLink { get; set; }
... ...
src/MapsDb/PostgresDbContext.cs
... ... @@ -728,7 +728,7 @@ namespace MapsDb
728 728 {
729 729 entity.ToTable("settlement_address_link");
730 730  
731   - entity.Property(e => e.SettlementAddressLinkId).HasColumnName("settlement_address_link_id");
  731 + entity.Property(e => e.Id).HasColumnName("id");
732 732  
733 733 entity.Property(e => e.Begin).HasColumnName("begin");
734 734  
... ... @@ -773,7 +773,7 @@ namespace MapsDb
773 773 {
774 774 entity.ToTable("settlement_location");
775 775  
776   - entity.Property(e => e.SettlementLocationId).HasColumnName("settlement_location_id");
  776 + entity.Property(e => e.Id).HasColumnName("id");
777 777  
778 778 entity.Property(e => e.Value)
779 779 .IsRequired()
... ...
src/MapsModels/DsModels/SettlementAddressLinkEditDsM.cs 0 → 100644
  1 +namespace MapsModels.DsModels
  2 +{
  3 + public class SettlementAddressLinkEditDsM
  4 + {
  5 + public int Id { get; set; }
  6 + public int? RoadId { get; set; }
  7 + public int? RegionId { get; set; }
  8 + public int? SettlementLocationId { get; set; }
  9 + public int? SettlementId { get; set; }
  10 + public double? Begin { get; set; }
  11 + public double? End { get; set; }
  12 + public double? Distance { get; set; }
  13 +
  14 + }
  15 +}
0 16 \ No newline at end of file
... ...
src/MapsModels/DsModels/SettlementLocationSelectListDsM.cs 0 → 100644
  1 +namespace MapsModels.DsModels
  2 +{
  3 + public class SettlementLocationSelectListDsM
  4 + {
  5 + public int Id { get; set; }
  6 + public string Name { get; set; }
  7 + }
  8 +}
0 9 \ No newline at end of file
... ...
src/MapsModels/ViewModels/CatalogListVm.cs
... ... @@ -16,5 +16,6 @@ namespace MapsModels.ViewModels
16 16 public List<SurfaceTreatmentSelectListDsM> SurfaceTreatmentSelectListDsM { get; set; }
17 17 public List<RoadTypeSelectListDsM> RoadTypeSelectListDsM { get; set; }
18 18 public List<OrganizationSelectListDsM> OrganizationSelectListDsM { get; set; }
  19 + public List<SettlementLocationSelectListDsM> SettlementLocationSelectListDsM { get; set; }
19 20 }
20 21 }
... ...
src/MapsModels/ViewModels/SettlementAddressLinkListVm.cs 0 → 100644
  1 +using System.Collections.Generic;
  2 +using MapsModels.DsModels;
  3 +
  4 +namespace MapsModels.ViewModels
  5 +{
  6 + public class SettlementAddressLinkListVm
  7 + {
  8 + public List<SettlementAddressLinkEditDsM> SettlementAddressLinkEditDsM { get; set; }
  9 + }
  10 +}
... ...