forked from dotnet-presentations/aspnetcore-app-workshop
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathSessionizeLoader.cs
More file actions
122 lines (107 loc) · 4.21 KB
/
SessionizeLoader.cs
File metadata and controls
122 lines (107 loc) · 4.21 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using BackEnd.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace BackEnd
{
public class SessionizeLoader : DataLoader
{
public override async Task LoadDataAsync(Stream fileStream, ApplicationDbContext db)
{
// var blah = new RootObject().rooms[0].sessions[0].speakers[0].name;
var addedSpeakers = new Dictionary<string, Speaker>();
var addedTracks = new Dictionary<string, Track>();
var array = await JToken.LoadAsync(new JsonTextReader(new StreamReader(fileStream)));
var root = array.ToObject<List<RootObject>>();
foreach (var date in root)
{
foreach (var room in date.rooms)
{
if (!addedTracks.ContainsKey(room.name))
{
var thisTrack = new Track { Name = room.name };
db.Tracks.Add(thisTrack);
addedTracks.Add(thisTrack.Name, thisTrack);
}
foreach (var thisSession in room.sessions)
{
foreach (var speaker in thisSession.speakers)
{
if (!addedSpeakers.ContainsKey(speaker.name))
{
var thisSpeaker = new Speaker { Name = speaker.name };
db.Speakers.Add(thisSpeaker);
addedSpeakers.Add(thisSpeaker.Name, thisSpeaker);
}
}
var session = new Session
{
Title = thisSession.title,
StartTime = thisSession.startsAt,
EndTime = thisSession.endsAt,
Track = addedTracks[room.name],
Abstract = thisSession.description
};
session.SessionSpeakers = new List<SessionSpeaker>();
foreach (var sp in thisSession.speakers)
{
session.SessionSpeakers.Add(new SessionSpeaker
{
Session = session,
Speaker = addedSpeakers[sp.name]
});
}
db.Sessions.Add(session);
}
}
}
}
private class RootObject
{
public DateTime date { get; set; }
public List<Room> rooms { get; set; }
public List<TimeSlot> timeSlots { get; set; }
}
private class ImportSpeaker
{
public string id { get; set; }
public string name { get; set; }
}
private class Category
{
public int id { get; set; }
public string name { get; set; }
public List<object> categoryItems { get; set; }
public int sort { get; set; }
}
private class ImportSession
{
public int id { get; set; }
public string title { get; set; }
public string description { get; set; }
public DateTime startsAt { get; set; }
public DateTime endsAt { get; set; }
public bool isServiceSession { get; set; }
public bool isPlenumSession { get; set; }
public List<ImportSpeaker> speakers { get; set; }
public List<Category> categories { get; set; }
public int roomId { get; set; }
public string room { get; set; }
}
private class Room
{
public int id { get; set; }
public string name { get; set; }
public List<ImportSession> sessions { get; set; }
public bool hasOnlyPlenumSessions { get; set; }
}
private class TimeSlot
{
public string slotStart { get; set; }
public List<Room> rooms { get; set; }
}
}
}