-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSessionizeService.cs
More file actions
164 lines (137 loc) · 5.8 KB
/
SessionizeService.cs
File metadata and controls
164 lines (137 loc) · 5.8 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System.Net.Http.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using PocketDDD.Server.DB;
using PocketDDD.Server.Model.DBModel;
using PocketDDD.Server.Model.Sessionize;
using Session = PocketDDD.Server.Model.DBModel.Session;
namespace PocketDDD.Server.Services;
public class SessionizeService
{
private readonly PocketDDDContext dbContext;
private readonly HttpClient httpClient;
public SessionizeService(HttpClient httpClient, PocketDDDContext dbContext, ILogger<SessionizeService> logger)
{
Logger = logger;
this.httpClient = httpClient;
this.dbContext = dbContext;
httpClient.BaseAddress = new Uri("https://sessionize.com/api/v2/");
}
private ILogger<SessionizeService> Logger { get; }
public async Task UpdateFromSessionize()
{
Logger.LogInformation("Looking for event detail in database");
var dbEvent = await dbContext.EventDetail.OrderBy(x => x.Id).LastAsync();
var sessionizeEventId = dbEvent.SessionizeId;
Logger.LogInformation("About to get data from Sessionize API");
var sessionizeEvent = await httpClient.GetFromJsonAsync<SessionizeEvent>($"{sessionizeEventId}/view/All");
if (sessionizeEvent is null)
throw new ArgumentNullException(nameof(sessionizeEvent));
Logger.LogInformation("Information retrieved from Sessionize API");
Logger.LogInformation("Looking for changes to rooms");
var dbTracks = await dbContext.Tracks.Where(track => track.EventDetail.Id == dbEvent.Id).ToListAsync();
foreach (var item in sessionizeEvent.rooms)
{
var dbTrack = dbTracks.SingleOrDefault(x => x.SessionizeId == item.id);
if (dbTrack == null)
{
dbTrack = new Track
{
SessionizeId = item.id,
EventDetail = dbEvent
};
dbContext.Tracks.Add(dbTrack);
}
dbTrack.RoomName = item.name;
dbTrack.Name = $"Track {item.sort}";
}
if (dbContext.ChangeTracker.HasChanges())
{
dbEvent.Version++;
Logger.LogInformation("Updating db with changes to rooms");
await dbContext.SaveChangesAsync();
}
else
{
Logger.LogInformation("No changes to rooms were detected");
}
Logger.LogInformation("Looking for changes to time slots and breaks");
var dbTimeSlots = await dbContext.TimeSlots.Where(timeSlot => timeSlot.EventDetail.Id == dbEvent.Id)
.ToListAsync();
var sessionizeTimeSlots = sessionizeEvent.sessions
.Select(x => (x.startsAt, x.endsAt, x.isServiceSession,
serviceSessionDetails: x.isServiceSession ? x.title : null))
.Distinct()
.ToList();
foreach (var item in sessionizeTimeSlots)
{
var dbTimeSlot = dbTimeSlots.SingleOrDefault(x => x.From == item.startsAt && x.To == item.endsAt);
if (dbTimeSlot == null)
{
dbTimeSlot = new TimeSlot
{
EventDetail = dbEvent,
From = item.startsAt,
To = item.endsAt
};
dbContext.TimeSlots.Add(dbTimeSlot);
}
dbTimeSlot.Info = item.isServiceSession ? item.serviceSessionDetails : null;
}
if (dbContext.ChangeTracker.HasChanges())
{
dbEvent.Version++;
Logger.LogInformation("Updating db with changes to time slots and breaks");
await dbContext.SaveChangesAsync();
}
else
{
Logger.LogInformation("No changes to time slots or breaks were detected");
}
Logger.LogInformation("Looking for changes to sessions");
var dbSessions = await dbContext.Sessions.Where(session => session.EventDetail.Id == dbEvent.Id).ToListAsync();
var speakers = sessionizeEvent.speakers;
dbTracks = await dbContext.Tracks.ToListAsync();
dbTimeSlots = await dbContext.TimeSlots.ToListAsync();
foreach (var item in sessionizeEvent.sessions)
{
if (item.isServiceSession) continue;
var sessionizeId = int.Parse(item.id);
var dbSession = dbSessions.SingleOrDefault(x => x.SessionizeId == sessionizeId);
if (dbSession == null)
{
dbSession = new Session
{
SessionizeId = sessionizeId,
EventDetail = dbEvent,
SpeakerToken = Guid.NewGuid(),
ShortDescription = ""
};
dbContext.Sessions.Add(dbSession);
}
dbSession.Title = item.title;
dbSession.FullDescription = item.description;
dbSession.Speaker = GetSpeakers(speakers, item.speakers);
dbSession.Track = dbTracks.Single(x => x.SessionizeId == item.roomId);
dbSession.TimeSlot = GetTimeSlot(dbTimeSlots, item.startsAt, item.endsAt);
}
if (dbContext.ChangeTracker.HasChanges())
{
dbEvent.Version++;
Logger.LogInformation("Updating db with changes to sessions");
await dbContext.SaveChangesAsync();
}
else
{
Logger.LogInformation("No changes to sessions were detected");
}
}
private string GetSpeakers(List<Speaker> speakers, List<string> speakerIds)
{
return speakerIds.Aggregate("", (acc, x) => acc + ", " + speakers.Single(s => s.id == x).fullName).Trim(',');
}
private TimeSlot GetTimeSlot(List<TimeSlot> timeSlots, DateTime startsAt, DateTime endsAt)
{
return timeSlots.Single(x => x.From == startsAt && x.To == endsAt);
}
}