-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSessionizeService.cs
More file actions
121 lines (99 loc) · 4.15 KB
/
SessionizeService.cs
File metadata and controls
121 lines (99 loc) · 4.15 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
using Microsoft.EntityFrameworkCore;
using PocketDDD.Server.DB;
using PocketDDD.Server.Model.DBModel;
using PocketDDD.Server.Model.Sessionize;
using System.Net.Http.Json;
namespace PocketDDD.Server.Services;
public class SessionizeService
{
private readonly HttpClient httpClient;
private readonly PocketDDDContext dbContext;
public SessionizeService(HttpClient httpClient, PocketDDDContext dbContext)
{
this.httpClient = httpClient;
this.dbContext = dbContext;
httpClient.BaseAddress = new Uri("https://sessionize.com/api/v2/");
}
public async Task UpdateFromSessionize()
{
var dbEvent = await dbContext.EventDetail.SingleAsync(x => x.Id == 1);
var sessionizeEventId = dbEvent.SessionizeId;
var sessionizeEvent = await httpClient.GetFromJsonAsync<SessionizeEvent>($"{sessionizeEventId}/view/All");
if (sessionizeEvent is null)
throw new ArgumentNullException(nameof(sessionizeEvent));
var dbTracks = await dbContext.Tracks.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}";
}
await dbContext.SaveChangesAsync();
var dbTimeSlots = await dbContext.TimeSlots.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,
Info = item.isServiceSession ? item.serviceSessionDetails : null
};
dbContext.TimeSlots.Add(dbTimeSlot);
}
}
await dbContext.SaveChangesAsync();
var dbSessions = await dbContext.Sessions.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 Model.DBModel.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);
}
await dbContext.SaveChangesAsync();
}
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);
}
}