SenseNet.Messaging.Subscription.Save C# (CSharp) Method

Save() public method

public Save ( ) : void
return void
        public void Save()
        {
            using (var context = new DataHandler())
            {
                var existing = context.Subscriptions.Where(x =>
                    x.ContentPath == this.ContentPath &&
                    x.UserPath == this.UserPath);

                var count = existing.Count();
                if (count == 1)
                {
                    // update
                    var item = existing.First();
                    item.Active = this.Active;
                    item.ContentPath = this.ContentPath;
                    item.Frequency = this.Frequency;
                    item.IsActive = this.IsActive;
                    item.UserEmail = this.UserEmail;
                    item.UserId = this.UserId;
                    item.UserName = this.UserName;
                    item.UserPath = this.UserPath;
                    item.Language = this.Language;
                }
                else
                {
                    if (count > 1)
                        context.Subscriptions.DeleteAllOnSubmit(existing);
                    context.Subscriptions.InsertOnSubmit(this);
                }
                context.SubmitChanges();
            }
        }

Usage Example

示例#1
0
        public static void Subscribe(User subscriber, Node target, NotificationFrequency frequency, string language, string sitePath, string siteUrl, bool isActive)
        {
            if (subscriber.Email == null)
            {
                throw new InvalidOperationException("Subscriber's email cannot be null.");
            }
            if (subscriber.Email.Length == 0)
            {
                throw new InvalidOperationException("Subscriber's email cannot be empty.");
            }

            var userName = subscriber.FullName;

            if (String.IsNullOrEmpty(userName))
            {
                userName = subscriber.Username;
            }

            var subscription = new Subscription
            {
                UserEmail   = subscriber.Email,
                UserId      = subscriber.Id,
                UserPath    = subscriber.Path,
                UserName    = userName,
                ContentPath = target.Path,
                Frequency   = frequency,
                Language    = language,
                IsActive    = true,
                SitePath    = sitePath,
                SiteUrl     = siteUrl,
            };

            subscription.Save();
        }
All Usage Examples Of SenseNet.Messaging.Subscription::Save