public async Task <IActionResult> AddQuick(QuickReservationDto newReservation) { if (!ModelState.IsValid) { return(BadRequest()); } return(Ok(await _reservationService.AddReservationQuick(newReservation))); }
public async Task <ServiceResponse <Reservation> > AddReservationQuick(QuickReservationDto newReservation) //UserID, DepartingFlight { ServiceResponse <Reservation> serviceResponse = new ServiceResponse <Reservation>(); try { User user = await _context.Users.Include(u => u.Reservations).FirstOrDefaultAsync(u => u.Id == GetUserId()); newReservation.User = user; Flight depFlight = await _context.Flights.AsNoTracking().Include(f => f.Seats).FirstOrDefaultAsync(f => f.Id == newReservation.DepartingFlight.Id); var depSeats = depFlight.Seats.Where(s => s.State == 0); //ovde su mi sad slobodna mesta Random random = new Random(); List <int> seatIds = new List <int>(); foreach (Seat seat in depSeats) { seatIds.Add(seat.Id); } int randomIndex = random.Next(0, seatIds.Count); var randomSeatId = seatIds[randomIndex]; // OVO JE NOVO Seat departingSeat = await _context.Seats.AsNoTracking().FirstOrDefaultAsync(s => s.Id == randomSeatId); newReservation.DepartingFlightSeat = departingSeat; Reservation reservation = _mapper.Map <Reservation>(newReservation); user.Reservations.Add(reservation); _context.Users.Update(user); await _context.SaveChangesAsync(); Reservation forResponse = _mapper.Map <Reservation>(newReservation); serviceResponse.Data = forResponse; #region email //poslati imejl string pattern = @"Reservation details.. Flight #1 : Origin: {0} | Destination: {1} | Takeoff time: {2} | Landing time: {3} | Duration: {4}. "; string emailData = string.Format(pattern, newReservation.DepartingFlight.Origin, newReservation.DepartingFlight.Destination, newReservation.DepartingFlight.TakeoffTime, newReservation.DepartingFlight.LandingTime, newReservation.DepartingFlight.Duration); var message = new MimeMessage(); message.From.Add(new MailboxAddress("Booking details", "*****@*****.**")); message.To.Add(new MailboxAddress("Luka", "*****@*****.**")); message.Subject = "Booking details"; message.Body = new TextPart("plain") { Text = emailData }; using (var client = new MailKit.Net.Smtp.SmtpClient()) { client.Connect("smtp.gmail.com", 587, false); //SMTP server authentication if needed client.Authenticate("rluka996", "kostadin"); client.Send(message); client.Disconnect(true); } #endregion email } catch (Exception ex) { serviceResponse.Message = ex.Message; serviceResponse.Success = false; } return(serviceResponse); }