Commit 405bb8be4d26ba6af67691a5db7d8b812850eb43
1 parent
0d66243f
Road
Showing
11 changed files
with
260 additions
and
3 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 RoadController : Controller | |
16 | + { | |
17 | + private readonly IRoadDs _roadDs; | |
18 | + private readonly IRoadTypeDs _roadTypeDs; | |
19 | + | |
20 | + public RoadController(IRoadDs RoadDs, IRoadTypeDs RoadTypeDs) | |
21 | + { | |
22 | + _roadDs = RoadDs; | |
23 | + _roadTypeDs = RoadTypeDs; | |
24 | + } | |
25 | + | |
26 | + // GET: Road | |
27 | + [HttpGet] | |
28 | + public async Task<IActionResult> Index([FromQuery] PaginationDsM data) | |
29 | + { | |
30 | + try | |
31 | + { | |
32 | + var roads = await _roadDs.GetIndexListAsync(data); | |
33 | + | |
34 | + RoadListVm vm = new RoadListVm | |
35 | + { | |
36 | + RoadEditDsM = roads.ToList() | |
37 | + }; | |
38 | + | |
39 | + return Json(vm); | |
40 | + } | |
41 | + catch (NullReferenceException) | |
42 | + { | |
43 | + Response.StatusCode = 400; | |
44 | + return Json("There is no field with name " + data.sort); | |
45 | + } | |
46 | + catch (Exception) | |
47 | + { | |
48 | + return NotFound(); | |
49 | + } | |
50 | + | |
51 | + | |
52 | + } | |
53 | + | |
54 | + [HttpGet] | |
55 | + public async Task<IActionResult> Directory(){ | |
56 | + var RoadTypes = await _roadTypeDs.GetSelectListAsync(); | |
57 | + | |
58 | + CatalogListVm vm = new CatalogListVm | |
59 | + { | |
60 | + RoadTypeSelectListDsM = RoadTypes.ToList() | |
61 | + }; | |
62 | + return Json(vm); | |
63 | + } | |
64 | + | |
65 | + | |
66 | + [HttpPost] | |
67 | + public async Task<IActionResult> Create([FromBody] RoadEditDsM data) | |
68 | + { | |
69 | + var result = await _roadDs.CreateAsync(data); | |
70 | + return Json(result); | |
71 | + } | |
72 | + | |
73 | + [HttpPost] | |
74 | + public async Task<IActionResult> Update(int id, [FromBody] RoadEditDsM data){ | |
75 | + await _roadDs.UpdateAsync(data,id); | |
76 | + return Json(String.Empty); | |
77 | + } | |
78 | + | |
79 | + | |
80 | + [HttpDelete] | |
81 | + public async Task<IActionResult> Delete(int id) | |
82 | + { | |
83 | + try | |
84 | + { | |
85 | + int road = await _roadDs.DeleteAsync(id); | |
86 | + return Json(road); | |
87 | + } | |
88 | + catch (ArgumentNullException ) | |
89 | + { | |
90 | + return NotFound(); | |
91 | + } | |
92 | + } | |
93 | + } | |
94 | +} | ... | ... |
src/MapsDb/DataService/RoadDs.cs
... | ... | @@ -4,6 +4,9 @@ using System.Threading.Tasks; |
4 | 4 | using MapsDb.Interfaces; |
5 | 5 | using MapsDb.Models; |
6 | 6 | using MapsModels.DsModels; |
7 | +using System.Reflection; | |
8 | +using Microsoft.EntityFrameworkCore; | |
9 | + | |
7 | 10 | namespace MapsDb.DataService |
8 | 11 | { |
9 | 12 | public class RoadDs : IRoadDs |
... | ... | @@ -19,10 +22,89 @@ namespace MapsDb.DataService |
19 | 22 | { |
20 | 23 | return _context.Road.Select(x => new RoadSelectListDsM |
21 | 24 | { |
22 | - RoadId = x.RoadId, | |
25 | + RoadId = x.Id, | |
23 | 26 | Name = x.Name |
24 | 27 | }).ToList(); |
25 | 28 | } |
26 | 29 | |
30 | + public Task<IList<RoadEditDsM>> GetIndexListAsync(PaginationDsM pagination){ | |
31 | + return Task.Factory.StartNew(()=> { return GetAllRoad(pagination); }); | |
32 | + } | |
33 | + private IList<RoadEditDsM> GetAllRoad(PaginationDsM pagination) | |
34 | + { | |
35 | + var data = _context.Road.Select(Road => new RoadEditDsM | |
36 | + { | |
37 | + Id = Road.Id, | |
38 | + Name = Road.Name, | |
39 | + Value = Road.Value, | |
40 | + Length = Road.Length, | |
41 | + HistoricalBackground = Road.HistoricalBackground, | |
42 | + EconomicValue = Road.EconomicValue, | |
43 | + LawDoc = Road.LawDoc, | |
44 | + AcceptTransferDoc = Road.AcceptTransferDoc, | |
45 | + AcceptanceDoc = Road.AcceptanceDoc, | |
46 | + AuthorityAct = Road.AuthorityAct, | |
47 | + RoadTypeId = Road.RoadTypeId, | |
48 | + Index = Road.Index | |
49 | + }).Skip(pagination.from).Take(pagination.perPage); | |
50 | + switch (pagination.orderType()) | |
51 | + { | |
52 | + case "ASC": | |
53 | + return data.OrderBy(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList(); | |
54 | + | |
55 | + case "DESC": | |
56 | + return data.OrderByDescending(i => i.GetType().GetProperty(pagination.sort).GetValue(i, null)).ToList(); | |
57 | + | |
58 | + default: | |
59 | + return data.OrderByDescending(i => i.Id).ToList(); | |
60 | + } | |
61 | + | |
62 | + } | |
63 | + | |
64 | + public Task<Road> CreateAsync(RoadEditDsM data){ | |
65 | + return Task.Factory.StartNew(()=> { return Create(data); }); | |
66 | + } | |
67 | + private Road Create(RoadEditDsM data) | |
68 | + { | |
69 | + | |
70 | + Road Model = InsertModel(data); | |
71 | + _context.Road.Add(Model); | |
72 | + _context.SaveChanges(); | |
73 | + return Model; | |
74 | + } | |
75 | + public Task<Road> UpdateAsync(RoadEditDsM data, int id){ | |
76 | + return Task.Factory.StartNew(()=> { return Update(data, id); }); | |
77 | + } | |
78 | + private Road Update(RoadEditDsM data, int id) | |
79 | + { | |
80 | + Road Model = InsertModel(data); | |
81 | + Model.Id = id; | |
82 | + _context.Road.Update(Model); | |
83 | + _context.SaveChanges(); | |
84 | + return Model; | |
85 | + } | |
86 | + public Road InsertModel(RoadEditDsM data){ | |
87 | + Road Model = new Road{ | |
88 | + Name = data.Name, | |
89 | + Value = data.Value, | |
90 | + Length = data.Length, | |
91 | + HistoricalBackground = data.HistoricalBackground, | |
92 | + EconomicValue = data.EconomicValue, | |
93 | + LawDoc = data.LawDoc, | |
94 | + AcceptTransferDoc = data.AcceptTransferDoc, | |
95 | + AcceptanceDoc = data.AcceptanceDoc, | |
96 | + AuthorityAct = data.AuthorityAct, | |
97 | + RoadTypeId = data.RoadTypeId, | |
98 | + Index = data.Index | |
99 | + }; | |
100 | + return Model; | |
101 | + } | |
102 | + public async Task<int> DeleteAsync(int Id) | |
103 | + { | |
104 | + var road = await _context.Road.SingleOrDefaultAsync(x => x.Id == Id); | |
105 | + _context.Road.Remove(road); | |
106 | + return await _context.SaveChangesAsync(); | |
107 | + } | |
108 | + | |
27 | 109 | } |
28 | 110 | } |
29 | 111 | \ No newline at end of file | ... | ... |
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 RoadTypeDs : IRoadTypeDs | |
10 | + { | |
11 | + private PostgresDbContext _context; | |
12 | + public RoadTypeDs(){ | |
13 | + _context = new PostgresDbContext(); | |
14 | + } | |
15 | + public Task<IList<RoadTypeSelectListDsM>> GetSelectListAsync(){ | |
16 | + return Task.Factory.StartNew(GetSelectList); | |
17 | + } | |
18 | + private IList<RoadTypeSelectListDsM> GetSelectList() | |
19 | + { | |
20 | + return _context.RoadType.Select(x => new RoadTypeSelectListDsM | |
21 | + { | |
22 | + RoadTypeId = x.RoadTypeId, | |
23 | + Name = x.Value | |
24 | + }).ToList(); | |
25 | + } | |
26 | + | |
27 | + } | |
28 | +} | |
0 | 29 | \ No newline at end of file | ... | ... |
src/MapsDb/Interfaces/IRoadDs.cs
... | ... | @@ -7,5 +7,9 @@ namespace MapsDb.Interfaces |
7 | 7 | public interface IRoadDs |
8 | 8 | { |
9 | 9 | Task<IList<RoadSelectListDsM>> GetSelectListAsync(); |
10 | + Task<IList<RoadEditDsM>> GetIndexListAsync(PaginationDsM pagination); | |
11 | + Task<Road> CreateAsync(RoadEditDsM road); | |
12 | + Task<Road> UpdateAsync(RoadEditDsM road, int id); | |
13 | + Task<int> DeleteAsync(int Id); | |
10 | 14 | } |
11 | 15 | } |
12 | 16 | \ 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 IRoadTypeDs | |
8 | + { | |
9 | + Task<IList<RoadTypeSelectListDsM>> GetSelectListAsync(); | |
10 | + } | |
11 | +} | |
0 | 12 | \ No newline at end of file | ... | ... |
src/MapsDb/Models/Road.cs
... | ... | @@ -19,7 +19,7 @@ namespace MapsDb.Models |
19 | 19 | SettlementAddressLink = new HashSet<SettlementAddressLink>(); |
20 | 20 | } |
21 | 21 | |
22 | - public int RoadId { get; set; } | |
22 | + public int Id { get; set; } | |
23 | 23 | public string Name { get; set; } |
24 | 24 | public string Value { get; set; } |
25 | 25 | public double? Length { get; set; } | ... | ... |
src/MapsDb/PostgresDbContext.cs
... | ... | @@ -317,7 +317,7 @@ namespace MapsDb |
317 | 317 | { |
318 | 318 | entity.ToTable("road"); |
319 | 319 | |
320 | - entity.Property(e => e.RoadId).HasColumnName("road_id"); | |
320 | + entity.Property(e => e.Id).HasColumnName("id"); | |
321 | 321 | |
322 | 322 | entity.Property(e => e.AcceptTransferDoc) |
323 | 323 | .HasColumnName("accept_transfer_doc") | ... | ... |
1 | +namespace MapsModels.DsModels | |
2 | +{ | |
3 | + public class RoadEditDsM | |
4 | + { | |
5 | + public int Id { get; set; } | |
6 | + public string Name { get; set; } | |
7 | + public string Value { get; set; } | |
8 | + public double? Length { get; set; } | |
9 | + public string HistoricalBackground { get; set; } | |
10 | + public string EconomicValue { get; set; } | |
11 | + public string LawDoc { get; set; } | |
12 | + public string AcceptTransferDoc { get; set; } | |
13 | + public string AcceptanceDoc { get; set; } | |
14 | + public string AuthorityAct { get; set; } | |
15 | + public int? RoadTypeId { get; set; } | |
16 | + public int Index { get; set; } | |
17 | + | |
18 | + } | |
19 | +} | |
0 | 20 | \ No newline at end of file | ... | ... |
src/MapsModels/ViewModels/CatalogListVm.cs
... | ... | @@ -14,5 +14,6 @@ namespace MapsModels.ViewModels |
14 | 14 | public List<RegionSelectListDsM> RegionSelectListDsM { get; set; } |
15 | 15 | public List<RoadDirectionSelectListDsM> RoadDirectionSelectListDsM { get; set; } |
16 | 16 | public List<SurfaceTreatmentSelectListDsM> SurfaceTreatmentSelectListDsM { get; set; } |
17 | + public List<RoadTypeSelectListDsM> RoadTypeSelectListDsM { get; set; } | |
17 | 18 | } |
18 | 19 | } | ... | ... |