BrightstarDB.Models.EventFeed.EventFeedService.RegisterInterest C# (CSharp) Method

RegisterInterest() public method

public RegisterInterest ( string userName, string topicId ) : void
userName string
topicId string
return void
        public void RegisterInterest(string userName, string topicId)
        {
            var ctx = new EventFeedContext(_connectionString);
            var topic = ctx.Topics.Where(t => t.Id.Equals(topicId)).ToList().FirstOrDefault();
            var subscriber = ctx.Subscribers.Where(s => s.UserName.Equals(userName)).ToList().FirstOrDefault();

            if (topic == null) { throw new Exception("No topic with id");}
            if (subscriber == null) throw new Exception("No subscriber with name provided");

            subscriber.Topics.Add(topic);
            ctx.SaveChanges();
        }

Usage Example

        public void TestRemoveInterest()
        {
            var storeId = Guid.NewGuid().ToString();
            var eventService = new EventFeedService(storeId);

            // create a topic first that can be subscribed to, this topic could have been 'found' by search or be
            // a well known topic.
            eventService.AssertTopic(new Uri("http://www.brightstardb.com/topics/34"), "Topic 34", "A very important topic");
            eventService.AssertTopic(new Uri("http://www.brightstardb.com/topics/33"), "Topic 33", "A very important topic");

            eventService.AssertSubscriber("domain\\bob", new List<Uri>() { new Uri("http://www.brightstardb.com/topics/34") });
            eventService.RegisterInterest("domain\\bob", "http://www.brightstardb.com/topics/33");

            eventService.RaiseEvent("Bob created document Y", DateTime.UtcNow, new List<string>() { "http://www.brightstardb.com/topics/33" });

            var events = eventService.GetSubscriberTimeline("domain\\bob", DateTime.MinValue);
            Assert.AreEqual(1, events.Count());

            // remove interest
            eventService.RemoveInterest("domain\\bob", "http://www.brightstardb.com/topics/33");
            eventService.RaiseEvent("Bob created document Y", DateTime.UtcNow, new List<string>() { "http://www.brightstardb.com/topics/33" });
            events = eventService.GetSubscriberTimeline("domain\\bob", DateTime.MinValue);
            Assert.AreEqual(1, events.Count());
        }