Example #1
0
        /// <summary>
        /// Deletes the record with specified id
        /// </summary>
        /// <param name="id">The id of the record that needs to be deleted</param>
        /// <returns>Returns true if operation is successful otherwise returns false</returns>
        public bool Delete(int id)
        {
            var result  = false;
            var booking = Find(id);

            if (booking != null)
            {
                //First Delete refrencing MenuBooking Records
                result = new RestaurantMenuBookingRepository().Delete(booking.BookingId);

                //If successful then delete Table Booking Records
                if (result)
                {
                    result = new RestaurantTableBookingRepository().Delete(booking.BookingId);
                }

                //If successful then delete all associated Bill
                if (result)
                {
                    result = booking.Bills
                             .Select(bill => new BookingBillRepository().Delete(bill.BillId))
                             .All(deleted => deleted);
                }

                //If all goes well then delete the booking
                if (result)
                {
                    using (var cn = new SqlConnection(DatabaseConnection.ConnectionStringToDb))
                    {
                        using (var cmd = new SqlCommand("DeleteRestaurantBooking", cn))
                        {
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.Parameters.Add("@BOOKINGID", SqlDbType.BigInt).Value = id;
                            cn.Open();
                            var res = cmd.ExecuteNonQuery();
                            result = res == 1;
                        }
                    }
                }
            }
            return(result);
        }
Example #2
0
        /// <summary>
        /// Cancells the Restaurant Booking with specfied booking id
        /// </summary>
        /// <param name="bookingid">The id of the Booking which needs to be cancelled</param>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        /// <returns>Returns true if operation is successful otherwise returns false</returns>
        public bool Cancel(int bookingid)
        {
            var  booking = Find(bookingid);
            bool result;

            if (booking == null)
            {
                throw new ArgumentException(String.Format("A Booking with booking id:{0} does not exist!", bookingid), "bookingid");
            }
            if (booking.Status != RestaurantBooking.BookingStatus.Confirmed)
            {
                throw new InvalidOperationException("A booking cannot be cancelled if it is not a Confirmed Booking");
            }

            booking.Status = RestaurantBooking.BookingStatus.Cancelled;
            using (var cn = new SqlConnection(DatabaseConnection.ConnectionStringToDb))
            {
                using (var cmd = new SqlCommand("UpdateRestaurantBooking", cn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@BOOKINGID", SqlDbType.BigInt).Value      = booking.BookingId;
                    cmd.Parameters.Add("@CUSTOMERID", SqlDbType.BigInt).Value     = booking.BookingCustomer.UserId;
                    cmd.Parameters.Add("@BOOKEDON", SqlDbType.DateTime).Value     = booking.BookedOn;
                    cmd.Parameters.Add("@BOOKEDFOR", SqlDbType.DateTime).Value    = booking.BookedFor.ToUniversalTime();
                    cmd.Parameters.Add("@BOOKEDTILL", SqlDbType.DateTime).Value   = booking.BookedTill.ToUniversalTime();
                    cmd.Parameters.Add("@BOOKINGSTATUS", SqlDbType.TinyInt).Value = (int)booking.Status;

                    cn.Open();
                    var res = cmd.ExecuteNonQuery();
                    result = res == 1;
                }
            }

            if (result)
            {
                result = new RestaurantTableBookingRepository().Delete(booking.BookingId)
                         & new RestaurantMenuBookingRepository().Delete(booking.BookingId);
            }

            return(result);
        }
Example #3
0
        /// <summary>
        /// Updates the Available Record with new Details (Do not use this to cancel a Booking)
        /// </summary>
        /// <param name="booking">The Modified Record which needs to be updated</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        /// <returns>Returns true if operation is successful otherwise returns false</returns>
        public bool Update(RestaurantBooking booking)
        {
            bool result;

            if (booking == null)
            {
                throw new ArgumentNullException("booking");
            }
            if (Find(booking.BookingId) == null)
            {
                return(false);
            }

            if (booking.Status != RestaurantBooking.BookingStatus.Confirmed || booking.Status != RestaurantBooking.BookingStatus.InProcess)
            {
                throw new InvalidOperationException("Only a Booking which is Confirmed or is In Process can be altered");
            }
            if (booking.Bills.Count < 1)
            {
                throw new InvalidOperationException("A Booking cannot be altered without the Bills Details");
            }
            if (booking.BookingCustomer == null || booking.BookingCustomer.UserId < 1)
            {
                throw new InvalidOperationException("A Booking Cannot be altered without details of the customer who booked it");
            }
            if (booking.BookedTables.Count < 1)
            {
                throw new InvalidOperationException("A Booking cannot be altered if it does not have any Booked Table Details");
            }

            if (new RestaurantTableBookingRepository().Delete(booking.BookingId))
            {
                //Checks to see if the tables selected are already booked
                if (new RestaurantTableRepository().AreTheseTablesBooked(booking.BookedTables, booking.BookedFor.ToUniversalTime(), booking.BookedTill.ToUniversalTime()))
                {
                    throw new InvalidOperationException("Cannot update with new Table selection as One or More of them are already booked");
                }
            }

            booking.Status = RestaurantBooking.BookingStatus.Confirmed;
            using (var cn = new SqlConnection(DatabaseConnection.ConnectionStringToDb))
            {
                using (var cmd = new SqlCommand("UpdateRestaurantBooking", cn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@BOOKINGID", SqlDbType.BigInt).Value      = booking.BookingId;
                    cmd.Parameters.Add("@CUSTOMERID", SqlDbType.BigInt).Value     = booking.BookingCustomer.UserId;
                    cmd.Parameters.Add("@BOOKEDON", SqlDbType.DateTime).Value     = booking.BookedOn;
                    cmd.Parameters.Add("@BOOKEDFOR", SqlDbType.DateTime).Value    = booking.BookedFor.ToUniversalTime();
                    cmd.Parameters.Add("@BOOKEDTILL", SqlDbType.DateTime).Value   = booking.BookedTill.ToUniversalTime();
                    cmd.Parameters.Add("@BOOKINGSTATUS", SqlDbType.TinyInt).Value = (int)booking.Status;

                    cn.Open();
                    var res = cmd.ExecuteNonQuery();
                    result = res == 1;
                }
            }

            //Finally Update the Table and Menu bookings and expect Table booking to returns false as Delete was already called on it
            if (result)
            {
                result = new RestaurantTableBookingRepository().Update(booking) |
                         new RestaurantMenuBookingRepository().Update(booking);
            }

            //And Update the bill ofcourse
            if (result)
            {
                var bookingbill = booking.Bills;
                var repo        = new BookingBillRepository();
                foreach (var bookingBill in bookingbill)
                {
                    result = bookingBill.BillId < 1 ?
                             repo.Add(new BookingBill(booking.BookingId)
                    {
                        DiscountAmount = bookingBill.DiscountAmount,
                        GrossAmount    = bookingBill.GrossAmount,
                        NetAmount      = bookingBill.NetAmount
                    }) > 0
                                     : repo.Update(bookingBill);
                }
            }
            return(result);
        }