From e02ee314ad9d20320f299b2d1c9f867268bbe2f4 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Fri, 10 Feb 2017 14:57:36 +0200 Subject: [PATCH] add update and create methods --- src/Maps/Controllers/BusStopController.cs | 11 +++-------- src/Maps/Controllers/ServiceObjectController.cs | 24 +++++++++++------------- src/Maps/Startup.cs | 1 + src/MapsDb/DataService/BusStopDs.cs | 78 +++++++++++++++++++++++++++++++++++++----------------------------------------- src/MapsDb/DataService/DepartmentAffiliationDs.cs | 6 +++--- src/MapsDb/DataService/ServiceObjectDs.cs | 74 +++++++++++++++++++++++++++++++++++--------------------------------------- src/MapsDb/DataService/ServiceObjectTypeDs.cs | 28 ++++++++++++++++++++++++++++ src/MapsDb/Interfaces/IBusStopDs.cs | 5 +++-- src/MapsDb/Interfaces/IDepartmentAffiliationDs.cs | 2 +- src/MapsDb/Interfaces/IServiceObjectDs.cs | 5 +++-- src/MapsDb/Interfaces/IServiceObjectTypeDs.cs | 11 +++++++++++ src/MapsModels/DsModels/DepartmentAffiliationListDsM.cs | 8 -------- src/MapsModels/DsModels/DepartmentAffiliationSelectListDsM.cs | 8 ++++++++ src/MapsModels/DsModels/ServiceObjectTypeSelectListDsM.cs | 8 ++++++++ src/MapsModels/ViewModels/CatalogListVm.cs | 3 ++- 15 files changed, 154 insertions(+), 118 deletions(-) create mode 100644 src/MapsDb/DataService/ServiceObjectTypeDs.cs create mode 100644 src/MapsDb/Interfaces/IServiceObjectTypeDs.cs delete mode 100644 src/MapsModels/DsModels/DepartmentAffiliationListDsM.cs create mode 100644 src/MapsModels/DsModels/DepartmentAffiliationSelectListDsM.cs create mode 100644 src/MapsModels/DsModels/ServiceObjectTypeSelectListDsM.cs diff --git a/src/Maps/Controllers/BusStopController.cs b/src/Maps/Controllers/BusStopController.cs index 85306ba..3b3efe8 100755 --- a/src/Maps/Controllers/BusStopController.cs +++ b/src/Maps/Controllers/BusStopController.cs @@ -68,27 +68,22 @@ namespace Maps.Controllers [HttpPost] public async Task Create([FromBody] BusStopEditDsM data) { - var result = await _busStopDs.SaveAsync(data); + var result = await _busStopDs.CreateAsync(data); return Json(result); } [HttpPost] public async Task Update(int id, [FromBody] BusStopEditDsM data){ - await _busStopDs.SaveAsync(data,id); + await _busStopDs.UpdateAsync(data,id); return Json(String.Empty); } [HttpDelete] - public async Task Delete(int? id) + public async Task Delete(int id) { try { - if (id == null) - { - return NotFound(); - } - int busStop = await _busStopDs.DeleteAsync(id); return Json(busStop); } diff --git a/src/Maps/Controllers/ServiceObjectController.cs b/src/Maps/Controllers/ServiceObjectController.cs index f3c4ec3..fad3207 100755 --- a/src/Maps/Controllers/ServiceObjectController.cs +++ b/src/Maps/Controllers/ServiceObjectController.cs @@ -15,18 +15,20 @@ namespace Maps.Controllers public class ServiceObjectController : Controller { private readonly IServiceObjectDs _serviceObjectDs; + private readonly IServiceObjectTypeDs _serviceObjectTypeDs; private readonly IRoadDs _roadDs; private readonly ISettlementDs _settlementDs; private readonly IRegionDs _regionDs; private readonly IDepartmentAffiliationDs _departmentAffiliationDs; - public ServiceObjectController(IServiceObjectDs ServiceObjectDs, IRoadDs RoadDs, ISettlementDs SettlementDs, IRegionDs RegionDs, IDepartmentAffiliationDs DepartmentAffiliationDs) + public ServiceObjectController(IServiceObjectTypeDs ServiceObjectTypeDs, IServiceObjectDs ServiceObjectDs, IRoadDs RoadDs, ISettlementDs SettlementDs, IRegionDs RegionDs, IDepartmentAffiliationDs DepartmentAffiliationDs) { _serviceObjectDs = ServiceObjectDs; _roadDs = RoadDs; _settlementDs = SettlementDs; _regionDs = RegionDs; - _departmentAffiliationDs = DepartmentAffiliationDs; + _departmentAffiliationDs = DepartmentAffiliationDs; + _serviceObjectTypeDs = ServiceObjectTypeDs; } // GET: BusStop @@ -49,11 +51,12 @@ namespace Maps.Controllers var Settlement = await _settlementDs.GetSelectListAsync(); var Road = await _roadDs.GetSelectListAsync(); var Region = await _regionDs.GetSelectListAsync(); - + var ServiceObjectType = await _serviceObjectTypeDs.GetSelectListAsync(); CatalogListVm vm = new CatalogListVm { - DepartmentAffiliationDsM = DepartmentAffiliation.ToList(), + DepartmentAffiliationSelectListDsM = DepartmentAffiliation.ToList(), SettlementSelectListDsM = Settlement.ToList(), + ServiceObjectTypeSelectListDsM = ServiceObjectType.ToList(), RoadSelectListDsM = Road.ToList(), RegionSelectListDsM = Region.ToList() }; @@ -64,28 +67,23 @@ namespace Maps.Controllers [HttpPost] public async Task Create([FromBody] ServiceObjectEditDsM data) { - var result = await _serviceObjectDs.SaveAsync(data); + var result = await _serviceObjectDs.CreateAsync(data); return Json(result); } [HttpPost] public async Task Update(int id, [FromBody] ServiceObjectEditDsM data){ - await _serviceObjectDs.SaveAsync(data,id); + await _serviceObjectDs.UpdateAsync(data,id); return Json(String.Empty); } [HttpDelete] - public async Task Delete(int? id) + public async Task Delete(int id) { try { - if (id == null) - { - return NotFound(); - } - - int data = await _serviceObjectDs.DeleteAsync(id); + int data = await _serviceObjectDs.DeleteAsync(id); return Json(data); } catch (ArgumentNullException ) diff --git a/src/Maps/Startup.cs b/src/Maps/Startup.cs index 6f25d3c..6074571 100644 --- a/src/Maps/Startup.cs +++ b/src/Maps/Startup.cs @@ -50,6 +50,7 @@ namespace Maps services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); services.AddScoped(); services.AddScoped(); // Add framework services. diff --git a/src/MapsDb/DataService/BusStopDs.cs b/src/MapsDb/DataService/BusStopDs.cs index c96d704..5da72b8 100644 --- a/src/MapsDb/DataService/BusStopDs.cs +++ b/src/MapsDb/DataService/BusStopDs.cs @@ -37,51 +37,47 @@ namespace MapsDb.DataService }).OrderByDescending(BusStop => BusStop.BusStopId).ToList(); } - public Task SaveAsync(BusStopEditDsM busStop, int? id = null){ - return Task.Factory.StartNew(()=> { return Save(busStop, id); }); + public Task CreateAsync(BusStopEditDsM data){ + return Task.Factory.StartNew(()=> { return Create(data); }); } - private BusStop Save(BusStopEditDsM busStop, int? id) + private BusStop Create(BusStopEditDsM data) { - BusStop Bs = new BusStop{ - RoadId = busStop.RoadId, - RegionId = busStop.RegionId, - SettlementId = busStop.SettlementId, - LocationLeft = busStop.LocationLeft, - LocationRight = busStop.LocationRight, - SurfaceTypeId = busStop.SurfaceTypeId, - AreaStopAvailability = busStop.AreaStopAvailability, - AreaLandAvailability = busStop.AreaLandAvailability, - PocketAvailability = busStop.PocketAvailability, - ToiletAvailability = busStop.ToiletAvailability, - YearBuild = busStop.YearBuild, - YearRepair = busStop.YearRepair, - StateCommonId = busStop.StateCommonId - }; - var busStopFromDb = _context.BusStop.FirstOrDefault(x => x.BusStopId == id); - if(busStopFromDb != null) - { - busStopFromDb.RoadId = busStop.RoadId; - busStopFromDb.RegionId = busStop.RegionId; - busStopFromDb.SettlementId = busStop.SettlementId; - busStopFromDb.LocationLeft = busStop.LocationLeft; - busStopFromDb.LocationRight = busStop.LocationRight; - busStopFromDb.SurfaceTypeId = busStop.SurfaceTypeId; - busStopFromDb.AreaStopAvailability = busStop.AreaStopAvailability; - busStopFromDb.AreaLandAvailability = busStop.AreaLandAvailability; - busStopFromDb.PocketAvailability = busStop.PocketAvailability; - busStopFromDb.ToiletAvailability = busStop.ToiletAvailability; - busStopFromDb.YearBuild = busStop.YearBuild; - busStopFromDb.YearRepair = busStop.YearRepair; - busStopFromDb.StateCommonId = busStop.StateCommonId; - } - else - { - _context.BusStop.Add(Bs); - } + + BusStop Model = InsertModel(data); + _context.BusStop.Add(Model); _context.SaveChanges(); - return Bs; + return Model; + } + public Task UpdateAsync(BusStopEditDsM data, int id){ + return Task.Factory.StartNew(()=> { return Update(data, id); }); + } + private BusStop Update(BusStopEditDsM data, int id) + { + BusStop Model = InsertModel(data); + Model.BusStopId = id; + _context.BusStop.Update(Model); + _context.SaveChanges(); + return Model; + } + public BusStop InsertModel(BusStopEditDsM data){ + BusStop Model = new BusStop{ + RoadId = data.RoadId, + RegionId = data.RegionId, + SettlementId = data.SettlementId, + LocationLeft = data.LocationLeft, + LocationRight = data.LocationRight, + SurfaceTypeId = data.SurfaceTypeId, + AreaStopAvailability = data.AreaStopAvailability, + AreaLandAvailability = data.AreaLandAvailability, + PocketAvailability = data.PocketAvailability, + ToiletAvailability = data.ToiletAvailability, + YearBuild = data.YearBuild, + YearRepair = data.YearRepair, + StateCommonId = data.StateCommonId + }; + return Model; } - public async Task DeleteAsync(int? Id) + public async Task DeleteAsync(int Id) { var busStop = await _context.BusStop.SingleOrDefaultAsync(x => x.BusStopId == Id); _context.BusStop.Remove(busStop); diff --git a/src/MapsDb/DataService/DepartmentAffiliationDs.cs b/src/MapsDb/DataService/DepartmentAffiliationDs.cs index 6865c15..c7e80e8 100644 --- a/src/MapsDb/DataService/DepartmentAffiliationDs.cs +++ b/src/MapsDb/DataService/DepartmentAffiliationDs.cs @@ -12,12 +12,12 @@ namespace MapsDb.DataService public DepartmentAffiliationDs(){ _context = new PostgresDbContext(); } - public Task> GetSelectListAsync(){ + public Task> GetSelectListAsync(){ return Task.Factory.StartNew(GetSelectList); } - private IList GetSelectList() + private IList GetSelectList() { - return _context.DepartmentAffiliation.Select(x => new DepartmentAffiliationListDsM + return _context.DepartmentAffiliation.Select(x => new DepartmentAffiliationSelectListDsM { DepartmentAffiliationId = x.DepartmentAffiliationId, Name = x.Name diff --git a/src/MapsDb/DataService/ServiceObjectDs.cs b/src/MapsDb/DataService/ServiceObjectDs.cs index d9b4b31..026927f 100644 --- a/src/MapsDb/DataService/ServiceObjectDs.cs +++ b/src/MapsDb/DataService/ServiceObjectDs.cs @@ -35,49 +35,45 @@ namespace MapsDb.DataService }).OrderByDescending(ServiceObject => ServiceObject.ServiceObjectId).ToList(); } - public Task SaveAsync(ServiceObjectEditDsM serviceObject, int? id = null){ - return Task.Factory.StartNew(()=> { return Save(serviceObject, id); }); + public Task CreateAsync(ServiceObjectEditDsM data){ + return Task.Factory.StartNew(()=> { return Create(data); }); } - private ServiceObject Save(ServiceObjectEditDsM serviceObject, int? id) + private ServiceObject Create(ServiceObjectEditDsM data) { - ServiceObject Data = new ServiceObject{ - ServiceObjectId = serviceObject.ServiceObjectId, - RoadId = serviceObject.RoadId, - RegionId = serviceObject.RegionId, - SettlementId = serviceObject.SettlementId, - LocationLeft = serviceObject.LocationLeft, - LocationRight = serviceObject.LocationRight, - ServiceObjectTypeId = serviceObject.ServiceObjectTypeId, - DepartmentAffiliationId = serviceObject.DepartmentAffiliationId, - LocationAxis = serviceObject.LocationAxis, - Distance = serviceObject.Distance, - Capacity = serviceObject.Capacity, - ArrangementElements = serviceObject.ArrangementElements, - }; - var ServiceObjectFromDb = _context.ServiceObject.FirstOrDefault(x => x.ServiceObjectId == id); - if(ServiceObjectFromDb != null) - { - ServiceObjectFromDb.ServiceObjectId = Data.ServiceObjectId; - ServiceObjectFromDb.RoadId = Data.RoadId; - ServiceObjectFromDb.RegionId = Data.RegionId; - ServiceObjectFromDb.SettlementId = Data.SettlementId; - ServiceObjectFromDb.LocationLeft = Data.LocationLeft; - ServiceObjectFromDb.LocationRight = Data.LocationRight; - ServiceObjectFromDb.ServiceObjectTypeId = Data.ServiceObjectTypeId; - ServiceObjectFromDb.DepartmentAffiliationId = Data.DepartmentAffiliationId; - ServiceObjectFromDb.LocationAxis = Data.LocationAxis; - ServiceObjectFromDb.Distance = Data.Distance; - ServiceObjectFromDb.Capacity = Data.Capacity; - ServiceObjectFromDb.ArrangementElements = Data.ArrangementElements; - } - else - { - _context.ServiceObject.Add(Data); - } + ServiceObject Model = InsertModel(data); + _context.ServiceObject.Add(Model); _context.SaveChanges(); - return Data; + return Model; + } + public Task UpdateAsync(ServiceObjectEditDsM data, int id){ + return Task.Factory.StartNew(()=> { return Update(data, id); }); + } + private ServiceObject Update(ServiceObjectEditDsM data, int id) + { + ServiceObject Model = InsertModel(data); + Model.ServiceObjectId = id; + _context.ServiceObject.Update(Model); + _context.SaveChanges(); + return Model; + } + public ServiceObject InsertModel(ServiceObjectEditDsM data){ + ServiceObject Model = new ServiceObject{ + ServiceObjectId = data.ServiceObjectId, + RoadId = data.RoadId, + RegionId = data.RegionId, + SettlementId = data.SettlementId, + LocationLeft = data.LocationLeft, + LocationRight = data.LocationRight, + ServiceObjectTypeId = data.ServiceObjectTypeId, + DepartmentAffiliationId = data.DepartmentAffiliationId, + LocationAxis = data.LocationAxis, + Distance = data.Distance, + Capacity = data.Capacity, + ArrangementElements = data.ArrangementElements, + }; + return Model; } - public async Task DeleteAsync(int? Id) + public async Task DeleteAsync(int Id) { var ServiceObject = await _context.ServiceObject.SingleOrDefaultAsync(x => x.ServiceObjectId == Id); _context.ServiceObject.Remove(ServiceObject); diff --git a/src/MapsDb/DataService/ServiceObjectTypeDs.cs b/src/MapsDb/DataService/ServiceObjectTypeDs.cs new file mode 100644 index 0000000..69ad829 --- /dev/null +++ b/src/MapsDb/DataService/ServiceObjectTypeDs.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 ServiceObjectTypeDs : IServiceObjectTypeDs + { + private PostgresDbContext _context; + public ServiceObjectTypeDs(){ + _context = new PostgresDbContext(); + } + public Task> GetSelectListAsync(){ + return Task.Factory.StartNew(GetSelectList); + } + private IList GetSelectList() + { + return _context.ServiceObjectType.Select(x => new ServiceObjectTypeSelectListDsM + { + ServiceObjectTypeId = x.ServiceObjectTypeId, + Name = x.Name + }).ToList(); + } + + } +} \ No newline at end of file diff --git a/src/MapsDb/Interfaces/IBusStopDs.cs b/src/MapsDb/Interfaces/IBusStopDs.cs index 9d4c508..e4aeb64 100644 --- a/src/MapsDb/Interfaces/IBusStopDs.cs +++ b/src/MapsDb/Interfaces/IBusStopDs.cs @@ -7,7 +7,8 @@ namespace MapsDb.Interfaces public interface IBusStopDs { Task> GetIndexListAsync(); - Task SaveAsync(BusStopEditDsM busStop, int? id = null); - Task DeleteAsync(int? Id); + Task CreateAsync(BusStopEditDsM busStop); + Task UpdateAsync(BusStopEditDsM busStop, int id); + Task DeleteAsync(int Id); } } \ No newline at end of file diff --git a/src/MapsDb/Interfaces/IDepartmentAffiliationDs.cs b/src/MapsDb/Interfaces/IDepartmentAffiliationDs.cs index c150f74..bfa8607 100644 --- a/src/MapsDb/Interfaces/IDepartmentAffiliationDs.cs +++ b/src/MapsDb/Interfaces/IDepartmentAffiliationDs.cs @@ -6,6 +6,6 @@ namespace MapsDb.Interfaces { public interface IDepartmentAffiliationDs { - Task> GetSelectListAsync(); + Task> GetSelectListAsync(); } } \ No newline at end of file diff --git a/src/MapsDb/Interfaces/IServiceObjectDs.cs b/src/MapsDb/Interfaces/IServiceObjectDs.cs index c00eee4..568f79d 100644 --- a/src/MapsDb/Interfaces/IServiceObjectDs.cs +++ b/src/MapsDb/Interfaces/IServiceObjectDs.cs @@ -7,8 +7,9 @@ namespace MapsDb.Interfaces public interface IServiceObjectDs { Task> GetIndexListAsync(); - Task SaveAsync(ServiceObjectEditDsM serviceObject, int? id = null); - Task DeleteAsync(int? Id); + Task CreateAsync(ServiceObjectEditDsM serviceObject); + Task UpdateAsync(ServiceObjectEditDsM serviceObject, int id); + Task DeleteAsync(int Id); } } \ No newline at end of file diff --git a/src/MapsDb/Interfaces/IServiceObjectTypeDs.cs b/src/MapsDb/Interfaces/IServiceObjectTypeDs.cs new file mode 100644 index 0000000..dd24fce --- /dev/null +++ b/src/MapsDb/Interfaces/IServiceObjectTypeDs.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using MapsModels.DsModels; +using MapsDb.Models; +namespace MapsDb.Interfaces +{ + public interface IServiceObjectTypeDs + { + Task> GetSelectListAsync(); + } +} \ No newline at end of file diff --git a/src/MapsModels/DsModels/DepartmentAffiliationListDsM.cs b/src/MapsModels/DsModels/DepartmentAffiliationListDsM.cs deleted file mode 100644 index 5acb172..0000000 --- a/src/MapsModels/DsModels/DepartmentAffiliationListDsM.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace MapsModels.DsModels -{ - public class DepartmentAffiliationListDsM - { - public int DepartmentAffiliationId { get; set; } - public string Name { get; set; } - } -} \ No newline at end of file diff --git a/src/MapsModels/DsModels/DepartmentAffiliationSelectListDsM.cs b/src/MapsModels/DsModels/DepartmentAffiliationSelectListDsM.cs new file mode 100644 index 0000000..654ea7b --- /dev/null +++ b/src/MapsModels/DsModels/DepartmentAffiliationSelectListDsM.cs @@ -0,0 +1,8 @@ +namespace MapsModels.DsModels +{ + public class DepartmentAffiliationSelectListDsM + { + public int DepartmentAffiliationId { get; set; } + public string Name { get; set; } + } +} \ No newline at end of file diff --git a/src/MapsModels/DsModels/ServiceObjectTypeSelectListDsM.cs b/src/MapsModels/DsModels/ServiceObjectTypeSelectListDsM.cs new file mode 100644 index 0000000..bba6b03 --- /dev/null +++ b/src/MapsModels/DsModels/ServiceObjectTypeSelectListDsM.cs @@ -0,0 +1,8 @@ +namespace MapsModels.DsModels +{ + public class ServiceObjectTypeSelectListDsM + { + public int ServiceObjectTypeId { 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 282686b..54a3d2b 100644 --- a/src/MapsModels/ViewModels/CatalogListVm.cs +++ b/src/MapsModels/ViewModels/CatalogListVm.cs @@ -5,8 +5,9 @@ namespace MapsModels.ViewModels { public class CatalogListVm { - public List DepartmentAffiliationDsM { get; set; } + public List DepartmentAffiliationSelectListDsM { get; set; } public List SurfaceTypeSelectListDsM { get; set; } + public List ServiceObjectTypeSelectListDsM { get; set; } public List StateCommonSelectListDsM { get; set; } public List SettlementSelectListDsM { get; set; } public List RoadSelectListDsM { get; set; } -- libgit2 0.21.4