-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathReportingRepository.cs
More file actions
88 lines (76 loc) · 3.39 KB
/
Copy pathReportingRepository.cs
File metadata and controls
88 lines (76 loc) · 3.39 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Duber.Infrastructure.Resilience.Abstractions;
using Duber.WebSite.Infrastructure.Persistence;
using Duber.WebSite.Models;
using Microsoft.EntityFrameworkCore;
namespace Duber.WebSite.Infrastructure.Repository
{
public class ReportingRepository : IReportingRepository
{
private readonly ReportingContext _reportingContext;
private readonly IPolicyAsyncExecutor _resilientAsyncSqlExecutor;
private readonly IPolicySyncExecutor _resilientSyncSqlExecutor;
public ReportingRepository(ReportingContext reportingContext, IPolicyAsyncExecutor resilientAsyncSqlExecutor, IPolicySyncExecutor resilientSyncSqlExecutor)
{
_reportingContext = reportingContext ?? throw new ArgumentNullException(nameof(reportingContext));
_resilientAsyncSqlExecutor = resilientAsyncSqlExecutor ?? throw new ArgumentNullException(nameof(resilientAsyncSqlExecutor));
_resilientSyncSqlExecutor = resilientSyncSqlExecutor ?? throw new ArgumentNullException(nameof(resilientSyncSqlExecutor));
}
public async Task AddTripAsync(Trip trip)
{
_reportingContext.Trips.Add(trip);
await _resilientAsyncSqlExecutor.ExecuteAsync(async () => await _reportingContext.SaveChangesAsync());
}
public void AddTrip(Trip trip)
{
_reportingContext.Trips.Add(trip);
_resilientSyncSqlExecutor.Execute(() => _reportingContext.SaveChanges());
}
public void UpdateTrip(Trip trip)
{
_reportingContext.Attach(trip);
_resilientSyncSqlExecutor.Execute(() => _reportingContext.SaveChanges());
}
public async Task UpdateTripAsync(Trip trip)
{
_reportingContext.Attach(trip);
await _resilientAsyncSqlExecutor.ExecuteAsync(async () => await _reportingContext.SaveChangesAsync());
}
public async Task<IList<Trip>> GetTripsAsync()
{
return await _resilientAsyncSqlExecutor.ExecuteAsync(async () => await _reportingContext.Trips.ToListAsync());
}
public async Task<Trip> GetTripAsync(Guid tripId)
{
return await _resilientAsyncSqlExecutor.ExecuteAsync(async () =>
await _reportingContext.Trips.SingleOrDefaultAsync(x => x.Id == tripId));
}
public Trip GetTrip(Guid tripId)
{
return _resilientSyncSqlExecutor.Execute(() => _reportingContext.Trips.SingleOrDefault(x => x.Id == tripId));
}
public async Task<IList<Trip>> GetTripsByUserAsync(int userId)
{
return await _resilientAsyncSqlExecutor.ExecuteAsync(async () =>
await _reportingContext.Trips
.Where(x => x.UserId == userId)
.OrderByDescending(x => x.Created)
.ToListAsync());
}
public async Task<IList<Trip>> GetTripsByDriverAsync(int driverid)
{
return await _resilientAsyncSqlExecutor.ExecuteAsync(async () =>
await _reportingContext.Trips
.Where(x => x.DriverId == driverid)
.OrderByDescending(x => x.Created)
.ToListAsync());
}
public void Dispose()
{
_reportingContext?.Dispose();
}
}
}