Exemplo n.º 1
0
        public void HotelBookingControllerEdit()
        {
            // Arrange
            var hotelbookingRepo = new Mock <HotelBookingRepository>();
            var hotelroomRepo    = new Mock <HotelRoomRepository>();
            var tamagotchiRepo   = new Mock <TamagotchiRepository>();

            var controller = new HotelBookingController(hotelbookingRepo.Object, hotelroomRepo.Object, tamagotchiRepo.Object);

            var hotelbooking = hotelbookingRepo.Object.GetAll().LastOrDefault();

            if (hotelbooking != null)
            {
                var hotelbookingVM = new HotelBookingVM(hotelbooking);
                //Act
                hotelbookingVM.TamagotchisIds = new int[] { 1, 2 };

                controller.Edit(hotelbookingVM);

                var lastDatabaseItem = hotelbookingRepo.Object.GetAll().LastOrDefault();

                // Assert
                Assert.AreEqual(lastDatabaseItem.HotelRoom.HotelRoomName, hotelbookingVM.HotelRoomName);
            }
            else
            {
                Assert.IsNull(hotelbooking);
            }
        }
        private void CheckIfYouCanBookRoom(HotelBookingVM hotelBookingVM)
        {
            var hotelroom = _hotelRoomRepository.GetWhereId(hotelBookingVM.HotelRoomId);

            if (hotelBookingVM.TamagotchisIds != null)
            {
                if (!(hotelBookingVM.TamagotchisIds.Count() <= hotelroom.RoomSize))
                {
                    ModelState.AddModelError("", $"Amount of tamagotchis is to big for this room(room size: {hotelroom.RoomSize})");
                }

                var tamagotchisVM = hotelBookingVM.TamagotchisIds.Select(ti => new TamagotchiVM(_tamagotchiRepository.GetWhereId(ti))).ToList();

                foreach (var tamagotchi in tamagotchisVM)
                {
                    if (tamagotchi.Money < hotelroom.HotelRoomType.RoomTypeCost)
                    {
                        ModelState.AddModelError("", $"{tamagotchi.Name} does not have enough money for this room ( room cost: {hotelroom.HotelRoomType.RoomTypeCost} , tamagotchi money: {tamagotchi.Money})");
                        break;
                    }
                }
            }
            else
            {
                ModelState.AddModelError("", "Amount of tamagotchis can not be 0");
            }
        }
