private string Unsubscribe(User user, Show show) { string response; using (AppDbContext db = new AppDbContext()) { Subscription subscription; Program.Logger.Debug($"{GetType().Name}: Checking for subscription existence"); try { subscription = db.Subscriptions.FirstOrDefault(s => s.User.Id == user.Id && s.Show.Id == show.Id); } catch (Exception e) { throw new Exception($"{GetType().Name}: An error occurred while checking for subscription existence", e); } if (subscription != null) { Program.Logger.Debug($"{GetType().Name}: Deleting notifications for subscription"); try { db.Notifications.RemoveRange(db.Notifications.Where(n => n.Subscription.Id == subscription.Id)); } catch (Exception e) { throw new Exception($"{GetType().Name}: An error occurred while deleting notifications for subscription", e); } Program.Logger.Debug($"{GetType().Name}: Deleting subscription"); try { db.Subscriptions.Remove(subscription); } catch (Exception e) { throw new Exception($"{GetType().Name}: An error occurred while deleting subscription", e); } try { db.SaveChanges(); } catch (Exception e) { throw new Exception($"{GetType().Name}: An error occurred while saving changes to database", e); } response = $"Вы отписались от сериала \"{show.Title}\""; } else { response = $"Вы, братишка, не подписаны на сериал \"{show.Title}\""; } } return response; }
private string LoadShowDescription(Show show) { string u = $"{Url}{ShowPageUrl}{show.SiteId}"; var doc = DownloadDocument(u).Result; string descriptionText = doc.DocumentNode.SelectNodes("//div[@id='MainDiv']//div[@id='Onwrapper']//div[@class='mid']//div//h1").First().ParentNode.InnerText.Trim(); if (string.IsNullOrEmpty(descriptionText)) { return string.Empty; } var descriptionParts = descriptionText.Replace("\r", string.Empty) .Replace("\t", string.Empty) .Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries) .ToList(); return WebUtility.HtmlDecode(descriptionParts.GetRange(1, descriptionParts.Count - 1).Aggregate(string.Empty, (s, s1) => s + s1 + "\n")); }
private static void Subscribe(AppDbContext db, User user, Show show, bool newUser) { Subscription subscription = new Subscription { User = user, Show = show, SubscriptionDate = DateTimeOffset.Now }; if (newUser) { user.Subscriptions.Add(subscription); db.Users.Add(user); } else { db.Subscriptions.Add(subscription); } }