From 8d5b269755f2b4e4044c495643424b1fdeb2c657 Mon Sep 17 00:00:00 2001 From: yarik Date: Wed, 15 Feb 2017 15:16:27 +0200 Subject: [PATCH] RoadService --- src/Maps/Controllers/RoadServiceController.cs | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/Maps/Startup.cs | 12 ++++++++++++ src/MapsDb/DataService/BusStopDs.cs | 4 ---- src/MapsDb/DataService/OrganizationDs.cs | 28 ++++++++++++++++++++++++++++ src/MapsDb/DataService/RoadServiceDs.cs | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/MapsDb/Interfaces/IOrganizationDs.cs | 11 +++++++++++ src/MapsDb/Interfaces/IRoadServiceDs.cs | 14 ++++++++++++++ src/MapsDb/Models/Organization.cs | 2 +- src/MapsDb/Models/RoadService.cs | 2 +- src/MapsDb/PostgresDbContext.cs | 4 ++-- src/MapsModels/DsModels/OrganizationSelectListDsM.cs | 8 ++++++++ src/MapsModels/DsModels/RoadServiceEditDsM.cs | 15 +++++++++++++++ src/MapsModels/ViewModels/CatalogListVm.cs | 1 + src/MapsModels/ViewModels/RoadServiceListVm.cs | 10 ++++++++++ 14 files changed, 281 insertions(+), 8 deletions(-) create mode 100755 src/Maps/Controllers/RoadServiceController.cs create mode 100644 src/MapsDb/DataService/OrganizationDs.cs create mode 100644 src/MapsDb/DataService/RoadServiceDs.cs create mode 100644 src/MapsDb/Interfaces/IOrganizationDs.cs create mode 100644 src/MapsDb/Interfaces/IRoadServiceDs.cs create mode 100644 src/MapsModels/DsModels/OrganizationSelectListDsM.cs create mode 100644 src/MapsModels/DsModels/RoadServiceEditDsM.cs create mode 100644 src/MapsModels/ViewModels/RoadServiceListVm.cs diff --git a/src/Maps/Controllers/RoadServiceController.cs b/src/Maps/Controllers/RoadServiceController.cs new file mode 100755 index 0000000..2b56078 --- /dev/null +++ b/src/Maps/Controllers/RoadServiceController.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 RoadServiceController : Controller + { + private readonly IRoadServiceDs _roadServiceDs; + private readonly IRoadDs _roadDs; + private readonly IRegionDs _regionDs; + private readonly IOrganizationDs _organizationDs; + private readonly IRoadDirectionDs _roadDirectionDs; + + public RoadServiceController(IRoadServiceDs RoadServiceDs, IRoadDs RoadDs, IRegionDs RegionDs, IOrganizationDs OrganizationDs, IRoadDirectionDs RoadDirectionDs) + { + _roadServiceDs = RoadServiceDs; + _roadDs = RoadDs; + _regionDs = RegionDs; + _organizationDs = OrganizationDs; + _roadDirectionDs = RoadDirectionDs; + } + + // GET: RoadService + [HttpGet] + public async Task Index([FromQuery] PaginationDsM data) + { + try + { + var roadServices = await _roadServiceDs.GetIndexListAsync(data); + + RoadServiceListVm vm = new RoadServiceListVm + { + RoadServiceEditDsM = roadServices.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 Organization = await _organizationDs.GetSelectListAsync(); + var RoadDirection = await _roadDirectionDs.GetSelectListAsync(); + + CatalogListVm vm = new CatalogListVm + { + RoadSelectListDsM = Road.ToList(), + RegionSelectListDsM = Region.ToList(), + OrganizationSelectListDsM = Organization.ToList(), + RoadDirectionSelectListDsM = RoadDirection.ToList(), + }; + return Json(vm); + } + + + [HttpPost] + public async Task Create([FromBody] RoadServiceEditDsM data) + { + var result = await _roadServiceDs.CreateAsync(data); + return Json(result); + } + + [HttpPost] + public async Task Update(int id, [FromBody] RoadServiceEditDsM data){ + await _roadServiceDs.UpdateAsync(data,id); + return Json(String.Empty); + } + + + [HttpDelete] + public async Task Delete(int id) + { + try + { + int roadService = await _roadServiceDs.DeleteAsync(id); + return Json(roadService); + } + catch (ArgumentNullException ) + { + return NotFound(); + } + } + } +} diff --git a/src/Maps/Startup.cs b/src/Maps/Startup.cs index 5d9f40b..214db70 100644 --- a/src/Maps/Startup.cs +++ b/src/Maps/Startup.cs @@ -12,6 +12,9 @@ using MapsDb; using MapsDb.Interfaces; using MapsDb.DataService; using MapsModels; +using AutoMapper; +using MapsDb.Models; +using MapsModels.DsModels; namespace Maps { @@ -45,6 +48,13 @@ namespace Maps })); services.AddScoped(); + Mapper.Initialize(cnf => { + cnf.CreateMap(); + cnf.CreateMap(); + cnf.CreateMap(); + cnf.CreateMap(); + }); + services.AddScoped(); services.AddScoped(); services.AddScoped(); @@ -61,6 +71,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/BusStopDs.cs b/src/MapsDb/DataService/BusStopDs.cs index ef93655..042a3a6 100644 --- a/src/MapsDb/DataService/BusStopDs.cs +++ b/src/MapsDb/DataService/BusStopDs.cs @@ -15,10 +15,6 @@ namespace MapsDb.DataService private PostgresDbContext _context; public BusStopDs(){ _context = new PostgresDbContext(); - Mapper.Initialize(cnf => { - cnf.CreateMap(); - cnf.CreateMap(); - }); } public Task> GetIndexListAsync(PaginationDsM pagination){ return Task.Factory.StartNew(()=> { return GetAllBusStop(pagination); }); diff --git a/src/MapsDb/DataService/OrganizationDs.cs b/src/MapsDb/DataService/OrganizationDs.cs new file mode 100644 index 0000000..6eda148 --- /dev/null +++ b/src/MapsDb/DataService/OrganizationDs.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 OrganizationDs : IOrganizationDs + { + private PostgresDbContext _context; + public OrganizationDs(){ + _context = new PostgresDbContext(); + } + public Task> GetSelectListAsync(){ + return Task.Factory.StartNew(GetSelectList); + } + private IList GetSelectList() + { + return _context.Organization.Select(x => new OrganizationSelectListDsM + { + Id = x.Id, + Name = x.Name + }).ToList(); + } + + } +} \ No newline at end of file diff --git a/src/MapsDb/DataService/RoadServiceDs.cs b/src/MapsDb/DataService/RoadServiceDs.cs new file mode 100644 index 0000000..14b8d21 --- /dev/null +++ b/src/MapsDb/DataService/RoadServiceDs.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 RoadServiceDs : IRoadServiceDs + { + private PostgresDbContext _context; + public RoadServiceDs(){ + _context = new PostgresDbContext(); + } + public Task> GetIndexListAsync(PaginationDsM pagination){ + return Task.Factory.StartNew(()=> { return GetAllRoadService(pagination); }); + } + private IList GetAllRoadService(PaginationDsM pagination) + { + var data = _context.RoadService.Select(RoadService => Mapper.Map(RoadService)).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(RoadServiceEditDsM data){ + return Task.Factory.StartNew(()=> { return Create(data); }); + } + private RoadService Create(RoadServiceEditDsM data) + { + + RoadService Model = InsertModel(data); + _context.RoadService.Add(Model); + _context.SaveChanges(); + return Model; + } + public Task UpdateAsync(RoadServiceEditDsM data, int id){ + return Task.Factory.StartNew(()=> { return Update(data, id); }); + } + private RoadService Update(RoadServiceEditDsM data, int id) + { + RoadService Model = InsertModel(data); + Model.Id = id; + _context.RoadService.Update(Model); + _context.SaveChanges(); + return Model; + } + public RoadService InsertModel(RoadServiceEditDsM data){ + RoadService Model = Mapper.Map(data); + return Model; + } + public async Task DeleteAsync(int Id) + { + var roadService = await _context.RoadService.SingleOrDefaultAsync(x => x.Id == Id); + _context.RoadService.Remove(roadService); + return await _context.SaveChangesAsync(); + } + } +} \ No newline at end of file diff --git a/src/MapsDb/Interfaces/IOrganizationDs.cs b/src/MapsDb/Interfaces/IOrganizationDs.cs new file mode 100644 index 0000000..c1a08a9 --- /dev/null +++ b/src/MapsDb/Interfaces/IOrganizationDs.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using MapsModels.DsModels; +using MapsDb.Models; +namespace MapsDb.Interfaces +{ + public interface IOrganizationDs + { + Task> GetSelectListAsync(); + } +} \ No newline at end of file diff --git a/src/MapsDb/Interfaces/IRoadServiceDs.cs b/src/MapsDb/Interfaces/IRoadServiceDs.cs new file mode 100644 index 0000000..994f2f3 --- /dev/null +++ b/src/MapsDb/Interfaces/IRoadServiceDs.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using MapsModels.DsModels; +using MapsDb.Models; +namespace MapsDb.Interfaces +{ + public interface IRoadServiceDs + { + Task> GetIndexListAsync(PaginationDsM pagination); + Task CreateAsync(RoadServiceEditDsM roadService); + Task UpdateAsync(RoadServiceEditDsM roadService, int id); + Task DeleteAsync(int Id); + } +} \ No newline at end of file diff --git a/src/MapsDb/Models/Organization.cs b/src/MapsDb/Models/Organization.cs index e977bc8..8f30fd3 100644 --- a/src/MapsDb/Models/Organization.cs +++ b/src/MapsDb/Models/Organization.cs @@ -10,7 +10,7 @@ namespace MapsDb.Models RoadService = new HashSet(); } - public int OrganizationId { get; set; } + public int Id { get; set; } public string Name { get; set; } public int? DateAdd { get; set; } diff --git a/src/MapsDb/Models/RoadService.cs b/src/MapsDb/Models/RoadService.cs index 820cb5f..bd2234a 100644 --- a/src/MapsDb/Models/RoadService.cs +++ b/src/MapsDb/Models/RoadService.cs @@ -5,7 +5,7 @@ namespace MapsDb.Models { public partial class RoadService { - public int RoadServiceId { get; set; } + public int Id { get; set; } public int? RoadId { get; set; } public int? RegionId { get; set; } public int? RoadDirectionId { get; set; } diff --git a/src/MapsDb/PostgresDbContext.cs b/src/MapsDb/PostgresDbContext.cs index bf5addb..d4fda95 100644 --- a/src/MapsDb/PostgresDbContext.cs +++ b/src/MapsDb/PostgresDbContext.cs @@ -288,7 +288,7 @@ namespace MapsDb { entity.ToTable("organization"); - entity.Property(e => e.OrganizationId).HasColumnName("organization_id"); + entity.Property(e => e.Id).HasColumnName("id"); entity.Property(e => e.DateAdd).HasColumnName("date_add"); @@ -419,7 +419,7 @@ namespace MapsDb { entity.ToTable("road_service"); - entity.Property(e => e.RoadServiceId).HasColumnName("road_service_id"); + entity.Property(e => e.Id).HasColumnName("id"); entity.Property(e => e.Begin).HasColumnName("begin"); diff --git a/src/MapsModels/DsModels/OrganizationSelectListDsM.cs b/src/MapsModels/DsModels/OrganizationSelectListDsM.cs new file mode 100644 index 0000000..68bf631 --- /dev/null +++ b/src/MapsModels/DsModels/OrganizationSelectListDsM.cs @@ -0,0 +1,8 @@ +namespace MapsModels.DsModels +{ + public class OrganizationSelectListDsM + { + public int Id { get; set; } + public string Name { get; set; } + } +} \ No newline at end of file diff --git a/src/MapsModels/DsModels/RoadServiceEditDsM.cs b/src/MapsModels/DsModels/RoadServiceEditDsM.cs new file mode 100644 index 0000000..93ce73f --- /dev/null +++ b/src/MapsModels/DsModels/RoadServiceEditDsM.cs @@ -0,0 +1,15 @@ +namespace MapsModels.DsModels +{ + public class RoadServiceEditDsM + { + public int Id { get; set; } + public int? RoadId { get; set; } + public int? RegionId { get; set; } + public int? RoadDirectionId { get; set; } + public int? OrganizationId { get; set; } + public double? Begin { get; set; } + public double? End { get; set; } + public int? YearBegin { get; set; } + + } +} \ No newline at end of file diff --git a/src/MapsModels/ViewModels/CatalogListVm.cs b/src/MapsModels/ViewModels/CatalogListVm.cs index ba936aa..9196297 100644 --- a/src/MapsModels/ViewModels/CatalogListVm.cs +++ b/src/MapsModels/ViewModels/CatalogListVm.cs @@ -15,5 +15,6 @@ namespace MapsModels.ViewModels public List RoadDirectionSelectListDsM { get; set; } public List SurfaceTreatmentSelectListDsM { get; set; } public List RoadTypeSelectListDsM { get; set; } + public List OrganizationSelectListDsM { get; set; } } } diff --git a/src/MapsModels/ViewModels/RoadServiceListVm.cs b/src/MapsModels/ViewModels/RoadServiceListVm.cs new file mode 100644 index 0000000..f2c0ae1 --- /dev/null +++ b/src/MapsModels/ViewModels/RoadServiceListVm.cs @@ -0,0 +1,10 @@ +using System.Collections.Generic; +using MapsModels.DsModels; + +namespace MapsModels.ViewModels +{ + public class RoadServiceListVm + { + public List RoadServiceEditDsM { get; set; } + } +} -- libgit2 0.21.4