ServiceObjectDs.cs
4.07 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
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MapsDb.Interfaces;
using MapsDb.Models;
using MapsModels.DsModels;
using Microsoft.EntityFrameworkCore;
namespace MapsDb.DataService
{
public class ServiceObjectDs : IServiceObjectDs
{
private PostgresDbContext _context;
public ServiceObjectDs(){
_context = new PostgresDbContext();
}
public Task<IList<ServiceObjectEditDsM>> GetIndexListAsync(){
return Task.Factory.StartNew(GetAllServiceObject);
}
private IList<ServiceObjectEditDsM> GetAllServiceObject()
{
return _context.ServiceObject.Select(ServiceObject => new ServiceObjectEditDsM
{
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,
}).OrderByDescending(ServiceObject => ServiceObject.ServiceObjectId).ToList();
}
public Task<ServiceObject> SaveAsync(ServiceObjectEditDsM serviceObject, int? id = null){
return Task.Factory.StartNew(()=> { return Save(serviceObject, id); });
}
private ServiceObject Save(ServiceObjectEditDsM serviceObject, int? id)
{
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);
}
_context.SaveChanges();
return Data;
}
public async Task<int> DeleteAsync(int? Id)
{
var ServiceObject = await _context.ServiceObject.SingleOrDefaultAsync(x => x.ServiceObjectId == Id);
_context.ServiceObject.Remove(ServiceObject);
return await _context.SaveChangesAsync();
}
}
}