Commit 59f80a4613260e0c6ec7140098e4c0797455b6f9
1 parent
f59d6b52
add cross section
Showing
8 changed files
with
268 additions
and
2 deletions
Show diff stats
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 CrossSectionController : Controller | |
16 | + { | |
17 | + private readonly IRoadDs _roadDs; | |
18 | + private readonly IRegionDs _regionDs; | |
19 | + private readonly ISurfaceTypeDs _surfaceTypeDs; | |
20 | + private readonly IStateCommonDs _stateCommonDs; | |
21 | + private readonly ICrossSectionDs _crossSectionDs; | |
22 | + | |
23 | + | |
24 | + public CrossSectionController( | |
25 | + ICrossSectionDs CrossSectionDs, | |
26 | + IStateCommonDs StateCommonDs, | |
27 | + IRoadDs RoadDs, | |
28 | + IRegionDs RegionDs, | |
29 | + ISurfaceTypeDs SurfaceTypeDs | |
30 | + ) | |
31 | + { | |
32 | + _roadDs = RoadDs; | |
33 | + _surfaceTypeDs = SurfaceTypeDs; | |
34 | + _regionDs = RegionDs; | |
35 | + _stateCommonDs = StateCommonDs; | |
36 | + _crossSectionDs = CrossSectionDs; | |
37 | + } | |
38 | + | |
39 | + // GET: BusStop | |
40 | + [HttpGet] | |
41 | + public async Task<IActionResult> Index([FromQuery] PaginationDsM data) | |
42 | + { | |
43 | + | |
44 | + try | |
45 | + { | |
46 | + var Data = await _crossSectionDs.GetIndexListAsync(data); | |
47 | + | |
48 | + CrossSectionListVm vm = new CrossSectionListVm | |
49 | + { | |
50 | + CrossSectionEditDsM = Data.ToList() | |
51 | + }; | |
52 | + | |
53 | + return Json(vm); | |
54 | + } | |
55 | + catch (NullReferenceException) | |
56 | + { | |
57 | + Response.StatusCode = 400; | |
58 | + return Json("There is no field with name " + data.sort); | |
59 | + } | |
60 | + catch (Exception) | |
61 | + { | |
62 | + return NotFound(); | |
63 | + } | |
64 | + } | |
65 | + | |
66 | + [HttpGet] | |
67 | + public async Task<IActionResult> Directory(){ | |
68 | + var Road = await _roadDs.GetSelectListAsync(); | |
69 | + var Region = await _regionDs.GetSelectListAsync(); | |
70 | + var SurfaceType = await _surfaceTypeDs.GetSelectListAsync(); | |
71 | + var StateCommon = await _stateCommonDs.GetSelectListAsync(); | |
72 | + | |
73 | + CatalogListVm vm = new CatalogListVm | |
74 | + { | |
75 | + RoadSelectListDsM = Road.ToList(), | |
76 | + RegionSelectListDsM = Region.ToList(), | |
77 | + SurfaceTypeSelectListDsM = SurfaceType.ToList(), | |
78 | + StateCommonSelectListDsM = StateCommon.ToList() | |
79 | + }; | |
80 | + return Json(vm); | |
81 | + } | |
82 | + | |
83 | + | |
84 | + [HttpPost] | |
85 | + public async Task<IActionResult> Create([FromBody] CrossSectionEditDsM data) | |
86 | + { | |
87 | + var result = await _crossSectionDs.CreateAsync(data); | |
88 | + return Json(result); | |
89 | + } | |
90 | + | |
91 | + [HttpPost] | |
92 | + public async Task<IActionResult> Update(int id, [FromBody] CrossSectionEditDsM data){ | |
93 | + await _crossSectionDs.UpdateAsync(data,id); | |
94 | + return Json(String.Empty); | |
95 | + } | |
96 | + | |
97 | + | |
98 | + [HttpDelete] | |
99 | + public async Task<IActionResult> Delete(int id) | |
100 | + { | |
101 | + try | |
102 | + { | |
103 | + int data = await _crossSectionDs.DeleteAsync(id); | |
104 | + return Json(data); | |
105 | + } | |
106 | + catch (ArgumentNullException ) | |
107 | + { | |
108 | + return NotFound(); | |
109 | + } | |
110 | + } | |
111 | + } | |
112 | +} | ... | ... |
src/Maps/Startup.cs
... | ... | @@ -57,6 +57,7 @@ namespace Maps |
57 | 57 | services.AddScoped<IRoadDirectionDs, RoadDirectionDs>(); |
58 | 58 | services.AddScoped<IRoadSurfaceDs, RoadSurfaceDs>(); |
59 | 59 | services.AddScoped<IRoadWidthDs, RoadWidthDs>(); |
60 | + services.AddScoped<ICrossSectionDs, CrossSectionDs>(); | |
60 | 61 | // Add framework services. |
61 | 62 | services.AddApplicationInsightsTelemetry(Configuration); |
62 | 63 | ... | ... |
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 | +using System.Reflection; | |
8 | +using Microsoft.EntityFrameworkCore; | |
9 | +namespace MapsDb.DataService | |
10 | +{ | |
11 | + public class CrossSectionDs : ICrossSectionDs | |
12 | + { | |
13 | + private PostgresDbContext _context; | |
14 | + public CrossSectionDs(){ | |
15 | + _context = new PostgresDbContext(); | |
16 | + } | |
17 | + public Task<IList<CrossSectionEditDsM>> GetIndexListAsync(PaginationDsM pagination){ | |
18 | + return Task.Factory.StartNew(()=> { return GetAllCrossSection(pagination); }); | |
19 | + } | |
20 | + private IList<CrossSectionEditDsM> GetAllCrossSection(PaginationDsM pagination) | |
21 | + { | |
22 | + var data = _context.CrossSection.Select(CrossSection => new CrossSectionEditDsM | |
23 | + { | |
24 | + Id = CrossSection.Id, | |
25 | + RoadId = CrossSection.RoadId, | |
26 | + RegionId = CrossSection.RegionId, | |
27 | + StateCommonId = CrossSection.StateCommonId, | |
28 | + YearBuild = CrossSection.YearBuild, | |
29 | + YearRepair = CrossSection.YearRepair, | |
30 | + SafetyAvailability = CrossSection.SafetyAvailability, | |
31 | + TubeAvailability = CrossSection.TubeAvailability, | |
32 | + Angle = CrossSection.Angle, | |
33 | + Width = CrossSection.Width, | |
34 | + DistanceEdge = CrossSection.DistanceEdge, | |
35 | + LengthSurface = CrossSection.LengthSurface, | |
36 | + LengthSection = CrossSection.LengthSection, | |
37 | + SurfaceTypeId = CrossSection.SurfaceTypeId, | |
38 | + Direction = CrossSection.Direction, | |
39 | + LocationRight = CrossSection.LocationRight, | |
40 | + LocationLeft = CrossSection.LocationLeft, | |
41 | + }).Skip(pagination.from).Take(pagination.perPage); | |
42 | + switch (pagination.orderType()) | |
43 | + { | |
44 | + case "ASC": | |
45 | + return data.OrderBy(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList(); | |
46 | + | |
47 | + case "DESC": | |
48 | + return data.OrderByDescending(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList(); | |
49 | + | |
50 | + default: | |
51 | + return data.OrderByDescending(i => i.Id).ToList(); | |
52 | + } | |
53 | + } | |
54 | + | |
55 | + public Task<CrossSection> CreateAsync(CrossSectionEditDsM data){ | |
56 | + return Task.Factory.StartNew(()=> { return Create(data); }); | |
57 | + } | |
58 | + private CrossSection Create(CrossSectionEditDsM data) | |
59 | + { | |
60 | + CrossSection Model = InsertModel(data); | |
61 | + _context.CrossSection.Add(Model); | |
62 | + _context.SaveChanges(); | |
63 | + return Model; | |
64 | + } | |
65 | + public Task<CrossSection> UpdateAsync(CrossSectionEditDsM data, int id){ | |
66 | + return Task.Factory.StartNew(()=> { return Update(data, id); }); | |
67 | + } | |
68 | + private CrossSection Update(CrossSectionEditDsM data, int id) | |
69 | + { | |
70 | + CrossSection Model = InsertModel(data); | |
71 | + Model.Id = id; | |
72 | + _context.CrossSection.Update(Model); | |
73 | + _context.SaveChanges(); | |
74 | + return Model; | |
75 | + } | |
76 | + public CrossSection InsertModel(CrossSectionEditDsM data){ | |
77 | + CrossSection Model = new CrossSection{ | |
78 | + Id = data.Id, | |
79 | + RoadId = data.RoadId, | |
80 | + RegionId = data.RegionId, | |
81 | + StateCommonId = data.StateCommonId, | |
82 | + YearBuild = data.YearBuild, | |
83 | + YearRepair = data.YearRepair, | |
84 | + SafetyAvailability = data.SafetyAvailability, | |
85 | + TubeAvailability = data.TubeAvailability, | |
86 | + Angle = data.Angle, | |
87 | + Width = data.Width, | |
88 | + DistanceEdge = data.DistanceEdge, | |
89 | + LengthSurface = data.LengthSurface, | |
90 | + LengthSection = data.LengthSection, | |
91 | + SurfaceTypeId = data.SurfaceTypeId, | |
92 | + Direction = data.Direction, | |
93 | + LocationRight = data.LocationRight, | |
94 | + LocationLeft = data.LocationLeft, | |
95 | + }; | |
96 | + return Model; | |
97 | + } | |
98 | + public async Task<int> DeleteAsync(int Id) | |
99 | + { | |
100 | + var CrossSection = await _context.CrossSection.SingleOrDefaultAsync(x => x.Id == Id); | |
101 | + _context.CrossSection.Remove(CrossSection); | |
102 | + return await _context.SaveChangesAsync(); | |
103 | + } | |
104 | + } | |
105 | +} | |
0 | 106 | \ No newline at end of file | ... | ... |
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 ICrossSectionDs | |
8 | + { | |
9 | + Task<IList<CrossSectionEditDsM>> GetIndexListAsync(PaginationDsM pagination); | |
10 | + Task<CrossSection> CreateAsync(CrossSectionEditDsM CrossSection); | |
11 | + Task<CrossSection> UpdateAsync(CrossSectionEditDsM CrossSection, int id); | |
12 | + Task<int> DeleteAsync(int Id); | |
13 | + | |
14 | + } | |
15 | +} | |
0 | 16 | \ No newline at end of file | ... | ... |
src/MapsDb/Models/CrossSection.cs
... | ... | @@ -5,7 +5,7 @@ namespace MapsDb.Models |
5 | 5 | { |
6 | 6 | public partial class CrossSection |
7 | 7 | { |
8 | - public int CrossSectionId { get; set; } | |
8 | + public int Id { get; set; } | |
9 | 9 | public int? RegionId { get; set; } |
10 | 10 | public int? RoadId { get; set; } |
11 | 11 | public double? LocationLeft { get; set; } | ... | ... |
src/MapsDb/PostgresDbContext.cs
... | ... | @@ -99,7 +99,7 @@ namespace MapsDb |
99 | 99 | { |
100 | 100 | entity.ToTable("cross_section"); |
101 | 101 | |
102 | - entity.Property(e => e.CrossSectionId).HasColumnName("cross_section_id"); | |
102 | + entity.Property(e => e.Id).HasColumnName("id"); | |
103 | 103 | |
104 | 104 | entity.Property(e => e.Angle).HasColumnName("angle"); |
105 | 105 | ... | ... |
1 | +namespace MapsModels.DsModels | |
2 | +{ | |
3 | + public class CrossSectionEditDsM | |
4 | + { | |
5 | + public int Id { get; set; } | |
6 | + public int? RegionId { get; set; } | |
7 | + public int? RoadId { get; set; } | |
8 | + public double? LocationLeft { get; set; } | |
9 | + public double? LocationRight { get; set; } | |
10 | + public string Direction { get; set; } | |
11 | + public int? SurfaceTypeId { get; set; } | |
12 | + public double? LengthSection { get; set; } | |
13 | + public double? LengthSurface { get; set; } | |
14 | + public double? DistanceEdge { get; set; } | |
15 | + public double? Width { get; set; } | |
16 | + public double? Angle { get; set; } | |
17 | + public int? TubeAvailability { get; set; } | |
18 | + public int? SafetyAvailability { get; set; } | |
19 | + public int? YearBuild { get; set; } | |
20 | + public int? YearRepair { get; set; } | |
21 | + public int? StateCommonId { get; set; } | |
22 | + } | |
23 | +} | |
0 | 24 | \ No newline at end of file | ... | ... |