RoadWidthDs.cs
3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using MapsDb.Interfaces;
using MapsDb.Models;
using MapsModels.DsModels;
using Microsoft.EntityFrameworkCore;
namespace MapsDb.DataService
{
public class RoadWidthDs : IRoadWidthDs
{
private PostgresDbContext _context;
public RoadWidthDs(){
_context = new PostgresDbContext();
}
public Task<IList<RoadWidthEditDsM>> GetIndexListAsync(PaginationDsM pagination){
return Task.Factory.StartNew(()=> { return GetAllRoadWidth(pagination); });
}
private IList<RoadWidthEditDsM> GetAllRoadWidth(PaginationDsM pagination)
{
var data = _context.RoadWidth.Select(RoadWidth => new RoadWidthEditDsM
{
Id = RoadWidth.Id,
RoadId = RoadWidth.RoadId,
RegionId = RoadWidth.RegionId,
Begin = RoadWidth.Begin,
End = RoadWidth.End,
WidthRoadsideLeft = RoadWidth.WidthRoadsideLeft,
WidthReverseRoad = RoadWidth.WidthReverseRoad,
WidthStrip = RoadWidth.WidthStrip,
WidthRoadwayForward = RoadWidth.WidthRoadwayForward,
WidthRoadsideRight = RoadWidth.WidthRoadsideRight,
CountLaneLeft = RoadWidth.CountLaneLeft,
CountLaneRight = RoadWidth.CountLaneRight,
}).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<RoadWidth> CreateAsync(RoadWidthEditDsM data){
return Task.Factory.StartNew(()=> { return Create(data); });
}
private RoadWidth Create(RoadWidthEditDsM data)
{
RoadWidth Model = InsertModel(data);
_context.RoadWidth.Add(Model);
_context.SaveChanges();
return Model;
}
public Task<RoadWidth> UpdateAsync(RoadWidthEditDsM data, int id){
return Task.Factory.StartNew(()=> { return Update(data, id); });
}
private RoadWidth Update(RoadWidthEditDsM data, int id)
{
RoadWidth Model = InsertModel(data);
Model.Id = id;
_context.RoadWidth.Update(Model);
_context.SaveChanges();
return Model;
}
public RoadWidth InsertModel(RoadWidthEditDsM data){
RoadWidth Model = new RoadWidth{
RoadId = data.RoadId,
RegionId = data.RegionId,
Begin = data.Begin,
End = data.End,
WidthRoadsideLeft = data.WidthRoadsideLeft,
WidthReverseRoad = data.WidthReverseRoad,
WidthStrip = data.WidthStrip,
WidthRoadwayForward = data.WidthRoadwayForward,
WidthRoadsideRight = data.WidthRoadsideRight,
CountLaneLeft = data.CountLaneLeft,
CountLaneRight = data.CountLaneRight,
};
return Model;
}
public async Task<int> DeleteAsync(int Id)
{
var RoadWidth = await _context.RoadWidth.SingleOrDefaultAsync(x => x.Id == Id);
_context.RoadWidth.Remove(RoadWidth);
return await _context.SaveChangesAsync();
}
}
}