Esempio n. 1
0
        public async Task <ActionResult <FlightReservation> > PostFlightReservation(TOFlightReservation flightReservation, int points)
        {
            TOFlight tempFlight = new TOFlight();

            string userId   = User.Claims.First(c => c.Type == "UserID").Value;
            string username = User.Claims.First(c => c.Type == "Username").Value;

            Common.Models.AppUser inviter = null;

            using (HttpClient client = new HttpClient())
            {
                var httpRequest = new HttpRequestMessage(HttpMethod.Get,
                                                         "http://usermicroservice/api/AppUsers/GetUser/" + username);
                HttpResponseMessage message = await client.SendAsync(httpRequest);

                var temp = await message.Content.ReadAsStringAsync();

                inviter = JsonSerializer.Deserialize <Common.Models.AppUser>(temp);
            }

            Common.Models.VehicleForEmail vehicle = null;

            if (flightReservation.VehicleReservationId != 0)
            {
                using (HttpClient client = new HttpClient())
                {
                    var httpRequest = new HttpRequestMessage(HttpMethod.Get,
                                                             "http://rentacarmicroservice/api/VehicleReservations/Vehicle/" + flightReservation.VehicleReservationId);
                    HttpResponseMessage message = await client.SendAsync(httpRequest);

                    var temp = await message.Content.ReadAsStringAsync();

                    vehicle = JsonSerializer.Deserialize <Common.Models.VehicleForEmail>(temp);
                }
            }

            FlightReservation tempFlightReservation = new FlightReservation()
            {
                ReservationId        = 0,
                TimeOfCreation       = DateTime.Now,
                Creator              = ((Common.Models.AppUser)inviter).UserName,
                VehicleReservationId = flightReservation.VehicleReservationId,
                FinalPrice           = flightReservation.FinalPrice
            };

            flightReservation.FlightReservationDetails.ForEach(flightReservation => { ((Common.Models.AppUser)inviter).NumberOfPoint += (int)Math.Round(flightReservation.Flight.Distance); });
            ((Common.Models.AppUser)inviter).NumberOfPoint -= points;

            using (var client = new HttpClient())
            {
                Common.Models.PointsClass p = new Common.Models.PointsClass {
                    Id = inviter.Id, Points = inviter.NumberOfPoint
                };
                client.BaseAddress = new Uri("http://usermicroservice/");
                await client.PutAsJsonAsync("api/AppUsers/UpdatePoints", p);
            }

            _context.FlightReservations.Add(tempFlightReservation);
            await _context.SaveChangesAsync();

            foreach (TOFlightReservationDetail tOFlightReservationDetail in flightReservation.FlightReservationDetails)
            {
                tempFlight = tOFlightReservationDetail.Flight;

                FlightReservationDetail flightReservationDetail = new FlightReservationDetail()
                {
                    FlightId                  = tOFlightReservationDetail.Flight.FlightId,
                    FlightReservation         = tempFlightReservation,
                    FlightReservationDetailId = 0,
                    AirlineName               = tOFlightReservationDetail.Flight.AirlineName
                };

                _context.Entry(flightReservationDetail).State = EntityState.Added;

                await _context.SaveChangesAsync();

                foreach (TOPassengerSeat tOPassengerSeat in tOFlightReservationDetail.PassengerSeats)
                {
                    PassengerSeat passengerSeat = new PassengerSeat()
                    {
                        SeatId                  = tOPassengerSeat.Seat.SeatId,
                        Surname                 = tOPassengerSeat.Surname,
                        PassengerSeatId         = 0,
                        Name                    = tOPassengerSeat.Name,
                        Passport                = tOPassengerSeat.Passport,
                        Username                = tOPassengerSeat.Username,
                        FlightReservationDetail = flightReservationDetail,
                        AirlineScored           = false,
                        FlightScored            = false,
                    };

                    if (passengerSeat.Username == "" || passengerSeat.Username == ((Common.Models.AppUser)inviter).UserName)
                    {
                        passengerSeat.Accepted = true;
                    }
                    else
                    {
                        passengerSeat.Accepted = false;
                    }

                    _context.Entry(passengerSeat).State = EntityState.Added;

                    if (passengerSeat.Username != null && passengerSeat.Username != "" && passengerSeat.Username != ((Common.Models.AppUser)inviter).UserName)
                    {
                        Common.Models.AppUser user = null;

                        using (HttpClient client = new HttpClient())
                        {
                            var httpRequest = new HttpRequestMessage(HttpMethod.Get,
                                                                     "http://usermicroservice/api/AppUsers/GetUser/" + passengerSeat.Username);
                            HttpResponseMessage message = await client.SendAsync(httpRequest);

                            var temp = await message.Content.ReadAsStringAsync();

                            user = JsonSerializer.Deserialize <Common.Models.AppUser>(temp);
                        }

                        MailingService.SendEMailInvite(((Common.Models.AppUser)inviter), (Common.Models.AppUser)user, flightReservation, vehicle, tempFlightReservation.ReservationId);
                    }

                    Seat seat = await _context.Seats.FindAsync(passengerSeat.SeatId);

                    seat.Occupied = true;
                    _context.Entry(seat).State = EntityState.Modified;
                    await _context.SaveChangesAsync();
                }

                await _context.SaveChangesAsync();
            }

            MailingService.SendEMailReceipt(((Common.Models.AppUser)inviter), flightReservation, vehicle);

            return(Ok());
        }
        public async Task <ActionResult <FlightReservation> > PostFlightReservation(TOFlightReservation flightReservation, int points)
        {
            TOFlight tempFlight = new TOFlight();

            string userId  = User.Claims.First(c => c.Type == "UserID").Value;
            var    inviter = await _userManager.FindByIdAsync(userId);

            VehicleReservation vehicleReservation = null;
            Vehicle            vehicle            = null;

            if (flightReservation.VehicleReservationId != 0)
            {
                vehicleReservation = await _context.VehicleReservation.FindAsync(flightReservation.VehicleReservationId);

                vehicle = await _context.Vehicles.Include(vehicle => vehicle.RentACar).FirstOrDefaultAsync(vehicle => vehicle.VehicleId == vehicleReservation.VehicleId);
            }

            FlightReservation tempFlightReservation = new FlightReservation()
            {
                ReservationId        = 0,
                TimeOfCreation       = DateTime.Now,
                Creator              = inviter.UserName,
                VehicleReservationId = flightReservation.VehicleReservationId,
                FinalPrice           = flightReservation.FinalPrice
            };

            flightReservation.FlightReservationDetails.ForEach(flightReservation => { inviter.NumberOfPoint += (int)Math.Round(flightReservation.Flight.Distance); });
            inviter.NumberOfPoint -= points;

            await _userManager.UpdateAsync(inviter);

            _context.FlightReservations.Add(tempFlightReservation);
            await _context.SaveChangesAsync();

            foreach (TOFlightReservationDetail tOFlightReservationDetail in flightReservation.FlightReservationDetails)
            {
                tempFlight = tOFlightReservationDetail.Flight;

                FlightReservationDetail flightReservationDetail = new FlightReservationDetail()
                {
                    FlightId                  = tOFlightReservationDetail.Flight.FlightId,
                    FlightReservation         = tempFlightReservation,
                    FlightReservationDetailId = 0,
                    AirlineName               = tOFlightReservationDetail.Flight.AirlineName
                };

                _context.Entry(flightReservationDetail).State = EntityState.Added;

                await _context.SaveChangesAsync();

                foreach (TOPassengerSeat tOPassengerSeat in tOFlightReservationDetail.PassengerSeats)
                {
                    PassengerSeat passengerSeat = new PassengerSeat()
                    {
                        SeatId                  = tOPassengerSeat.Seat.SeatId,
                        Surname                 = tOPassengerSeat.Surname,
                        PassengerSeatId         = 0,
                        Name                    = tOPassengerSeat.Name,
                        Passport                = tOPassengerSeat.Passport,
                        Username                = tOPassengerSeat.Username,
                        FlightReservationDetail = flightReservationDetail,
                        AirlineScored           = false,
                        FlightScored            = false,
                    };

                    if (passengerSeat.Username == "" || passengerSeat.Username == inviter.UserName)
                    {
                        passengerSeat.Accepted = true;
                    }
                    else
                    {
                        passengerSeat.Accepted = false;
                    }

                    _context.Entry(passengerSeat).State = EntityState.Added;

                    if (passengerSeat.Username != null && passengerSeat.Username != "" && passengerSeat.Username != inviter.UserName)
                    {
                        var user = await _userManager.FindByNameAsync(passengerSeat.Username);

                        flightReservation.FlightReservationDetails.ForEach(flightReservation => { user.NumberOfPoint += (int)Math.Round(flightReservation.Flight.Distance); });

                        await _userManager.UpdateAsync(user);

                        MailingService.SendEMailInvite(inviter, user, flightReservation, vehicle, tempFlightReservation.ReservationId);
                    }

                    Seat seat = await _context.Seats.FindAsync(passengerSeat.SeatId);

                    seat.Occupied = true;
                    _context.Entry(seat).State = EntityState.Modified;
                    await _context.SaveChangesAsync();
                }

                await _context.SaveChangesAsync();
            }

            MailingService.SendEMailReceipt(inviter, flightReservation, vehicle);

            return(Ok());
        }