Commit ed579efd98a6938981b2d4779d979dbf3e06058d

Authored by Administrator
1 parent 823a5ae0

add road to category

src/Maps/Controllers/RoadToCategoryController.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 RoadToCategoryController : Controller
  16 + {
  17 + private readonly IRoadDs _roadDs;
  18 + private readonly IRegionDs _regionDs;
  19 + private readonly IRoadToCategoryDs _roadToCategoryDs;
  20 + private readonly IRoadCategoryDs _roadCategoryDs;
  21 + public RoadToCategoryController(
  22 + IRoadDs RoadDs,
  23 + IRegionDs RegionDs,
  24 + IRoadCategoryDs RoadCategoryDs,
  25 + IRoadToCategoryDs RoadToCategoryDs
  26 + )
  27 + {
  28 + _roadDs = RoadDs;
  29 + _regionDs = RegionDs;
  30 + _roadCategoryDs = RoadCategoryDs;
  31 + _roadToCategoryDs = RoadToCategoryDs;
  32 + }
  33 +
  34 + // GET: BusStop
  35 + [HttpGet]
  36 + public async Task<IActionResult> Index([FromQuery] PaginationDsM data)
  37 + {
  38 +
  39 + try
  40 + {
  41 + var Data = await _roadToCategoryDs.GetIndexListAsync(data);
  42 +
  43 + RoadToCategoryListVm vm = new RoadToCategoryListVm
  44 + {
  45 + RoadToCategoryEditDsM = Data.ToList()
  46 + };
  47 +
  48 + return Json(vm);
  49 + }
  50 + catch (NullReferenceException)
  51 + {
  52 + Response.StatusCode = 400;
  53 + return Json("There is no field with name " + data.sort);
  54 + }
  55 + catch (Exception)
  56 + {
  57 + return NotFound();
  58 + }
  59 + }
  60 +
  61 + [HttpGet]
  62 + public async Task<IActionResult> Directory(){
  63 + var Road = await _roadDs.GetSelectListAsync();
  64 + var Region = await _regionDs.GetSelectListAsync();
  65 + var RoadCategory = await _roadCategoryDs.GetSelectListAsync();
  66 + CatalogListVm vm = new CatalogListVm
  67 + {
  68 + RoadSelectListDsM = Road.ToList(),
  69 + RegionSelectListDsM = Region.ToList(),
  70 + RoadCategorySelectListDsM = RoadCategory.ToList()
  71 + };
  72 + return Json(vm);
  73 + }
  74 +
  75 +
  76 + [HttpPost]
  77 + public async Task<IActionResult> Create([FromBody] RoadToCategoryEditDsM data)
  78 + {
  79 + var result = await _roadToCategoryDs.CreateAsync(data);
  80 + return Json(result);
  81 + }
  82 +
  83 + [HttpPost]
  84 + public async Task<IActionResult> Update(int id, [FromBody] RoadToCategoryEditDsM data){
  85 + await _roadToCategoryDs.UpdateAsync(data,id);
  86 + return Json(String.Empty);
  87 + }
  88 +
  89 +
  90 + [HttpDelete]
  91 + public async Task<IActionResult> Delete(int id)
  92 + {
  93 + try
  94 + {
  95 + int data = await _roadToCategoryDs.DeleteAsync(id);
  96 + return Json(data);
  97 + }
  98 + catch (ArgumentNullException )
  99 + {
  100 + return NotFound();
  101 + }
  102 + }
  103 + }
  104 +}
... ...
src/Maps/Startup.cs
... ... @@ -55,6 +55,8 @@ namespace Maps
55 55 cnf.CreateMap<RoadServiceEditDsM, RoadService>();
56 56 cnf.CreateMap<SettlementAddressLink, SettlementAddressLinkEditDsM>();
57 57 cnf.CreateMap<SettlementAddressLinkEditDsM, SettlementAddressLink>();
  58 + cnf.CreateMap<RoadToCategory, RoadToCategoryEditDsM>();
  59 + cnf.CreateMap<RoadToCategoryEditDsM, RoadToCategory>();
