-
Notifications
You must be signed in to change notification settings - Fork 8
/
OfferRepository.cs
131 lines (104 loc) · 4.79 KB
/
OfferRepository.cs
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using GroupFlights.Sales.Domain.Offers;
using GroupFlights.Sales.Domain.Offers.Repositories;
using GroupFlights.Sales.Infrastructure.Data.EF;
using GroupFlights.Sales.Infrastructure.Data.Json;
using GroupFlights.Sales.Infrastructure.Data.Models;
using GroupFlights.Shared.Types.Exceptions;
using Microsoft.EntityFrameworkCore;
namespace GroupFlights.Sales.Infrastructure.Data.Repositories;
internal class OfferRepository : IOfferRepository
{
private readonly SalesDbContext _dbContext;
public OfferRepository(SalesDbContext dbContext)
{
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
}
public async Task AddDraft(OfferDraft offerDraft, CancellationToken cancellationToken = default)
{
var exists = await _dbContext.Offers
.AnyAsync(o => o.Id.Equals(offerDraft.Id.Value) && o.Type.Equals(OfferDbModel.OfferDraftType),
cancellationToken);
if (exists)
{
throw new AlreadyExistsException();
}
await _dbContext.Offers.AddAsync(new OfferDbModel
{
Id = offerDraft.Id,
Object = ComplexObjectSerializer.SerializeToJson(offerDraft),
Type = OfferDbModel.OfferDraftType
}, cancellationToken);
await _dbContext.SaveChangesAsync(cancellationToken);
}
public async Task<OfferDraft> GetDraftById(OfferId offerId, CancellationToken cancellationToken = default)
{
var offerDbModel = await _dbContext.Offers
.SingleOrDefaultAsync(o => o.Id.Equals(offerId.Value) && o.Type.Equals(OfferDbModel.OfferDraftType),
cancellationToken);
if (offerDbModel is null)
{
throw new DoesNotExistException();
}
return ComplexObjectSerializer.DeserializeFromJson<OfferDraft>(offerDbModel.Object);
}
public async Task<IReadOnlyCollection<OfferDraft>> BrowseDrafts(CancellationToken cancellationToken = default)
{
return (await _dbContext.Offers.Where(_ => _.Type.Equals(OfferDbModel.OfferDraftType)).ToListAsync(cancellationToken))
.Select(o => ComplexObjectSerializer.DeserializeFromJson<OfferDraft>(o.Object))
.ToList();
}
public async Task UpdateDraft(OfferDraft offerDraft, CancellationToken cancellationToken = default)
{
var existingDraft = await _dbContext.Offers.SingleOrDefaultAsync(
o => o.Type.Equals(OfferDbModel.OfferDraftType), cancellationToken);
if (existingDraft is null)
{
throw new DoesNotExistException();
}
existingDraft.Object = ComplexObjectSerializer.SerializeToJson(offerDraft);
_dbContext.Offers.Update(existingDraft);
await _dbContext.SaveChangesAsync(cancellationToken);
}
public async Task ReplaceDraftWithOffer(Offer offer, CancellationToken cancellationToken = default)
{
var existingDraft = await _dbContext.Offers.SingleOrDefaultAsync(
o => o.Type.Equals(OfferDbModel.OfferDraftType), cancellationToken);
if (existingDraft is null)
{
throw new DoesNotExistException();
}
existingDraft.Object = ComplexObjectSerializer.SerializeToJson(offer);
existingDraft.Type = OfferDbModel.OfferType;
_dbContext.Offers.Update(existingDraft);
await _dbContext.SaveChangesAsync(cancellationToken);
}
public async Task<Offer> GetOfferById(OfferId offerId, CancellationToken cancellationToken = default)
{
var offerDbModel = await _dbContext.Offers
.SingleOrDefaultAsync(o => o.Id.Equals(offerId.Value) && o.Type.Equals(OfferDbModel.OfferType),
cancellationToken);
if (offerDbModel is null)
{
throw new DoesNotExistException();
}
return ComplexObjectSerializer.DeserializeFromJson<Offer>(offerDbModel.Object);
}
public async Task<IReadOnlyCollection<Offer>> BrowseOffers(CancellationToken cancellationToken = default)
{
return (await _dbContext.Offers.Where(_ => _.Type.Equals(OfferDbModel.OfferType)).ToListAsync(cancellationToken))
.Select(o => ComplexObjectSerializer.DeserializeFromJson<Offer>(o.Object))
.ToList();
}
public async Task UpdateOffer(Offer offer, CancellationToken cancellationToken = default)
{
var existingOffer = await _dbContext.Offers.SingleOrDefaultAsync(
o => o.Type.Equals(OfferDbModel.OfferType), cancellationToken);
if (existingOffer is null)
{
throw new DoesNotExistException();
}
existingOffer.Object = ComplexObjectSerializer.SerializeToJson(offer);
_dbContext.Offers.Update(existingOffer);
await _dbContext.SaveChangesAsync(cancellationToken);
}
}