Exemple #1
0
        public async Task FindTrips(RailwayMonitoring monitoring)
        {
            var monitoringResult =
                dataContext.RailwayMonitoring.Include(m => m.Trips).FirstOrDefault(m => m.Id == monitoring.Id);

            if (monitoringResult == null)
            {
                RecurringJob.RemoveIfExists(monitoring.Guid);
                return;
            }

            if (monitoringResult.DepartureDate <= DateTime.Now)
            {
                RecurringJob.RemoveIfExists(monitoring.Guid);
                monitoringResult.IsSuccessful             = false;
                monitoringResult.IsInProcess              = false;
                dataContext.Entry(monitoringResult).State = EntityState.Modified;
                await dataContext.SaveChangesAsync();

                return;
            }

            var trips = (await tripFinder.FindTripsAsync(monitoringResult.From, monitoringResult.To,
                                                         monitoringResult.DepartureDate)).ToList().ConvertAll(t => (Train)t)
                        .FindAll(t => t.Types.Any(p => p.Places >= monitoring.MinPlaces));

            if (trips.Any())
            {
                if (!string.IsNullOrEmpty(monitoring.PlacesType))
                {
                    trips = trips.FindAll(t =>
                                          t.Types.Any(p => p.Letter == monitoring.PlacesType));
                }
                if (trips.Any())
                {
                    monitoringResult.IsSuccessful             = true;
                    monitoringResult.IsInProcess              = false;
                    monitoringResult.Trips                    = trips;
                    dataContext.Entry(monitoringResult).State = EntityState.Modified;
                    await dataContext.SaveChangesAsync();

                    var user = await dataContext.Users.FindAsync(monitoringResult.UserId);

                    if (user.EmailNotificationEnabled)
                    {
                        smtpService.SendRailwayNotification(monitoringResult, user.Email);
                    }

                    if (user.SmsNotificationEnabled)
                    {
                        smsService.SendRailwayNotification(monitoringResult, user.PhoneNumber);
                    }

                    RecurringJob.RemoveIfExists(monitoring.Guid);
                }
            }
        }