public async Task <IActionResult> AddReservation([FromBody] AddReservationViewModel newReservation) { if (!ModelState.IsValid) { var modelErrors = new List <string>(); foreach (var modelState in ModelState.Values) { foreach (var modelError in modelState.Errors) { modelErrors.Add(modelError.ErrorMessage); } } return(BadRequest(new AddingResult { Successful = false, Errors = modelErrors })); } string userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; var user = await _userManager.FindByIdAsync(userId); var feedback = await _reservationsService .AddServiceReservationAsync(newReservation, userId); if (feedback.Successful == false) { return(BadRequest("Could not add service because: " + feedback.Message)); } else { return(Ok(feedback.Message)); } return(BadRequest("Could not add service because: " + feedback.Message)); }
public async Task GetAllReservations_ShouldReturnCorrectCount() { // Arrange MapperInitializer.InitializeMapper(); var context = HotelDbContextInMemoryFactory.InitializeContext(); var reservationRepository = new EfDeletableEntityRepository <Reservation>(context); var reservationsService = this.GetReservationService(reservationRepository, context); var seeder = new ReservationsServiceTestsSeeder(); await seeder.SeedReservationAsync(context); // Act var actualResult = reservationsService.GetAllReservations <AddReservationViewModel>().ToList(); var expectedResult = new AddReservationViewModel[] { new AddReservationViewModel { StartDate = new DateTime(2020, 4, 4), EndDate = new DateTime(2020, 4, 8), Adults = 2, Kids = 1, ReservationStatusId = context.ReservationStatuses.First().Id, PaymentTypeId = context.PaymentTypes.First().Id, }, }; Assert.Equal(expectedResult.Length, actualResult.Count()); }
public async Task GetAllReservations_ShouldReturnCorrectResult() { var errorMessagePrefix = "ReservationsService GetAllReservations() method does not work properly."; // Arrange MapperInitializer.InitializeMapper(); var context = HotelDbContextInMemoryFactory.InitializeContext(); var reservationRepository = new EfDeletableEntityRepository <Reservation>(context); var reservationsService = this.GetReservationService(reservationRepository, context); var seeder = new ReservationsServiceTestsSeeder(); await seeder.SeedReservationAsync(context); // Act var actualResult = reservationsService.GetAllReservations <AddReservationViewModel>().ToList(); var expectedResult = new AddReservationViewModel[] { new AddReservationViewModel { StartDate = new DateTime(2020, 4, 4), EndDate = new DateTime(2020, 4, 8), Adults = 2, Kids = 1, ReservationStatusId = context.ReservationStatuses.First().Id, PaymentTypeId = context.PaymentTypes.First().Id, }, }; Assert.True(expectedResult[0].StartDate == actualResult[0].StartDate, errorMessagePrefix + " " + "Start date is not returned properly."); Assert.True(expectedResult[0].EndDate == actualResult[0].EndDate, errorMessagePrefix + " " + "End date is not returned properly."); Assert.True(expectedResult[0].Adults == actualResult[0].Adults, errorMessagePrefix + " " + "Adults is not returned properly."); Assert.True(expectedResult[0].Kids == actualResult[0].Kids, errorMessagePrefix + " " + "Kids is not returned properly."); Assert.True(expectedResult[0].ReservationStatusId == actualResult[0].ReservationStatusId, errorMessagePrefix + " " + "Reservation status is not returned properly."); Assert.True(expectedResult[0].PaymentTypeId == actualResult[0].PaymentTypeId, errorMessagePrefix + " " + "Payment type id is not returned properly."); }
/// <summary> /// Creates a new reservation in the database. /// </summary> /// <param name="model"> Information about the reservation. </param> /// <param name="id"> Holds all the needed ids. </param> /// <returns> It is a void method. </returns> public async Task CreateAsync(AddReservationViewModel model, string id) { this.addReservationViewModel = model; this.hotelId = id.Split()[0]; var guest = await this.IsTheGuestRegisteredBeforeAsync() ? await this.GetUserProfileAsync() : await this.CreateNewUserProfileAsync(); await this.AddGuestToRoleAsync(guest); await this.CreateReservationAsync(guest); }
public ActionResult Create(AddReservationViewModel reservation) { if (reservation != null && ModelState.IsValid) { var dbReservation = Mapper.Map <Reservation>(reservation); this.Data.Reservations.Add(dbReservation); this.Data.SaveChanges(); return(this.RedirectToAction("MyReservations", "Reservations")); } return(View(reservation)); }
public ActionResult Create() { var pets = this.Data.Pets .All() .Where(p => p.OwnerId == this.UserProfile.Id) .Select(p => new SelectListItem { Value = p.Id.ToString(), Text = p.Name }) .ToList(); var model = new AddReservationViewModel { Pets = pets }; return(View(model)); }
public async Task <IActionResult> Post([FromBody] AddReservationViewModel model) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var statusCode = await ValidateEntities(model); if (statusCode == HttpStatusCode.NotFound) { return(NotFound(_message)); } else if (statusCode == HttpStatusCode.BadRequest) { return(BadRequest(_message)); } model.ReservationDate = DateTime.Now.Date; var newReservationEntity = _mapper.Map <AddReservationViewModel, Reservation>(model); _reservationRepository.AddEntity(newReservationEntity); await _hotelRepository.DecreaseAvailableRoomQuantity((int)model.HotelId); if (await _reservationRepository.SaveAsync()) { model.Id = newReservationEntity.Id; var newUri = Url.Link("GetReservation", new { id = model.Id }); return(Created(newUri, model)); } else { _message = "Nie udało się dodać nowej rezerwacji"; return(BadRequest(_message)); } } catch (Exception e) { return(BadRequest(e.Message)); } }
public void AddReservationTest() { ReservationManager rm = new ReservationManager(new UnitOfWork(new ReservationContextTest())); AddReservationViewModel mockViewModel = new AddReservationViewModel(rm); Client client = new Client(999, "Alice", "Cards", ClientCategory.Vip, new Address()); rm.AddClient(client); Location start = Location.Antwerpen; Location stop = Location.Brussel; List <Price> prices = new List <Price>(); prices.Add(new Price(Arrangement.Airport, 100m)); prices.Add(new Price(Arrangement.Business, 100m)); prices.Add(new Price(Arrangement.NightLife, 900m)); prices.Add(new Price(Arrangement.Wedding, 800m)); prices.Add(new Price(Arrangement.Wellness, 750m)); Car car = new Car("RabbitHole", "Delux", "Brown", prices); DeliveryAddress address = new DeliveryAddress("Teaparty", "1", "Wonderland"); rm.AddCar(car); DateTime startTime = new DateTime(2020, 12, 12, 8, 0, 0); Arrangement arrangement = Arrangement.Airport; DateTime endTime = new DateTime(2020, 12, 12, 12, 0, 0); var reservationInfo = new ReservationInfo(start, stop, startTime, arrangement, endTime, address); mockViewModel.CurrentClient = client; mockViewModel.ReservationInfo = reservationInfo; mockViewModel.CurrentCar = car; var reservation = new Reservation(client, car, reservationInfo); mockViewModel.AddReservationCommand.Execute(null); Assert.AreEqual(reservation.Client, mockViewModel.Reservations.Last().Client); }
public async Task <IActionResult> Create(AddReservationViewModel model) { byte[] empIdBytes = new byte[200]; if (HttpContext.Session.TryGetValue("empId", out empIdBytes)) { int employeeId = int.Parse(Encoding.UTF8.GetString(empIdBytes)); if (employeeId == 1) { List <Employee> employees = (from emp in _context.Employees where emp.Id == employeeId select emp).ToList(); Employee employee = employees[0]; ViewData["username"] = employee.Username; ViewData["isLoggedIn"] = true; ViewData["isAdmin"] = true; return(RedirectToAction(nameof(AdminIndex))); } else if (employeeId != 1 && employeeId > 0) { List <Employee> employees = (from emp in _context.Employees where emp.Id == employeeId select emp).ToList(); Employee employee = employees[0]; ViewData["username"] = employee.Username; ViewData["isLoggedIn"] = true; ViewData["isAdmin"] = false; if (ModelState.IsValid) { Ticket t = (from _t in _context.Tickets where _t.TicketTypeId == model.TicketTypeId select _t).First(); int rId = _context.Reservations.Max(r => r.Id); Reservation _res = new Reservation() { Id = rId + 1, ReservationDate = DateTime.Now, TicketId = t.Id }; _context.Reservations.Add(_res); await _context.SaveChangesAsync(); int pId = _context.Passagers.Max(p => p.Id); Passager _pass = new Passager() { Id = pId + 1, FirstName = model.FirstName, SecondName = model.SecondName, LastName = model.LastName, Egn = model.EGN, PhoneNumber = model.PhoneNumber, NationalityId = model.NationalityId }; _context.Passagers.Add(_pass); await _context.SaveChangesAsync(); int _flId = _context.Flights.Find(_context.Tickets.Find(_res.TicketId).FlightId).Id; foreach (var item in flightCtrl.passagerNames) { if (item.Equals(new Tuple <int, List <string> >(_flId, new List <string>()))) { item.Item2.Add(_pass.FirstName + " " + _pass.LastName); } } int psId = _context.ReservationPassagers.Max(ps => ps.Id); ReservationPassager _rp = new ReservationPassager() { Id = psId + 1, PassagerId = _pass.Id, ResrvationId = _res.Id }; _context.ReservationPassagers.Add(_rp); await _context.SaveChangesAsync(); // sends email MimeMessage message = new MimeMessage(); MailboxAddress from = new MailboxAddress("Admin", "*****@*****.**"); message.From.Add(from); string passFullName = _pass.FirstName + " " + _pass.LastName; MailboxAddress to = new MailboxAddress(passFullName, model.Email); message.To.Add(to); message.Subject = "About your flight reservation:"; string text = "Hello, " + passFullName + "!" + Environment.NewLine + "Today (" + _res.ReservationDate + "), you made a reservation for a plane ticket for a flight with No " + model.FlightId + ". Your ticket is with No " + _res.TicketId + "." + Environment.NewLine + Environment.NewLine + "Personal Information:" + Environment.NewLine + "First Name: " + _pass.FirstName + Environment.NewLine + "Second Name: " + _pass.SecondName + Environment.NewLine + "Last Name: " + _pass.LastName + Environment.NewLine + "Personal Identification Number: " + _pass.Egn + Environment.NewLine + "Phone Number: " + _pass.PhoneNumber + Environment.NewLine + "Email: " + model.Email + Environment.NewLine + Environment.NewLine + "If some of this data is incorrect or the person who made this reservation was not you," + " please contact us on email: [email protected]" + Environment.NewLine + Environment.NewLine + "Greetings from FlightManager's team!"; message.Body = new TextPart("plain") { Text = text }; using (var client = new MailKit.Net.Smtp.SmtpClient()) { client.Connect("smtp.gmail.com", 587, false); client.Authenticate("*****@*****.**", "myPass_Petya123"); client.Send(message); await client.DisconnectAsync(true); } ///////////////////////////////////////////////////////////////// return(RedirectToAction(nameof(Index))); } return(View(model)); } else { ViewData["isLoggedIn"] = false; ViewData["isAdmin"] = false; if (ModelState.IsValid) { Ticket t = (from _t in _context.Tickets where _t.TicketTypeId == model.TicketTypeId select _t).First(); int rId = _context.Reservations.Max(r => r.Id); Reservation _res = new Reservation() { Id = rId + 1, ReservationDate = DateTime.Now, TicketId = t.Id }; _context.Reservations.Add(_res); await _context.SaveChangesAsync(); int pId = _context.Passagers.Max(p => p.Id); Passager _pass = new Passager() { Id = pId + 1, FirstName = model.FirstName, SecondName = model.SecondName, LastName = model.LastName, Egn = model.EGN, PhoneNumber = model.PhoneNumber, NationalityId = model.NationalityId }; _context.Passagers.Add(_pass); await _context.SaveChangesAsync(); int psId = _context.ReservationPassagers.Max(ps => ps.Id); ReservationPassager _rp = new ReservationPassager() { Id = psId + 1, PassagerId = _pass.Id, ResrvationId = _res.Id }; _context.ReservationPassagers.Add(_rp); await _context.SaveChangesAsync(); // sends email MimeMessage message = new MimeMessage(); MailboxAddress from = new MailboxAddress("Admin", "*****@*****.**"); message.From.Add(from); string passFullName = _pass.FirstName + " " + _pass.LastName; MailboxAddress to = new MailboxAddress(passFullName, model.Email); message.To.Add(to); message.Subject = "About your flight reservation:"; string text = "Hello, " + passFullName + "!" + Environment.NewLine + "Today (" + _res.ReservationDate + "), you made a reservation for a plane ticket for a flight with No " + model.FlightId + ". Your ticket is with No " + _res.TicketId + "." + Environment.NewLine + Environment.NewLine + "Personal Information:" + Environment.NewLine + "First Name: " + _pass.FirstName + Environment.NewLine + "Second Name: " + _pass.SecondName + Environment.NewLine + "Last Name: " + _pass.LastName + Environment.NewLine + "Personal Identification Number: " + _pass.Egn + Environment.NewLine + "Phone Number: " + _pass.PhoneNumber + Environment.NewLine + "Email: " + model.Email + Environment.NewLine + Environment.NewLine + "If some of this data is incorrect or the person who made this reservation was not you," + " please contact us on email: [email protected]" + Environment.NewLine + Environment.NewLine + "Greetings from FlightManager's team!"; message.Body = new TextPart("plain") { Text = text }; using (var client = new MailKit.Net.Smtp.SmtpClient()) { client.Connect("smtp.gmail.com", 587, false); client.Authenticate("*****@*****.**", "myPass_Petya123"); client.Send(message); await client.DisconnectAsync(true); } ///////////////////////////////////////////////////////////////// return(RedirectToAction(nameof(Index))); } return(View(model)); } } else { ViewData["isLoggedIn"] = false; ViewData["isAdmin"] = false; if (ModelState.IsValid) { Ticket t = (from _t in _context.Tickets where _t.TicketTypeId == model.TicketTypeId select _t).First(); int rId = _context.Reservations.Max(r => r.Id); Reservation _res = new Reservation() { Id = rId + 1, ReservationDate = DateTime.Now, TicketId = t.Id }; _context.Reservations.Add(_res); await _context.SaveChangesAsync(); int pId = _context.Passagers.Max(p => p.Id); Passager _pass = new Passager() { Id = pId + 1, FirstName = model.FirstName, SecondName = model.SecondName, LastName = model.LastName, Egn = model.EGN, PhoneNumber = model.PhoneNumber, NationalityId = model.NationalityId }; _context.Passagers.Add(_pass); await _context.SaveChangesAsync(); int psId = _context.ReservationPassagers.Max(ps => ps.Id); ReservationPassager _rp = new ReservationPassager() { Id = psId + 1, PassagerId = _pass.Id, ResrvationId = _res.Id }; _context.ReservationPassagers.Add(_rp); await _context.SaveChangesAsync(); // sends email MimeMessage message = new MimeMessage(); MailboxAddress from = new MailboxAddress("Admin", "*****@*****.**"); message.From.Add(from); string passFullName = _pass.FirstName + " " + _pass.LastName; MailboxAddress to = new MailboxAddress(passFullName, model.Email); message.To.Add(to); message.Subject = "About your flight reservation:"; string text = "Hello, " + passFullName + "!" + Environment.NewLine + "Today (" + _res.ReservationDate + "), you made a reservation for a plane ticket for a flight with No " + model.FlightId + ". Your ticket is with No " + _res.TicketId + "." + Environment.NewLine + Environment.NewLine + "Personal Information:" + Environment.NewLine + "First Name: " + _pass.FirstName + Environment.NewLine + "Second Name: " + _pass.SecondName + Environment.NewLine + "Last Name: " + _pass.LastName + Environment.NewLine + "Personal Identification Number: " + _pass.Egn + Environment.NewLine + "Phone Number: " + _pass.PhoneNumber + Environment.NewLine + "Email: " + model.Email + Environment.NewLine + Environment.NewLine + "If some of this data is incorrect or the person who made this reservation was not you," + " please contact us on email: [email protected]" + Environment.NewLine + Environment.NewLine + "Greetings from FlightManager's team!"; message.Body = new TextPart("plain") { Text = text }; using (var client = new MailKit.Net.Smtp.SmtpClient()) { client.Connect("smtp.gmail.com", 587, false); client.Authenticate("*****@*****.**", "myPass_Petya123"); client.Send(message); await client.DisconnectAsync(true); } ///////////////////////////////////////////////////////////////// return(RedirectToAction(nameof(Index))); } return(View(model)); } }
public AddReservationView(AddReservationViewModel viewModel) : base(viewModel, DataWindowMode.Custom) { InitializeComponent(); }