public void TestSameSatrtEndDay()
        {
            var reservation = new Reservation()
            {
                LectureHallId = 1,
                LecturerId    = 1,
                StartDate     = new DateTime(2019, 12, 3, 8, 0, 0),
                EndDate       = new DateTime(2019, 12, 4, 11, 0, 0)
            };
            var results = _Validator.Validate(reservation);

            Assert.IsFalse(results.IsValid);
            CollectionAssert.Contains(results.Errors.Select(e => e.ErrorMessage),
                                      ReservationValidator.MsgWrongDay);
        }
Example #2
0
        public IActionResult Post([FromBody] ReservationDto reservation)
        {
            var reserv    = _Mapper.Map <ReservationDto, Reservation>(reservation);
            var validator = new ReservationValidator(_Repo);
            var results   = validator.Validate(reserv);

            if (results.IsValid)
            {
                _Repo.Create(reserv);
                _Repo.SaveChanges();
                _Logger.LogInformation($"Reservation {reserv.ID} was created");
                return(CreatedAtAction(nameof(Get), new { id = reserv.ID }));
            }
            results.Errors.ToList().ForEach(e => ModelState.AddModelError("Reservation is not valid", e.ErrorMessage));
            return(BadRequest(new ValidationProblemDetails(ModelState)));
        }
Example #3
0
        // Register Car Rent reservation database manipulations
        public void CreateReservation(Reservation reservation)
        {
            PrintColorMessage    colorMessage = new PrintColorMessage();
            Navigation           navigation   = new Navigation();
            ReservationValidator validator    = new ReservationValidator();
            ValidationResult     result       = validator.Validate(reservation);

            // Input Customer Name Validation
            if (!result.IsValid)
            {
                colorMessage.Print(ConsoleColor.Red, "\nReservation is not created! See reason below:");

                //data validation
                foreach (var failure in result.Errors)
                {
                    Console.WriteLine("\n '" + failure.PropertyName + "' written incorrectly . \n Details: " + failure.ErrorMessage);
                }
            }
            else
            {
                using (var context = new ApplicationDbContext())
                {
                    try
                    {
                        context.Reservations.Add(reservation);

                        try
                        {
                            context.SaveChanges();
                        }
                        catch (SqlException)
                        {
                            colorMessage.Print(ConsoleColor.Red, "Error: CustomerID should exist in the Customer table");
                        }

                        colorMessage.Print(ConsoleColor.Yellow, "Reservation created succesffuly!");
                        navigation.GoToMenu();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
        }
Example #4
0
        public void MakeReservation(Reservation reservation, Hotel hotel)
        {
            var validationResult = _validator.Validate(reservation);

            reservation.Result = "Decline";
            if (validationResult.IsValid && hotel != null)
            {
                Room room = _reservationStrategy.FindBestRoom(hotel, reservation.StartDay, reservation.EndDay);
                if (room != null)
                {
                    reservation.Result = "Accept";
                    _reservationStrategy.BookDays(room, reservation.StartDay, reservation.EndDay);
                }
            }
            else
            {
                foreach (var failure in validationResult.Errors)
                {
                    Console.WriteLine("Failed validation. Error was: " + failure.ErrorMessage);
                }
            }
        }
Example #5
0
        // Update Reservation database manipulations
        public void UpdateReservation(Reservation reservation)
        {
            Car car = new Car();
            ReservationValidator validator    = new ReservationValidator();
            ValidationResult     result       = validator.Validate(reservation);
            PrintColorMessage    colorMessage = new PrintColorMessage();


            // Input Customer Name Validation
            if (!result.IsValid)
            {
                colorMessage.Print(ConsoleColor.Red, "\nReservation is not Updated! See reason below:");

                //data validation
                foreach (var failure in result.Errors)
                {
                    Console.WriteLine("\n '" + failure.PropertyName + "' written incorrectly . \n Details: " + failure.ErrorMessage);
                }
            }
            else
            {
                //EF
                using (var context = new ApplicationDbContext())
                {
                    var reservationToUpdate =
                        context.Reservations.First(x => x.ReservationID == reservation.ReservationID);
                    reservationToUpdate.CarPlate  = reservation.CarPlate;
                    reservationToUpdate.StartDate = reservation.StartDate;
                    reservationToUpdate.EndDate   = reservation.EndDate;
                    reservationToUpdate.Location  = reservation.Location;


                    context.SaveChanges();
                }
            }
        }