public virtual T Save(T obj) { _context.Set <T>().Add(obj); _context.SaveChanges(); return(obj); }
public void AddChatMessageToDb(List <ChatMessageJsonClass.Comment> comments, long streamId) { using (var context = new ChatDataContext()) { _logger.Info("Saving chat..."); foreach (var comment in comments) { context.Chats.Add(new Chat { messageId = comment._id, streamId = streamId, body = comment.message.body, userId = comment.commenter._id, userName = comment.commenter.name, sentAt = comment.created_at, offsetSeconds = comment.content_offset_seconds, userBadges = comment.message.userBadges, userColour = comment.message.user_color, mod = comment.message.userBadges?.Contains("moderator") ?? false, subscriber = comment.message.userBadges?.Contains("subscriber") ?? false, turbo = comment.message.userBadges?.Contains("turbo") ?? false, emotes = comment.message.formattedEmoticons }); } context.SaveChanges(); } }
private void RemoveStreamChatFromDb(long streamId) { using (var context = new ChatDataContext()) { var existingChat = context.Chats.Where(item => item.streamId == streamId); context.RemoveRange(existingChat); context.SaveChanges(); } }
public void AddLiveStreamChatToDb(List <Chat> chats, long streamId) { using (var context = new ChatDataContext()) { _logger.Info("Saving live chat..."); for (int i = 0; i < chats.Count; i++) { context.Chats.Add(chats[i]); } context.SaveChanges(); } }
// removes any in-progress live stream downloads since the last run from the database public Task RemoveLeftOverLiveStreamDownloads() { List <Stream> streams; Console.WriteLine("Checking for dead streams..."); using (var mainDataContext = new MainDataContext()) { streams = mainDataContext.Streams.Where(stream => stream.vodId != 0 && stream.downloading).ToList(); mainDataContext.RemoveRange(streams); if (streams.Count > 0) { using (var chatDataContext = new ChatDataContext()) { chatDataContext.RemoveRange(chatDataContext.Chats.Where(chat => streams.Select(stream => stream.streamId).Contains(chat.streamId))); chatDataContext.SaveChanges(); } } mainDataContext.SaveChanges(); } Console.WriteLine("Done!"); return(Task.CompletedTask); }
public DeleteStreamReturn DeleteSingleStreamLogic(long streamId) { using (var context = new MainDataContext()) { var stream = context.Streams.FirstOrDefault(item => item.streamId == streamId); if (stream == null) { stream = context.Streams.FirstOrDefault(item => item.vodId == streamId); // add live stream delete capabilities } if (stream != null) { if (stream.downloadJobId != null) { var splitJobKey = stream.downloadJobId.Split("."); try { JobHelpers.CancelJob(splitJobKey[1], splitJobKey[0], QuartzSchedulers.PrimaryScheduler(), true); } catch (MissingJobException e) { _logger.Info(e.Message); } } if (stream.vodId != 0) { try { CleanUpStreamFiles(GlobalConfig.GetGlobalConfig("contentRootPath"), stream.vodId, stream.streamerId); } catch (DirectoryNotFoundException) { CleanUpStreamFiles(GlobalConfig.GetGlobalConfig("contentRootPath"), stream.streamId, stream.streamerId); } } else { CleanUpStreamFiles(GlobalConfig.GetGlobalConfig("contentRootPath"), stream.streamId, stream.streamerId); } context.Remove(stream); if (stream.chatDownloadJobId != null) { var splitJobKey = stream.chatDownloadJobId.Split("."); try { JobHelpers.CancelJob(splitJobKey[1], splitJobKey[0], QuartzSchedulers.PrimaryScheduler(), true); } catch (MissingJobException e) { _logger.Info(e.Message); } } using (var chatContext = new ChatDataContext()) { chatContext.Chats.RemoveRange(chatContext.Chats.Where(item => item.streamId == streamId)); chatContext.SaveChanges(); } } context.SaveChanges(); } TwitchApiHelpers twitchApiHelpers = new TwitchApiHelpers(); var request = twitchApiHelpers.TwitchRequest("https://api.twitch.tv/helix/videos?id=" + streamId, Method.GET); if (request.StatusCode == HttpStatusCode.OK) { return(new DeleteStreamReturn { isStillAvailable = true }); } return(new DeleteStreamReturn { isStillAvailable = false }); }