forked from dotnet-presentations/aspnetcore-app-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSessionizeLoader.cs
135 lines (119 loc) · 4.87 KB
/
SessionizeLoader.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
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
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(string conferenceName, 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 addedTags = new Dictionary<string, Tag>();
var array = await JToken.LoadAsync(new JsonTextReader(new StreamReader(fileStream)));
var conference = new Conference { Name = conferenceName };
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, Conference = conference };
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);
}
}
foreach (var category in thisSession.categories)
{
if (!addedTags.ContainsKey(category.name))
{
var thisTag = new Tag { Name = category.name };
db.Tags.Add(thisTag);
addedTags.Add(thisTag.Name, thisTag);
}
}
var session = new Session
{
Conference = conference,
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; }
}
}
}