forked from episerver/content-graph-js-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProvisionDatabase.cs
60 lines (47 loc) · 1.56 KB
/
ProvisionDatabase.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
using EPiServer.Web;
namespace MusicFestival.Backend;
public class ProvisionDatabase : IHostedService
{
private readonly ILogger<ProvisionDatabase> _logger;
private readonly ISiteDefinitionRepository _siteDefinitionRepository;
public ProvisionDatabase(
ILogger<ProvisionDatabase> logger,
ISiteDefinitionRepository siteDefinitionRepository)
{
_logger = logger;
_siteDefinitionRepository = siteDefinitionRepository;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
await AddPrimarySiteHost();
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
private Task AddPrimarySiteHost()
{
_logger.LogInformation("Provisioning primary site host.");
var site = _siteDefinitionRepository
.List()
.FirstOrDefault();
if (site is null)
{
_logger.LogInformation("Primary site host already exists.");
return Task.CompletedTask;
}
else
{
site = site.CreateWritableClone();
}
if (!site.Hosts.Any(x => x.Type == HostDefinitionType.Primary))
{
var editHost = site.Hosts.First(x => x.Name != "*");
editHost.Type = HostDefinitionType.Edit;
site.Hosts.Add(new HostDefinition
{
Type = HostDefinitionType.Primary,
Name = "localhost:3000"
});
}
_siteDefinitionRepository.Save(site);
return Task.CompletedTask;
}
}