58 60 });
59 61  
60 62 services.AddScoped<IBusStopDs, BusStopDs>();
... ... @@ -77,6 +79,8 @@ namespace Maps
77 79 services.AddScoped<IRoadServiceDs, RoadServiceDs>();
78 80 services.AddScoped<ISettlementLocationDs, SettlementLocationDs>();
79 81 services.AddScoped<ISettlementAddressLinkDs, SettlementAddressLinkDs>();
  82 + services.AddScoped<IRoadToCategoryDs, RoadToCategoryDs>();
  83 + services.AddScoped<IRoadCategoryDs, RoadCategoryDs>();
80 84 // Add framework services.
81 85 services.AddApplicationInsightsTelemetry(Configuration);
82 86  
... ...
src/MapsDb/DataService/RoadCategoryDs.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 RoadCategoryDs : IRoadCategoryDs
  10 + {
  11 + private PostgresDbContext _context;
  12 + public RoadCategoryDs(){
  13 + _context = new PostgresDbContext();
  14 + }
  15 + public Task<IList<RoadCategorySelectListDsM>> GetSelectListAsync(){
  16 + return Task.Factory.StartNew(GetSelectList);
  17 + }
  18 + private IList<RoadCategorySelectListDsM> GetSelectList()
  19 + {
  20 + return _context.RoadCategory.Select(x => new RoadCategorySelectListDsM
  21 + {
  22 + RoadCategoryId = x.RoadCategoryId,
  23 + Value = x.Value
  24 + }).ToList();
  25 + }
  26 +
  27 + }
  28 +}
0 29 \ No newline at end of file
... ...
src/MapsDb/DataService/RoadToCategoryDs.cs 0 → 100644
  1 +using System.Collections.Generic;
  2 +using System.Linq;
  3 +using System.Reflection;
  4 +using System.Threading.Tasks;
  5 +using MapsDb.Interfaces;
  6 +using MapsDb.Models;
  7 +using MapsModels.DsModels;
  8 +using Microsoft.EntityFrameworkCore;
  9 +using AutoMapper;
  10 +namespace MapsDb.DataService
  11 +{
  12 + public class RoadToCategoryDs : IRoadToCategoryDs
  13 + {
  14 + private PostgresDbContext _context;
  15 + public RoadToCategoryDs(){
  16 + _context = new PostgresDbContext();
  17 + }
  18 + public Task<IList<RoadToCategoryEditDsM>> GetIndexListAsync(PaginationDsM pagination){
  19 + return Task.Factory.StartNew(()=> { return GetAllRoadToCategory(pagination); });
  20 + }
  21 + private IList<RoadToCategoryEditDsM> GetAllRoadToCategory(PaginationDsM pagination)
  22 + {
  23 + var data = _context.RoadToCategory
  24 + .Select(RoadToCategory => Mapper.Map<RoadToCategoryEditDsM>(RoadToCategory))
  25 + .Skip(pagination.from).Take(pagination.perPage);
  26 + switch (pagination.orderType())
  27 + {
  28 + case "ASC":
  29 + return data.OrderBy(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList();
  30 +
  31 + case "DESC":
  32 + return data.OrderByDescending(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList();
  33 +
  34 + default:
  35 + return data.OrderByDescending(i => i.Id).ToList();
  36 + }
  37 +
  38 + }
  39 +
  40 + public Task<RoadToCategory> CreateAsync(RoadToCategoryEditDsM data){
  41 + return Task.Factory.StartNew(()=> { return Create(data); });
  42 + }
  43 + private RoadToCategory Create(RoadToCategoryEditDsM data)
  44 + {
  45 +
  46 + RoadToCategory Model = InsertModel(data);
  47 + _context.RoadToCategory.Add(Model);
  48 + _context.SaveChanges();
  49 + return Model;
  50 + }
  51 + public Task<RoadToCategory> UpdateAsync(RoadToCategoryEditDsM data, int id){
  52 + return Task.Factory.StartNew(()=> { return Update(data, id); });
  53 + }
  54 + private RoadToCategory Update(RoadToCategoryEditDsM data, int id)
  55 + {
  56 + RoadToCategory Model = InsertModel(data);
  57 + Model.Id = id;
  58 + _context.RoadToCategory.Update(Model);
  59 + _context.SaveChanges();
  60 + return Model;
  61 + }
  62 + public RoadToCategory InsertModel(RoadToCategoryEditDsM data){
  63 + RoadToCategory Model = Mapper.Map<RoadToCategory>(data);
  64 + return Model;
  65 + }
  66 + public async Task<int> DeleteAsync(int Id)
  67 + {
  68 + var RoadToCategory = await _context.RoadToCategory.SingleOrDefaultAsync(x => x.Id == Id);
  69 + _context.RoadToCategory.Remove(RoadToCategory);
  70 + return await _context.SaveChangesAsync();
  71 + }
  72 + }
  73 +}
0 74 \ No newline at end of file
... ...
src/MapsDb/Interfaces/IRoadCategoryDs.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 IRoadCategoryDs
  8 + {
  9 + Task<IList<RoadCategorySelectListDsM>> GetSelectListAsync();
  10 + }
  11 +}
0 12 \ No newline at end of file
... ...
src/MapsDb/Interfaces/IRoadToCategoryDs.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 IRoadToCategoryDs
  8 + {
  9 + Task<IList<RoadToCategoryEditDsM>> GetIndexListAsync(PaginationDsM pagination);
  10 + Task<RoadToCategory> CreateAsync(RoadToCategoryEditDsM data);
  11 + Task<RoadToCategory> UpdateAsync(RoadToCategoryEditDsM data, int id);
  12 + Task<int> DeleteAsync(int Id);
  13 + }
  14 +}
