-
Notifications
You must be signed in to change notification settings - Fork 7
/
DynamoDbVacationRepository.cs
58 lines (51 loc) · 2.18 KB
/
DynamoDbVacationRepository.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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Dvelop.Domain.Repositories;
using Dvelop.Domain.Vacation;
namespace Dvelop.Plugins.DynamoDbFake
{
public class DynamoDbVacationRepository : IVacationRepository
{
private readonly ITenantRepository _tenantRepository;
private readonly ConcurrentDictionary<string, Dictionary<Guid,VacationModel>> _vacationDatabase = new ConcurrentDictionary<string, Dictionary<Guid,VacationModel>>();
public DynamoDbVacationRepository(ITenantRepository tenantRepository)
{
_tenantRepository = tenantRepository;
var defaultValues = _vacationDatabase.GetOrAdd("0", new Dictionary<Guid, VacationModel>());
var value1 = new VacationModel
{
Id = Guid.NewGuid(),
Comment = "Some vacation makes me happy",
From = DateTime.UtcNow,
To = DateTime.UtcNow.AddDays(14)
};
defaultValues.Add(value1.Id, value1);
}
public Guid AddVacation(VacationModel vacation)
{
var id = Guid.NewGuid();
var tenant = _tenantRepository.TenantId;
var vacationModels = _vacationDatabase.GetOrAdd(tenant, new Dictionary<Guid,VacationModel>());
vacation.Id = id;
if (vacationModels.ContainsKey(id))
{
throw new Exception($"key {id} already exists");
}
vacationModels[id] = vacation;
return id;
}
public bool UpdateVacation(VacationModel vacation)
{
var tenant = _tenantRepository.TenantId;
var vacationModels = _vacationDatabase.GetOrAdd(tenant, new Dictionary<Guid,VacationModel>());
if (!vacationModels.ContainsKey(vacation.Id))
{
throw new Exception($"key {vacation.Id} does not exist");
}
vacationModels[vacation.Id] = vacation;
return true;
}
public IEnumerable<VacationModel> Vacations => _vacationDatabase.GetOrAdd(_tenantRepository.TenantId, new Dictionary<Guid, VacationModel>()).Values;
}
}