示例#1
0
        public override void Execute()
        {
            using (AppDbContext db = new AppDbContext())
            {
                Program.Logger.Debug($"{GetType().Name}: Searching user with TelegramId: {Message.From.Id} in database");
                var user = db.GetUserByTelegramId(Message.From.Id);
                if (user == null)
                {
                    user = new User
                    {
                        TelegramUserId = Message.From.Id,
                        FirstName = Message.From.FirstName,
                        LastName = Message.From.LastName,
                        Username = Message.From.Username
                    };

                    Program.Logger.Info($"{GetType().Name}: {user} is new User");
                    Program.Logger.Debug($"{GetType().Name}: Adding user {user} to database");
                    db.Users.Add(user);
                }
                else
                {
                    Program.Logger.Debug($"{GetType().Name}: User {user} is already exist");
                }

                Program.Logger.Debug($"{GetType().Name}: Subscribing {user} to all shows");
                var subscriptions = user.Subscriptions.ToList();
                foreach (Show show in db.Shows)
                {
                    Subscription subscription = new Subscription
                    {
                        User = user,
                        Show = show,
                        SubscriptionDate = DateTime.Now
                    };

                    if (subscriptions.Any(s => Equals(s.Show, show)))
                    {
                        continue;
                    }

                    db.Subscriptions.Add(subscription);
                }

                Program.Logger.Debug($"{GetType().Name}: Saving changes to database");
                db.SaveChanges();
            }

            Program.Logger.Debug($"{GetType().Name}: Sending response to {Message.From}");
            try
            {
                TelegramApi.SendMessage(Message.From, "Вы, братишка, подписаны на все сериалы");
            }
            catch (Exception e)
            {
                throw new Exception($"{GetType().Name}: An error occurred while sending response to {Message.From}", e);
            }

            Status = true;
        }
示例#2
0
        public override void Execute()
        {
            Show show;
            string showTitle = string.Empty;
            bool subscribeById = ShowId != null;
            if (ShowId == null)
            {
                int messageSize;
                int.TryParse(Arguments, out messageSize);
                if (messageSize == 0)
                {
                    messageSize = MaxPageSize;
                }
                messageSize = Math.Min(messageSize, MaxPageSize);

                ShowId = RequestShow(out showTitle);
                if (ShowId == null)
                {
                    SendShowList(showTitle, messageSize);
                    Status = true;
                    return;
                }
            }

            string response;
            Program.Logger.Info($"{GetType().Name}: {Message.From} is trying to subscribe to '{(subscribeById ? $"Id = {ShowId}" : showTitle)}'");
            using (AppDbContext db = new AppDbContext())
            {
                show = db.GetShowById(ShowId.Value);
                if (show == null)
                {
                    Program.Logger.Info($"{GetType().Name}: Show '{(subscribeById ? $"Id = {ShowId}" : showTitle)}' was not found");
                    response = $"Сериал '{(subscribeById ? $"Id = {ShowId}" : showTitle)}' не найден";
                    Program.Logger.Debug($"{GetType().Name}: Sending response to {Message.From}");
                    try
                    {
                        TelegramApi.SendMessage(Message.From, response);
                    }
                    catch (Exception e)
                    {
                        throw new Exception($"{GetType().Name}: An error occurred while sending response to {Message.From}", e);
                    }
                    Status = true;
                    return;
                }

                Program.Logger.Debug($"{GetType().Name}: Searching user with TelegramId: {Message.From.Id} in database");
                User user;
                try
                {
                    user = db.GetUserByTelegramId(Message.From.Id);
                }
                catch (Exception e)
                {
                    throw new Exception($"{GetType().Name}: An error occurred while searching user in database", e);
                }
                bool newUser = false;
                if (user == null)
                {
                    user = new User
                    {
                        TelegramUserId = Message.From.Id,
                        FirstName = Message.From.FirstName,
                        LastName = Message.From.LastName,
                        Username = Message.From.Username
                    };
                    newUser = true;
                }

                if (newUser)
                {
                    Program.Logger.Info($"{GetType().Name}: {user} is new User");
                }
                else
                {
                    Program.Logger.Debug($"{GetType().Name}: User {user} is already exist");
                }

                bool subscriptionExists;
                Program.Logger.Debug($"{GetType().Name}: Checking for subscription existence");
                try
                {
                    subscriptionExists = user.Subscriptions.Any(s => s.Show.Id == show.Id);
                }
                catch (Exception e)
                {
                    throw new Exception($"{GetType().Name}: An error occurred while checking for subscription existence", e);
                }
                if (subscriptionExists)
                {
                    Program.Logger.Info($"{GetType().Name}: User {Message.From} is already subscribed to {show.OriginalTitle}");
                    response = $"Вы уже подписаны на сериал '{show.Title}'";
                }
                else
                {
                    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);
                    }
                    response = $"Вы подписались на сериал '{show.Title}'";
                }

                Program.Logger.Debug($"{GetType().Name}: Saving changes to database");
                try
                {
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                    throw new Exception($"{GetType().Name}: An error occurred while saving changes to database", e);
                }
            }

            Program.Logger.Debug($"{GetType().Name}: Sending response to {Message.From}");
            try
            {
                TelegramApi.SendMessage(Message.From, response);
            }
            catch (Exception e)
            {
                throw new Exception($"{GetType().Name}: An error occurred while sending response to {Message.From}", e);
            }

            Status = true;
        }
示例#3
0
 /// <summary>
 /// Deprecated Method for adding a new object to the Subscriptions EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToSubscriptions(Subscription subscription)
 {
     base.AddObject("Subscriptions", subscription);
 }
示例#4
0
 /// <summary>
 /// Create a new Subscription object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 /// <param name="name">Initial value of the Name property.</param>
 /// <param name="lastName">Initial value of the LastName property.</param>
 /// <param name="active">Initial value of the Active property.</param>
 /// <param name="insertedDate">Initial value of the InsertedDate property.</param>
 public static Subscription CreateSubscription(global::System.Int32 id, global::System.String name, global::System.String lastName, global::System.Boolean active, global::System.String insertedDate)
 {
     Subscription subscription = new Subscription();
     subscription.Id = id;
     subscription.Name = name;
     subscription.LastName = lastName;
     subscription.Active = active;
     subscription.InsertedDate = insertedDate;
     return subscription;
 }
示例#5
0
        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);
            }
        }