CrossSectionDs.cs
4.33 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
97
98
99
100
101
102
103
104
105
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MapsDb.Interfaces;
using MapsDb.Models;
using MapsModels.DsModels;
using System.Reflection;
using Microsoft.EntityFrameworkCore;
namespace MapsDb.DataService
{
public class CrossSectionDs : ICrossSectionDs
{
private PostgresDbContext _context;
public CrossSectionDs(){
_context = new PostgresDbContext();
}
public Task<IList<CrossSectionEditDsM>> GetIndexListAsync(PaginationDsM pagination){
return Task.Factory.StartNew(()=> { return GetAllCrossSection(pagination); });
}
private IList<CrossSectionEditDsM> GetAllCrossSection(PaginationDsM pagination)
{
var data = _context.CrossSection.Select(CrossSection => new CrossSectionEditDsM
{
Id = CrossSection.Id,
RoadId = CrossSection.RoadId,
RegionId = CrossSection.RegionId,
StateCommonId = CrossSection.StateCommonId,
YearBuild = CrossSection.YearBuild,
YearRepair = CrossSection.YearRepair,
SafetyAvailability = CrossSection.SafetyAvailability,
TubeAvailability = CrossSection.TubeAvailability,
Angle = CrossSection.Angle,
Width = CrossSection.Width,
DistanceEdge = CrossSection.DistanceEdge,
LengthSurface = CrossSection.LengthSurface,
LengthSection = CrossSection.LengthSection,
SurfaceTypeId = CrossSection.SurfaceTypeId,
Direction = CrossSection.Direction,
LocationRight = CrossSection.LocationRight,
LocationLeft = CrossSection.LocationLeft,
}).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<CrossSection> CreateAsync(CrossSectionEditDsM data){
return Task.Factory.StartNew(()=> { return Create(data); });
}
private CrossSection Create(CrossSectionEditDsM data)
{
CrossSection Model = InsertModel(data);
_context.CrossSection.Add(Model);
_context.SaveChanges();
return Model;
}
public Task<CrossSection> UpdateAsync(CrossSectionEditDsM data, int id){
return Task.Factory.StartNew(()=> { return Update(data, id); });
}
private CrossSection Update(CrossSectionEditDsM data, int id)
{
CrossSection Model = InsertModel(data);
Model.Id = id;
_context.CrossSection.Update(Model);
_context.SaveChanges();
return Model;
}
public CrossSection InsertModel(CrossSectionEditDsM data){
CrossSection Model = new CrossSection{
Id = data.Id,
RoadId = data.RoadId,
RegionId = data.RegionId,
StateCommonId = data.StateCommonId,
YearBuild = data.YearBuild,
YearRepair = data.YearRepair,
SafetyAvailability = data.SafetyAvailability,
TubeAvailability = data.TubeAvailability,
Angle = data.Angle,
Width = data.Width,
DistanceEdge = data.DistanceEdge,
LengthSurface = data.LengthSurface,
LengthSection = data.LengthSection,
SurfaceTypeId = data.SurfaceTypeId,
Direction = data.Direction,
LocationRight = data.LocationRight,
LocationLeft = data.LocationLeft,
};
return Model;
}
public async Task<int> DeleteAsync(int Id)
{
var CrossSection = await _context.CrossSection.SingleOrDefaultAsync(x => x.Id == Id);
_context.CrossSection.Remove(CrossSection);
return await _context.SaveChangesAsync();
}
}
}