Пример #1
0
        public List <Booking> GetAllBookings()
        {
            SqlCommand     cmd;
            List <Booking> res = new List <Booking>();

            int[]  roomId = new int[50];
            string sql    = "SELECT * FROM [HotelBooking].[dbo].[Booking]";

            using (SqlConnection con = new SqlConnection(connectionstring))
            {
                cmd = new SqlCommand(sql, con);
                try
                {
                    con.Open();
                    var read = cmd.ExecuteReader();
                    if (read.HasRows)
                    {
                        try
                        {
                            int counter = 0;
                            while (read.Read())
                            {
                                //get the stuff
                                var booking = new Booking();
                                booking.Id = (int)read["Id"];
                                booking.customerComment = read["customerComment"].ToString();
                                booking.dateOfOrder     = (DateTime)read["DateOfOrder"];
                                booking.startDate       = (DateTime)read["StartDate"];
                                booking.endDate         = (DateTime)read["EndDate"];
                                booking.totalPrice      = (float)read["totalPrice"];
                                roomId[counter]         = (int)read["RoomId"];
                                res.Add(booking);
                                counter++;
                            }
                        }
                        catch (Exception e)
                        {
                            throw e;
                        }
                    }
                }
                catch (SqlException e)
                {
                    throw e;
                }
                finally
                {
                    con.Close();
                }
            }
            for (var i = 0; i <= res.Count; i++)
            {
                res[i].room = roomRepo.GetRoomDetails(roomId[i]);
            }
            return(res);
        }
Пример #2
0
        public bool BookRoom(int roomId, DateTime newBookingStart, DateTime newBookingEnd, string customerComment)
        {
            //is the data valid?
            if (newBookingStart.Date > newBookingEnd.Date)
            {
                throw new ArgumentException("Illegal: start should be before end");
            }

            //which room are we dealing with?
            Room room = roomRepo.GetRoomDetails(roomId);

            //Is the room available at this time?
            var bookings = repo.GetBookingsByRoom(room);

            foreach (var item in bookings)
            {
                if (!((item.startDate.Date > newBookingStart.Date && item.startDate.Date > newBookingEnd.Date) || (newBookingStart.Date > item.endDate.Date && newBookingEnd.Date > item.endDate.Date)))
                {
                    throw new Exception("Room is not available in this period! It clashed with Booking ID: " + item.Id);
                }
            }

            //all is well. Let's create the booking.
            Booking booking = new Booking();

            booking.room            = room;
            booking.startDate       = newBookingStart;
            booking.endDate         = newBookingEnd;
            booking.totalPrice      = (newBookingEnd.Day + 1 - newBookingStart.Day) * room.PricePerDay;
            booking.dateOfOrder     = DateTime.Today;
            booking.customerComment = customerComment;
            return(repo.BookRoom(booking));
        }
Пример #3
0
        public void GetRoomDetailsTest()
        {
            //arrange
            //repo = new BookingRepo(connectionString);
            repo = _serviceProvider.GetService <IRoomRepo>();
            Room room = new Room();

            room.Id          = 1;
            room.status      = "A bit dusty";
            room.PricePerDay = 12.5F;
            //act
            var res = repo.GetRoomDetails(room.Id);

            //assert
            Assert.AreEqual(room.Id, res.Id);
            Assert.AreEqual(room.status, res.status);
            Assert.AreEqual(room.PricePerDay, res.PricePerDay);
        }
Пример #4
0
 public Room GetRoom(int i)
 {
     return(repo.GetRoomDetails(i));
 }