Ejemplo n.º 1
0
        public IEnumerable <FacilityBookingBase> FacilityBookingGetByDate(FacilityBookingAdd newItem)
        {
            var o = ds.FacilityBookings.Where(j => j.BookedDate == newItem.BookedDate && j.Facility.Id == newItem.FacilityId);

            return((o == null) ? null : Mapper.Map <IEnumerable <FacilityBookingBase> >(o));
        }
        public IHttpActionResult Post([FromBody] FacilityBookingAdd newItem)
        {
            if (Request.GetRouteData().Values["id"] != null)
            {
                return(BadRequest("Invalid request URI"));
            }

            if (newItem == null)
            {
                return(BadRequest("Must send an entity body with the request"));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var facility = m.FacilityGetById(newItem.FacilityId);

            if (facility != null)
            {
                var facilityOpenHour  = facility.OpenTime.Value.Hour;
                var facilityOpenMin   = facility.OpenTime.Value.Minute;
                var facilityCloseHour = facility.CloseTime.Value.Hour;
                var facilityCloseMin  = facility.CloseTime.Value.Minute;

                var startTimeHour = newItem.StartTime.Value.Hour;
                var startTimeMin  = newItem.StartTime.Value.Minute;

                var endTimeHour = newItem.EndTime.Value.Hour;
                var endTimeMin  = newItem.EndTime.Value.Minute;

                if (startTimeHour < facilityOpenHour || startTimeHour > facilityCloseHour)
                {
                    return(Content(HttpStatusCode.Conflict, "The facility is closed at this start time"));
                }
                else if (startTimeHour == facilityOpenHour && startTimeMin < facilityOpenMin)
                {
                    return(Content(HttpStatusCode.Conflict, "The facility is closed at this start time"));
                }
                else if (endTimeHour > facilityCloseHour)
                {
                    return(Content(HttpStatusCode.Conflict, "The facility is closed at this end time"));
                }
                else if (endTimeHour == facilityCloseHour && endTimeMin > facilityCloseMin)
                {
                    return(Content(HttpStatusCode.Conflict, "The facility is closed at this end time"));
                }
            }

            var booking = m.FacilityBookingGetByDate(newItem);

            if (booking != null)
            {
                foreach (FacilityBookingBase book in booking)
                {
                    var startTimeHour = newItem.StartTime.Value.Hour;
                    var startTimeMin  = newItem.StartTime.Value.Minute;
                    var endTimeHour   = newItem.EndTime.Value.Hour;
                    var endTimeMin    = newItem.EndTime.Value.Minute;

                    var bookstartTimeHour = book.StartTime.Value.Hour;
                    var bookstartTimeMin  = book.StartTime.Value.Minute;
                    var bookendTimeHour   = book.EndTime.Value.Hour;
                    var bookendTimeMin    = book.EndTime.Value.Minute;

                    if (startTimeHour >= bookstartTimeHour && startTimeHour < bookendTimeHour)
                    {
                        return(Content(HttpStatusCode.Conflict, "There's a booking at this time"));
                    }
                    else if (startTimeHour < bookstartTimeHour && endTimeHour >= bookstartTimeHour)
                    {
                        return(Content(HttpStatusCode.Conflict, "There's a booking at this time"));
                    }

                    else if (startTimeHour < bookstartTimeHour && endTimeHour >= bookstartTimeHour)
                    {
                        return(Content(HttpStatusCode.Conflict, "There's a booking at this time"));
                    }
                    else if (startTimeHour >= bookendTimeHour && startTimeMin < bookendTimeMin)
                    {
                        return(Content(HttpStatusCode.Conflict, "There's a booking at this time"));
                    }
                }
            }

            var addedItem = m.FacilityBookingAdd(newItem);

            if (addedItem == null)
            {
                return(BadRequest("Cannot add the object"));
            }

            // HTTP 201
            var uri = Url.Link("DefaultApi", new { id = addedItem.Id });

            return(Created(uri, addedItem));
        }