Exemplo n.º 3
0
        public void HotelBookingControllerCreate()
        {
            // Arrange
            var hotelbookingRepo = new Mock <HotelBookingRepository>();
            var hotelroomRepo    = new Mock <HotelRoomRepository>();
            var tamagotchiRepo   = new Mock <TamagotchiRepository>();
            var controller       = new HotelBookingController(hotelbookingRepo.Object, hotelroomRepo.Object, tamagotchiRepo.Object);

            var hotelbookingVM = new HotelBookingVM();


            var hotelrooms = hotelroomRepo.Object.GetAllHotelRoomsWhereBookingIsNull().ToList();

            if (hotelrooms != null)
            {
                // Act
                hotelbookingVM.HotelRoomId    = hotelrooms[0].HotelRoomId;
                hotelbookingVM.TamagotchisIds = new int[] { 1, 2 };

                controller.Create(hotelbookingVM);

                var lastDatabaseItem = hotelbookingRepo.Object.GetAll().LastOrDefault();

                // Assert
                Assert.AreEqual(lastDatabaseItem.HotelRoomId, hotelbookingVM.HotelRoomId);
            }
            else
            {
                Assert.IsNull(hotelrooms);
            }
        }
        public ActionResult DeleteConfirmed(int id)
        {
            HotelBookingVM hotelBookingVM = new HotelBookingVM(_hotelBookingRepository.GetWhereId(id));

            _hotelBookingRepository.Delete(hotelBookingVM.ToModel());

            return(RedirectToAction("Index"));
        }
        // GET: HotelBookings/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            HotelBookingVM hotelBookingVM = new HotelBookingVM(_hotelBookingRepository.GetWhereId(id));

            if (hotelBookingVM == null)
            {
                return(HttpNotFound());
            }
            return(View(hotelBookingVM));
        }
        private void ChangeTamagotchiHotelRoom(HotelBookingVM hotelBookingVM)
        {
            if (hotelBookingVM.TamagotchisIds != null)
            {
                List <TamagotchiVM> tamagotchisVM = hotelBookingVM.TamagotchisIds.Select(ti => new TamagotchiVM(_tamagotchiRepository.GetWhereId(ti))).ToList();

                foreach (var tamagotchiVM in tamagotchisVM)
                {
                    tamagotchiVM.HotelBooking = null;
                    tamagotchiVM.HotelRoomId  = hotelBookingVM.HotelRoomId;
                    _tamagotchiRepository.Edit(tamagotchiVM.ToModel());
                }
            }
        }
        public ActionResult Create()
        {
            var hotelRoomsWithOutBooking = this._hotelRoomRepository.GetAllHotelRoomsWhereBookingIsNull();
            var tamagotchi = this._tamagotchiRepository.GetAllTamagotchisALiveAndNoHotelRoom();

            if (hotelRoomsWithOutBooking.Count() > 0 && tamagotchi.Count() > 0)
            {
                var hotelBookingVM = new HotelBookingVM();

                ViewBag.HotelRoomId    = new SelectList(hotelRoomsWithOutBooking, "HotelRoomId", "HotelRoomName");
                ViewBag.TamagotchisIds = new MultiSelectList(tamagotchi, "TamagotchiId", "Name");

                return(View(hotelBookingVM));
            }

            return(RedirectToAction("Index"));
        }
        public ActionResult Edit(HotelBookingVM hotelBookingVM)
        {
            this.CheckIfYouCanBookRoom(hotelBookingVM);

            if (ModelState.IsValid)
            {
                _hotelBookingRepository.Edit(hotelBookingVM.ToModel());
                ChangeTamagotchiHotelRoom(hotelBookingVM);
                return(RedirectToAction("Index"));
            }

            var tamagotchi = this._tamagotchiRepository.GetAllTamagotchisALiveAndNoHotelRoom();

            ViewBag.TamagotchisIds = new MultiSelectList(tamagotchi, "TamagotchiId", "Name", hotelBookingVM.TamagotchisIds);

            hotelBookingVM = new HotelBookingVM(_hotelBookingRepository.GetWhereId(hotelBookingVM.HotelRoomId));

            return(View(hotelBookingVM));
        }
        public ActionResult Create(HotelBookingVM hotelBookingVM)
        {
            this.CheckIfYouCanBookRoom(hotelBookingVM);

            if (ModelState.IsValid)
            {
                _hotelBookingRepository.Add(hotelBookingVM.ToModel());
                ChangeTamagotchiHotelRoom(hotelBookingVM);
                return(RedirectToAction("Index"));
            }

            var hotelRoomsWithOutBooking = this._hotelRoomRepository.GetAllHotelRoomsWhereBookingIsNull();

            ViewBag.HotelRoomId = new SelectList(hotelRoomsWithOutBooking, "HotelRoomId", "HotelRoomName", hotelBookingVM.HotelRoomId);

            var tamagotchi = this._tamagotchiRepository.GetAllTamagotchisALiveAndNoHotelRoom();

            ViewBag.TamagotchisIds = new MultiSelectList(tamagotchi, "TamagotchiId", "Name", hotelBookingVM.TamagotchisIds);

            return(View(hotelBookingVM));
        }
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            HotelBookingVM hotelBookingVM = new HotelBookingVM(_hotelBookingRepository.GetWhereId(id));

            _hotelBookingRepository.SetAllTamagotchiHotelRoomToNull(hotelBookingVM.ToModel());

            if (hotelBookingVM == null)
            {
                return(HttpNotFound());
            }

            var tamagotchi = this._tamagotchiRepository.GetAllTamagotchisALiveAndNoHotelRoom();

            ViewBag.TamagotchisIds = new MultiSelectList(tamagotchi, "TamagotchiId", "Name", hotelBookingVM.TamagotchisIds);

            return(View(hotelBookingVM));
        }