public async Task <List <Notification> > AddNotifications(List <Order> orders) { // Start watch var stopwatch = new Stopwatch(); stopwatch.Start(); // Now var now = DateTime.UtcNow.StripSeconds(); // Create notifications var notifications = new List <Notification>(); // Orders pending to notify var ordersPendingToNotify = orders.Where(OrderExpression.PendingToNotify()).ToList(); // For each order pending to notify foreach (var order in ordersPendingToNotify) { // Get user var user = await _mainDbContext.Users.FindAsync(order.UserId); // Create message var message = NotificationBuilder.BuildMessage( OrderMessage.OrderNotification, order.CurrencyId, order.OrderType, order.Price); // Create notification var notification = new Notification(user.UserId, user.PhoneNumber, message, now); // Add notification notifications.Add(notification); // Mark order as notified order.MarkAsNotified(); // Update order _mainDbContext.Orders.Update(order); } // Add notifications await _mainDbContext.Notifications.AddRangeAsync(notifications); // Save await _mainDbContext.SaveChangesAsync(); // Stop watch stopwatch.Stop(); // Log _logger.LogInformation("{@Event}, {@Count}, {@ExecutionTime}", "NotificationsAdded", notifications.Count, stopwatch.Elapsed.TotalSeconds); // Return return(notifications); }