Ejemplo n.º 1
0
        private DateTime AddYears(DateTime date, FormSubscribe formSubscribe)
        {
            DateTime expirationDate = date;

            if (formSubscribe.RadioButton1.Checked)
            {
                expirationDate = expirationDate.AddYears(1);
            }
            else if (formSubscribe.RadioButton2.Checked)
            {
                expirationDate = expirationDate.AddYears(2);
            }
            else
            {
                expirationDate = expirationDate.AddYears(3);
            }

            return(expirationDate);
        }
Ejemplo n.º 2
0
        private void ButtonSubscribeClick(object sender, EventArgs e)
        {
            FormSubscribe formSubscribe = new FormSubscribe();
            DialogResult  result        = formSubscribe.ShowDialog(this);

            if (result == DialogResult.Cancel)
            {
                return;
            }

            using (var context = new DataContext())
            {
                Guid authorizationUserId = new Guid(ConfigurationManager.AppSettings["AuthorizationUserId"]);

                var subscriptions = context.Subscriptions.Where(s => s.UserId == authorizationUserId).ToList();

                if (subscriptions.Count == 0)
                {
                    context.Subscriptions.Add(new Subscription
                    {
                        UserId         = authorizationUserId,
                        ExpirationDate = AddYears(DateTime.Now, formSubscribe)
                    });
                }
                else if (subscriptions.FirstOrDefault().ExpirationDate < DateTime.Now)
                {
                    var subscription = context.Subscriptions.Where(s => s.UserId == authorizationUserId).FirstOrDefault();

                    subscription.ExpirationDate = AddYears(DateTime.Now, formSubscribe);
                }
                else
                {
                    var subscription = context.Subscriptions.Where(s => s.UserId == authorizationUserId).FirstOrDefault();

                    subscription.ExpirationDate = AddYears(subscription.ExpirationDate, formSubscribe);
                }
                context.SaveChanges();
                LoadForm();
            }
        }