0 15 \ No newline at end of file
... ...
src/MapsModels/DsModels/RoadCategorySelectListDsM.cs 0 → 100644
  1 +namespace MapsModels.DsModels
  2 +{
  3 + public class RoadCategorySelectListDsM
  4 + {
  5 + public int RoadCategoryId { get; set; }
  6 + public string Value { get; set; }
  7 + }
  8 +}
0 9 \ No newline at end of file
... ...
src/MapsModels/DsModels/RoadToCategoryEditDsM.cs 0 → 100644
  1 +namespace MapsModels.DsModels
  2 +{
  3 + public class RoadToCategoryEditDsM
  4 + {
  5 + public int Id { get; set; }
  6 + public int? RegionId { get; set; }
  7 + public int? RoadId { get; set; }
  8 + public double? Begin { get; set; }
  9 + public double? End { get; set; }
  10 + public double? Distance { get; set; }
  11 + public int? RoadCategoryId { get; set; }
  12 +
  13 + }
  14 +}
0 15 \ No newline at end of file
... ...
src/MapsModels/ViewModels/CatalogListVm.cs
... ... @@ -17,5 +17,6 @@ namespace MapsModels.ViewModels
17 17 public List<RoadTypeSelectListDsM> RoadTypeSelectListDsM { get; set; }
18 18 public List<OrganizationSelectListDsM> OrganizationSelectListDsM { get; set; }
19 19 public List<SettlementLocationSelectListDsM> SettlementLocationSelectListDsM { get; set; }
  20 + public List<RoadCategorySelectListDsM> RoadCategorySelectListDsM { get; set; }
20 21 }
21 22 }
... ...
src/MapsModels/ViewModels/RoadToCategoryListVm.cs 0 → 100644
  1 +using System.Collections.Generic;
  2 +using MapsModels.DsModels;
  3 +
  4 +namespace MapsModels.ViewModels
  5 +{
  6 + public class RoadToCategoryListVm
  7 + {
  8 + public List<RoadToCategoryEditDsM> RoadToCategoryEditDsM { get; set; }
  9 + }
  10 +}
... ...