Esempio n. 1
0
        public async Task <HttpResponseMessage> Get(int id = -1)
        {
            if (id != -1)
            {
                Booking res = await _db.Bookings.Where(b => b.Id == id && !b.Deleted).FirstOrDefaultAsync();

                if (res == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NoContent, "No Booking Found With ID"));
                }

                LibBookingService.Dtos.Booking booking = CreateBoookingFromDbBooking(res);

                return(Request.CreateResponse(HttpStatusCode.OK, booking));
            }
            else
            {
                IEnumerable <Booking> res = await _db.Bookings.Where(b => b.Deleted != true).ToListAsync();

                IEnumerable <LibBookingService.Dtos.Booking> bookings = res.Select(b => CreateBoookingFromDbBooking(b));

                return(bookings.Any() ?
                       Request.CreateResponse(HttpStatusCode.OK, bookings) :
                       Request.CreateErrorResponse(HttpStatusCode.NoContent, "No Bookings"));
            }
        }
Esempio n. 2
0
        public async Task <HttpResponseMessage> Update(int id, LibBookingService.Dtos.Booking booking)
        {
            try
            {
                Booking b = await _db.Bookings.Where(bb => bb.Id == id).FirstOrDefaultAsync();

                b.Customer_id     = booking.CustomerId;
                b.Restaurant_id   = booking.RestaurantId;
                b.BookingMadeDate = booking.BookingMadeDate;
                b.BookingMadeTime = booking.BookingMadeTime;
                b.BookingDate     = booking.BookingDate;
                b.StartTime       = booking.StartTime;
                b.EndTime         = booking.EndTime;
                b.PaymentTotal    = booking.PaymentTotal;
                b.PaymentMadeDate = booking.PaymentMadeDate;
                b.NoCustomers     = booking.NoCustomers;
                b.Comments        = booking.Comments;
                b.Cancelled       = booking.Cancelled;

                _db.SetModified(b);
                await _db.SaveChangesAsync();

                LibBookingService.Dtos.Booking res = CreateBoookingFromDbBooking(b);

                return(Request.CreateResponse(HttpStatusCode.OK, res));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Failed"));
            }
        }
Esempio n. 3
0
        public async Task <HttpResponseMessage> Post(LibBookingService.Dtos.Booking booking)
        {
            try
            {
                Booking newBoooking = _db.Bookings.Add(new Booking
                {
                    Customer_id     = booking.CustomerId,
                    Restaurant_id   = booking.RestaurantId,
                    BookingMadeDate = booking.BookingMadeDate,
                    BookingMadeTime = booking.BookingMadeTime,
                    BookingDate     = booking.BookingDate,
                    StartTime       = booking.StartTime,
                    EndTime         = booking.EndTime,
                    PaymentTotal    = booking.PaymentTotal,
                    PaymentMadeDate = booking.PaymentMadeDate,
                    NoCustomers     = booking.NoCustomers,
                    Comments        = booking.Comments,
                    Cancelled       = booking.Cancelled
                });
                await _db.SaveChangesAsync();

                if (booking.Tables != null)
                {
                    foreach (LibBookingService.Dtos.Table t in booking.Tables)
                    {
                        _db.TableBookings.Add(new TableBooking
                        {
                            Table_id   = t.Id,
                            Booking_id = newBoooking.Id
                        });
                        await _db.SaveChangesAsync();
                    }
                }

                if (booking.Payments != null)
                {
                    foreach (LibBookingService.Dtos.Payment p in booking.Payments)
                    {
                        _db.Payments.Add(new Payment
                        {
                            PaymentMethod_id = p.PaymentMethod.Id,
                            Customer_id      = p.CustomerId,
                            Booking_id       = p.BookingId,
                            Amount           = p.Amount,
                            Comments         = p.Comments
                        });
                        await _db.SaveChangesAsync();
                    }
                }

                if (booking.MenuItems != null)
                {
                    foreach (LibBookingService.Dtos.BookingMenuItem m in booking.MenuItems)
                    {
                        _db.BookingMenuItems.Add(new BookingMenuItem
                        {
                            Booking_id  = newBoooking.Id,
                            MenuItem_id = m.MenuItemId,
                            Quantity    = m.Quantity
                        });
                        await _db.SaveChangesAsync();
                    }
                }

                if (!isTesting)
                {
                    _db.Entry(newBoooking).Reload();
                }

                return(Request.CreateResponse(HttpStatusCode.OK, CreateBoookingFromDbBooking(newBoooking)));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Failed"));
            }
        }
Esempio n. 4
0
        public async Task <HttpResponseMessage> GetAvailableTable(LibBookingService.Dtos.Booking booking)
        {
            if (booking == null)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid booking model"));
            }

            IEnumerable <Booking> res = await _db.Bookings.Where(b => !b.Deleted && !b.Cancelled).ToListAsync();

            res = res.Where(b => b.Restaurant_id == booking.RestaurantId);
            res = res.Where(b => b.BookingDate.Date == booking.BookingDate.Date);

            if (!res.Any())
            {
                IEnumerable <Table> restaurantTables = _db.Tables.Where(t => t.Restaurant_id == booking.RestaurantId).OrderBy(t => t.NoSeats);
                restaurantTables = restaurantTables.Where(t => !t.Deleted && t.Active);
                restaurantTables = restaurantTables.Where(t => t.NoSeats >= booking.NoCustomers);

                if (restaurantTables.Any())
                {
                    Table restaurantTable = restaurantTables.FirstOrDefault();

                    return(Request.CreateResponse(HttpStatusCode.OK, new LibBookingService.Dtos.Table
                    {
                        Id = restaurantTable.Id,
                        RestaurantId = restaurantTable.Restaurant_id,
                        AdditionalNotes = restaurantTable.AdditionalNotes,
                        NoSeats = restaurantTable.NoSeats,
                        TableNo = restaurantTable.TableNo
                    }));
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NoContent, "No tables available"));
                }
            }

            if (booking.StartTime != null && booking.EndTime != null)
            {
                res = res.Where(b =>
                                (TimeSpan.Compare(booking.StartTime, b.StartTime) == 1 && TimeSpan.Compare(booking.StartTime, b.EndTime) == -1) ||
                                (TimeSpan.Compare(booking.EndTime, b.StartTime) == 1 && TimeSpan.Compare(booking.EndTime, b.EndTime) == -1) ||
                                (booking.StartTime == b.StartTime && booking.EndTime == b.EndTime)
                                );

                IEnumerable <Table> resTables = res.SelectMany(b => b.TableBookings.Select(bb => bb.Table));

                IEnumerable <Table> restaurantTables = _db.Tables.Where(t => t.Restaurant_id == booking.RestaurantId).OrderBy(t => t.NoSeats);
                restaurantTables = restaurantTables.Where(t => !t.Deleted && t.Active);
                restaurantTables = restaurantTables.Where(t => t.NoSeats >= booking.NoCustomers);

                List <Table> result = new List <Table>();

                foreach (var t in restaurantTables)
                {
                    if (resTables.Where(tt => tt.Id == t.Id).FirstOrDefault() == null)
                    {
                        result.Add(t);
                    }
                }

                if (result.Any())
                {
                    return(Request.CreateResponse(HttpStatusCode.OK, new LibBookingService.Dtos.Table
                    {
                        Id = result.First().Id,
                        RestaurantId = result.First().Restaurant_id,
                        AdditionalNotes = result.First().AdditionalNotes,
                        NoSeats = result.First().NoSeats,
                        TableNo = result.First().TableNo
                    }));
                }
            }

            return(Request.CreateErrorResponse(HttpStatusCode.NoContent, "No tables available"));
        }