Ejemplo n.º 1
0
        public async Task <IActionResult> MoveToNextFlight(int id)
        {
            var currentFlight = await ctx.Flights.FindAsync(id);

            Flight nextFlight = null;

            // Check if there is a flight with the same dep and dest + a later departure date
            foreach (var flight in ctx.Flights)
            {
                if (flight.Departure == currentFlight.Departure &&
                    flight.Arrival == currentFlight.Arrival &&
                    flight.DepartureTime > currentFlight.DepartureTime)
                {
                    nextFlight = flight;
                    break;
                }
            }

            if (nextFlight != null)
            {
                // check if the next flight is already overbooked
                if (nextFlight.FreeSeats < 0 || nextFlight.FreeSeats < ctx.GetOverbookedUsersFromFlight(currentFlight).Count)
                {
                    return(View("/Views/Errors/NextFlightOverbookedError.cshtml"));
                }
                else
                { // if not, move the overbooked users there
                    List <string> overbookedEmails = new List <string>();

                    // list of overbooked users from this flight
                    var overbookedUsers = ctx.GetOverbookedUsersFromFlight(currentFlight);

                    // Removes all overbooked users and replaces them to the next available flight
                    for (int i = 0; i < overbookedUsers.Count; i++)
                    {
                        overbookedEmails.Add(overbookedUsers.ElementAt(i).Email);
                        ctx.UserFlights.Add(new UserFlights {
                            User = ctx.GetUserFromEmail(overbookedUsers.ElementAt(i).Email), Flight = nextFlight
                        });
                        --nextFlight.FreeSeats;


                        ctx.UserFlights.Remove(ctx.GetSpecificUserFlight(currentFlight, ctx.GetUserFromEmail(overbookedUsers.ElementAt(i).Email)));
                        ctx.OverbookedUsers.Remove(overbookedUsers.ElementAt(i));

                        ++currentFlight.FreeSeats;
                        await ctx.SaveChangesAsync();
                    }



                    return(RedirectToAction("SendEmail", "Email", new { emails = overbookedEmails }));
                }
            }
            else
            {
                return(View("/Views/Errors/NoNextFlightError.cshtml"));
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> SendEmail(List <string> emails)
        {
            MimeMessage msg = new MimeMessage();

            msg.Subject = "Flight Postponing";
            msg.From.Add(new MailboxAddress("Admin", "*****@*****.**"));

            BodyBuilder bodyBuilder = new BodyBuilder();

            foreach (var email in emails)
            {
                using (var emailClient = new SmtpClient())
                {
                    await emailClient.ConnectAsync("smtp-mail.outlook.com", 587, false);

                    await emailClient.AuthenticateAsync("*****@*****.**", "Iamadmin~");

                    var    currentUser = ctx.GetUserFromEmail(email);
                    string FirstName   = currentUser.FirstName;
                    string LastName    = currentUser.LastName;

                    MailboxAddress to = new MailboxAddress(FirstName, email);

                    bodyBuilder.TextBody = "Dear " + FirstName + " " + LastName +
                                           ",\nDue to overbooked seats, we have decided to postpone your current flight.\nSincerely,\n\nThe Administrator";

                    msg.To.Add(to);
                    msg.Body = bodyBuilder.ToMessageBody();



                    emailClient.Send(msg);



                    emailClient.Disconnect(true);
                    emailClient.Dispose();
                }
            }



            return(View());;
        }