Example #1
0
        public UserSubscription AddNewFeed(string feedUrl, User user)
        {
            var feedHashCode = feedUrl.GetHashCode();
            var subscription = _dbContext.Subscriptions.SingleOrDefault(x => x.Hash == feedHashCode);
            if (subscription == null)
            {
                subscription = AddSubscription(feedUrl, user);
            }
            else
            {
                var alreadySubscribed = subscription.UserSubscriptions.Any(x => x.User.Email == user.Email);
                if (alreadySubscribed)
                {
                    throw new BralekTechnicalException("ALREADY_SUBSCRIBED", null);
                }
            }
            var userSubscription = new UserSubscription
            {
                AddedOnUTC = DateTime.UtcNow,
                Subscription = subscription,
                UpdatedOnUTC = DateTime.UtcNow,
                User = user,
                UnreadItems = subscription.SubscriptionPosts.Count()
            };
            _dbContext.UserSubscriptions.Add(userSubscription);
            _dbContext.SaveChanges();

            return userSubscription;
        }
Example #2
0
        public void ImportOpml(Stream opmlFile, string userEmail)
        {
            var user = _dbContext.Users.Single(x => x.Email == userEmail);
            using (var textReader = new XmlTextReader(opmlFile))
            {
                const string rss = "rss";
                var xs = new XmlSerializer(typeof(Opml));
                var opmlData = (Opml)xs.Deserialize(textReader);

                foreach (var outline in opmlData.Body.Outline.Where(x => x.Type == rss))
                {
                    var xmlUrlHash = outline.XmlUrl.GetHashCode();
                    var subscription = _dbContext.Subscriptions.SingleOrDefault(x => x.Hash == xmlUrlHash);

                    if (subscription == null)
                    {
                        subscription = new Subscription
                                           {
                                               FeedUrl = outline.XmlUrl,
                                               Hash = xmlUrlHash,
                                               SiteUrl = outline.HtmlUrl,
                                               AddedBy = user,
                                               AddedOnUTC = DateTime.UtcNow,
                                               Title = outline.Title,
                                           };
                        _dbContext.Subscriptions.Add(subscription);
                    }

                    var userSubscription = new UserSubscription
                                               {
                                                   Subscription = subscription,
                                                   User = user,
                                                   AddedOnUTC = DateTime.UtcNow,
                                                   UpdatedOnUTC = DateTime.UtcNow
                                               };
                    _dbContext.UserSubscriptions.Add(userSubscription);
                }
                _dbContext.SaveChanges();
            }
        }