Esempio n. 1
0
        public IHttpActionResult PostReserveRoom([FromBody]ReserveConfirmationModel model)
        {

            if (model.id == null)
            {
                return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The room is not found. Bad request!"));
            }
            Room roomToReserve = db.Rooms.SingleOrDefault(x => x.RoomId == model.id);

            if (roomToReserve == null)
            {
                return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "The room is already taken. Please make sure to search for another room."));
            }
            RoomSearchModel roomConfirmation = new RoomSearchModel();

            roomConfirmation.AccomodationId = roomToReserve.AccomodationId;
            roomConfirmation.AccomodationName = roomToReserve.Accomodation.AccomodationName;
            roomConfirmation.NumberOfRooms = roomToReserve.NumberOfRooms;
            roomConfirmation.Price = model.Price;   //
            roomConfirmation.RoomCapacity = roomToReserve.RoomCapacity;
            roomConfirmation.RoomDetails = roomToReserve.RoomDetails;
            roomConfirmation.RoomId = model.id.Value;
            roomConfirmation.RoomType_Name = roomToReserve.RoomType.RoomTypeName;
            roomConfirmation.RoomTypeId = roomToReserve.RoomTypeId;
            roomConfirmation.TempArrivalDate = model.ArrivalDate;   //
            roomConfirmation.TempDepartureDate = model.DepartureDate;   //
            roomConfirmation.TempIsPaid = model.IsPaid; //
            roomConfirmation.TempReservationString = null;
            roomConfirmation.TempTotalPrice = model.TotalPrice; //
            roomConfirmation.TempUserId = model.UserId; //
            try
            {
                roomConfirmation.PhotoUrl = db.Images.FirstOrDefault(
                    x => x.AccomodationId == roomConfirmation.AccomodationId &&
                    x.RoomTypeId == roomConfirmation.RoomTypeId &&
                    x.Priority == 0)
                    .PhotoUrl;
            }
            catch (Exception e)
            {
                roomConfirmation.PhotoUrl = "https://www.roominreturn.nl/static/images/room-default.svg";
            }
            
            return Ok(roomConfirmation);
        }
Esempio n. 2
0
        public RoomSearchModel GetAvailabilityAndPrice(int accomodationId, int roomId, DateTime? from, DateTime? to, string UserId)   //nedostaje UserID da bi se znalo za koga se soba rezerviše
        {
            var currentBooking = db.RoomAvailabilities.Where((x => x.RoomId == roomId && x.AccomodationId == accomodationId &&
                (from <= x.DepartureDate && from >= x.ArrivalDate || x.ArrivalDate <= to && x.ArrivalDate >= from)))
                .ToList();

            var roomForInformation = db.Rooms.Where(x => x.RoomId == roomId).SingleOrDefault();

            var roomTypeId = roomForInformation.RoomTypeId;
            var pricePerNight = roomForInformation.Price;
            var numberOfRooms = roomForInformation.NumberOfRooms;
            var roomCapacity = roomForInformation.RoomCapacity;
            
            //START - NEW TEST

            if (currentBooking.Count >= numberOfRooms)
            {
                return null;
            }

            //END - NEW TEST

            RoomSearchModel foundRoom = new RoomSearchModel()
            {
                AccomodationId = accomodationId,
                Price = pricePerNight,
                RoomId = roomId,
                RoomTypeId = roomTypeId,
                NumberOfRooms = numberOfRooms,
                RoomCapacity = roomCapacity,
                TempArrivalDate = from.Value,
                TempDepartureDate = to.Value,
                TempIsPaid = false,
                TempUserId = UserId
            };


            double totalPrice = 0;
            DateTime currentDay = new DateTime();
            double pricePerDay = 0;

            currentDay = from.Value;

            while (currentDay <= to.Value)
            {
                try
                {
                    pricePerDay = db.RoomPrices.Where(x => x.StartDate <= currentDay && x.EndDate >= currentDay && x.RoomTypeId == roomTypeId && x.AccomodationId == foundRoom.AccomodationId)
                                        .FirstOrDefault().SpecialPrice;
                }
                catch (Exception)
                {
                    //Implement the error LOG when the user does not sets the prices for a room on certain dates. The error will be cought here!
                    totalPrice += pricePerNight;
                }
                totalPrice += pricePerDay;
                currentDay = currentDay.AddDays(1);
            }

            double totalDays = (to.Value - from.Value).TotalDays;

            foundRoom.Price = totalPrice / totalDays;

            foundRoom.TempTotalPrice = totalPrice;

            return foundRoom;
        }