private static void TestRunningRandom() { var db = new VevoContext(); var svc = new RandomVideoService(db.Videos.ToList()); var user = db.Users.First(); var watched = user.WatchedVideos.ToList(); System.Console.WriteLine("Press Enter to get a new video"); while (System.Console.ReadLine() != "n") { var nextVid = svc.GetRandomVideo(user); var wv = new WatchedVideo { User = user, Video = nextVid, WatchDate = DateTime.Now }; db.WatchHistory.Add(wv); db.SaveChanges(); System.Console.WriteLine(nextVid); } }
private static void BatchInsert(IEnumerable<Video> byIsrc, VevoContext ctx) { var sb = new System.Text.StringBuilder(); sb.Append("insert into Videos (title, isrc, artistid, artist, videoyear, duration, totalviews, genre) "); foreach (var v in byIsrc) { sb.AppendFormat("SELECT '{0}','{1}','{2}','{3}',{4},'{5}',{6},'{7}' UNION ", SafeReplace(v.Title), SafeReplace(v.Isrc), SafeReplace(v.ArtistId), SafeReplace(v.Artist), v.VideoYear, v.Duration, v.TotalViews, SafeReplace(v.Genre) ); } sb.Length = sb.Length - 7; ctx.Database.ExecuteSqlCommand(sb.ToString()); }
public static void ReadFile(string fileName) { var allLines = File.ReadAllLines(fileName); var videos = new List<Video>(); foreach (var line in allLines) { var fields = line.Split('\t'); var vid = new Video(); vid.Title = fields[0]; vid.Isrc = fields[1]; vid.ArtistId = fields[2]; vid.Artist = fields[3]; if (!IsNull(fields[4])) vid.VideoYear = int.Parse(fields[4]); if (!IsNull(fields[5])) vid.Duration = TimeSpan.Parse(fields[5]); if (!IsNull(fields[6])) vid.TotalViews = long.Parse(fields[6]); if (!IsNull(fields[7])) vid.Genre = fields[7]; videos.Add(vid); } var byIsrc = videos.GroupBy(v => v.Isrc).Select(v => Combine(v)).ToArray(); var ctx = new VevoContext(); ctx.Database.ExecuteSqlCommand("Delete from WatchedVideos"); ctx.Database.ExecuteSqlCommand("Delete From Videos"); var currentSkip = 0; while (currentSkip < byIsrc.Length) { BatchInsert(byIsrc.Skip(currentSkip).Take(1000), ctx); currentSkip += 1000; } }
static NewVevoController() { var db = new VevoContext(); svc = new RandomVideoService(db.Videos.ToList()); }