예제 #1
0
        public string CreateNewReservation([FromBody] string req)
        {
            var result   = String.Empty;
            var request  = new CreateNewReservationRequest();
            var response = new CreateNewReservationResponse();

            //int totalTableCount = Convert.ToInt16(ConfigurationManager.AppSettings["NumberOfTables"]);
            try
            {
                request = JsonConvert.DeserializeObject <CreateNewReservationRequest>(req);
                ReservationService service = new ReservationService();
                response = service.PerformCreateNewReservation(request);
            }
            catch (Exception ex)
            {
                response.MessageString   = ex.Message;
                response.MessageStatusID = (byte)EMessageStatus.Success;
            }

            result = JsonConvert.SerializeObject(response);

            return(result);
        }
        public CreateNewReservationResponse PerformCreateNewReservation(CreateNewReservationRequest request)
        {
            CreateNewReservationResponse response = new CreateNewReservationResponse();
            var createdDate = DateTime.Now;

            try
            {
                using (var db = new CuddlyWombatEntities())
                {
                    db.Configuration.LazyLoadingEnabled   = false;
                    db.Configuration.ProxyCreationEnabled = false;



                    var totalTables = (from t in db.Tables
                                       where t.IsAvailable == true
                                       select t).ToList();

                    int totalTableCount = totalTables.Count();

                    var customer = (from c in db.Customers
                                    where c.CustomerID == request.Reservation.CustomerID
                                    select c).FirstOrDefault();

                    if (customer == null)
                    {
                        response.MessageString   = "Cannot find the customer";
                        response.MessageStatusID = (byte)EMessageStatus.Fail;

                        return(response);
                    }

                    if (totalTableCount < request.Reservation.NumberOfTables)
                    {
                        response.MessageString   = "Maximum tables you can book is " + totalTableCount;
                        response.MessageStatusID = (byte)EMessageStatus.Fail;

                        return(response);
                    }

                    var allReservationsForGivenDate = (from r in db.Reservations
                                                       where r.ReservationDate == request.Reservation.ReservationDate
                                                       select r).ToList();

                    var allAvailableTablesForGivenDate = 0;
                    var allReservedTablesForGivenDate  = 0;

                    foreach (var r in allReservationsForGivenDate)
                    {
                        allReservedTablesForGivenDate = allReservedTablesForGivenDate + r.NumberOfTables;
                    }
                    allAvailableTablesForGivenDate = totalTableCount - allReservedTablesForGivenDate;

                    if (allAvailableTablesForGivenDate < request.Reservation.NumberOfTables)
                    {
                        response.MessageString   = "No available tables for the selected date, Please select a different date.";
                        response.MessageStatusID = (byte)EMessageStatus.Fail;
                        return(response);
                    }

                    var reservation = new Reservation()
                    {
                        ReservationID       = 0,
                        CustomerID          = request.Reservation.CustomerID,
                        ReservationStatusID = request.Reservation.ReservationStatusID,
                        ReservationDate     = request.Reservation.ReservationDate,
                        NumberOfTables      = request.Reservation.NumberOfTables,
                        DateCreated         = createdDate
                    };

                    db.Reservations.Add(reservation);
                    db.SaveChanges();

                    response.MessageString   = "Reservation successfully added";
                    response.MessageStatusID = (byte)EMessageStatus.Success;
                }
            }
            catch (Exception ex)
            {
                response.MessageStatusID = (byte)EMessageStatus.Exception;
                response.MessageString   = ex.Message;
            }

            return(response);
        }