示例#1
0
        public override async Task LoadDataAsync(string conferenceName, Stream fileStream, ApplicationDbContext db)
        {
            var reader = new JsonTextReader(new StreamReader(fileStream));

            var conference = new Conference {
                Name = conferenceName
            };

            var speakerNames = new Dictionary <string, Speaker>();
            var tracks       = new Dictionary <string, Track>();

            JArray doc = await JArray.LoadAsync(reader);

            foreach (JObject item in doc)
            {
                var theseSpeakers = new List <Speaker>();
                foreach (var thisSpeakerName in item["speakerNames"])
                {
                    if (!speakerNames.ContainsKey(thisSpeakerName.Value <string>()))
                    {
                        var thisSpeaker = new Speaker {
                            Name = thisSpeakerName.Value <string>()
                        };
                        db.Speakers.Add(thisSpeaker);
                        speakerNames.Add(thisSpeakerName.Value <string>(), thisSpeaker);
                        Console.WriteLine(thisSpeakerName.Value <string>());
                    }
                    theseSpeakers.Add(speakerNames[thisSpeakerName.Value <string>()]);
                }

                var theseTracks = new List <Track>();
                foreach (var thisTrackName in item["trackNames"])
                {
                    if (!tracks.ContainsKey(thisTrackName.Value <string>()))
                    {
                        var thisTrack = new Track {
                            Name = thisTrackName.Value <string>(), Conference = conference
                        };
                        db.Tracks.Add(thisTrack);
                        tracks.Add(thisTrackName.Value <string>(), thisTrack);
                    }
                    theseTracks.Add(tracks[thisTrackName.Value <string>()]);
                }

                var session = new Session
                {
                    Conference = conference,
                    Title      = item["title"].Value <string>(),
                    StartTime  = item["startTime"].Value <DateTime>(),
                    EndTime    = item["endTime"].Value <DateTime>(),
                    Track      = theseTracks[0],
                    Abstract   = item["abstract"].Value <string>()
                };

                session.SessionSpeakers = new List <SessionSpeaker>();
                foreach (var sp in theseSpeakers)
                {
                    session.SessionSpeakers.Add(new SessionSpeaker
                    {
                        Session = session,
                        Speaker = sp
                    });
                }

                db.Sessions.Add(session);
            }
        }
        public override async Task <Conference> LoadDataAsync(string conferenceName, Stream fileStream)
        {
            //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.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
                                };
                                addedSpeakers.Add(thisSpeaker.Name, thisSpeaker);
                            }
                        }

                        foreach (var category in thisSession.categories)
                        {
                            if (!addedTags.ContainsKey(category.name))
                            {
                                var thisTag = new Tag {
                                    Name = category.name
                                };
                                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.SessionTags = thisSession.categories.Select(c => new SessionTag
                        {
                            Session = session,
                            Tag     = addedTags[c.name]
                        }).ToList();

                        session.SessionSpeakers = thisSession.speakers.Select(s => new SessionSpeaker
                        {
                            Session = session,
                            Speaker = addedSpeakers[s.name]
                        }).ToList();

                        conference.Sessions.Add(session);
                    }
                }
            }

            conference.StartTime = conference.Sessions.Min(s => s.StartTime);
            conference.EndTime   = conference.Sessions.Max(s => s.EndTime);

            return(conference);
        }