コード例 #1
0
        private static async Task TrySendNotificationsAndUpdateSubscriptions(ILogger log)
        {
            using SqlConnection conn = ConnectionFactory.CreateConnection();
            conn.Open();
            List <Subscriber> subscribers = DbUtilities.GetAllSubscribersWithActiveSubscriptions(conn);
            List <LegoSet>    sets        = DbUtilities.GetAllSetsWithNotificationToSend(conn);

            foreach (Subscriber subscriber in subscribers)
            {
                List <Subscription> subscriptions = DbUtilities.GetSubscriptionsOfSubscriberWithId(conn, subscriber.Id);
                List <(Subscription subscription, LegoSet set)> subscriptionsWithSets =
                    subscriptions.Join(
                        sets,
                        sub => sub.SetNumber,
                        set => set.Number,
                        (sub, set) => (sub, set))
                    .ToList();

                try
                {
                    Response response = await EmailSender.PrepareAndSendEmail(subscriber.Email, subscriptionsWithSets);

                    log.LogInformation(response?.StatusCode.ToString() ?? "Email has not been sent");
                    if (response == null ||
                        response.StatusCode != HttpStatusCode.Accepted &&
                        response.StatusCode != HttpStatusCode.OK)
                    {
                        continue;
                    }

                    DbUtilities.UpdateSubscriptionsWithReportedPrices(conn, subscriptionsWithSets);
                }
                catch (Exception e)
                {
                    log.LogError(e.Message);
                    throw;
                }
            }

            DbUtilities.UpdateSetsWithNotifcationSent(conn);
        }