public async Task <IActionResult> Create(RequestReservationCreateViewModel createModel) { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); User user = await UserManager.GetUserAsync(User); List <Client> clients = new List <Client>(); foreach (var clientId in createModel.ClientsId) { Client client = new Client(); client = _context.Clients.FirstOrDefault(x => x.Id == clientId); clients.Add(client); } Room room = _context.Rooms.FirstOrDefault(x => x.Id == createModel.RoomId); room.IsFree = false; if (ModelState.IsValid) { Reservation reservation = new Reservation { UserId = user.Id, CheckInDate = createModel.CheckInDate, CheckOutDate = createModel.CheckOutDate, HasBreakfast = createModel.HasBreakfast, IsAllInclusive = createModel.IsAllInclusive, Room = room, FinalPrice = createModel.FinalPrice, ClientReservations = clients.Select(client => new ClientReservation { Client = client }).ToList() }; _context.Add(reservation); await _context.SaveChangesAsync(); SendMessage(clients); return(RedirectToAction(nameof(Index))); } return(View(createModel)); }
public async Task <IActionResult> Create(ClientCreateViewModel createModel) { if (ModelState.IsValid) { Client client = new Client { Name = createModel.Name, Surname = createModel.Surname, Email = createModel.Email, PhoneNumber = createModel.PhoneNumber, IsAdult = createModel.IsAdult }; _context.Add(client); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(createModel)); }
public async Task <IActionResult> Create(RoomsCreateViewModel createModel) { if (ModelState.IsValid) { Room room = new Room { Capacity = createModel.Capacity, PricePerAdult = createModel.PricePerAdult, PricePerChild = createModel.PricePerChild, RoomType = createModel.RoomType, IsFree = createModel.IsFree, Number = createModel.Number }; _context.Add(room); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(createModel)); }
public async Task <IActionResult> Create(RequestReservationCreateViewModel createModel) { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); User user = await UserManager.GetUserAsync(User); List <Client> clients = new List <Client>(); foreach (var clientId in createModel.ClientsId) { Client client = new Client(); //client = (Client)_context.Clients.Where(x => x.Id == clientId); client = _context.Clients.FirstOrDefault(x => x.Id == clientId); clients.Add(client); } Room room = _context.Rooms.FirstOrDefault(x => x.Id == createModel.RoomId); room.IsFree = false; if (ModelState.IsValid) { Reservation reservation = new Reservation { //User = createModel.User, UserId = user.Id, //User = user, CheckInDate = createModel.CheckInDate, CheckOutDate = createModel.CheckOutDate, HasBreakfast = createModel.HasBreakfast, IsAllInclusive = createModel.IsAllInclusive, Room = room, FinalPrice = createModel.FinalPrice, ClientReservations = clients.Select(client => new ClientReservation { Client = client }).ToList() }; _context.Add(reservation); await _context.SaveChangesAsync(); foreach (Client client in clients) { var message = new MimeMessage(); message.From.Add(new MailboxAddress("ReservationManager", "*****@*****.**")); message.To.Add(new MailboxAddress($"{client.Email}")); message.Subject = "Rreservation"; message.Body = new TextPart("Successful reservation!"); using (var emailClient = new SmtpClient()) { emailClient.Connect("smtp.gmail.com", 587, false); emailClient.Authenticate("*****@*****.**", password); emailClient.Send(message); emailClient.Disconnect(true); } } return(RedirectToAction(nameof(Index))); } return(View(createModel)); }