Exemple #1
0
        public FacilityBookingBase FacilityBookingEdit(FacilityBookingEdit editedItem)
        {
            if (editedItem == null)
            {
                return(null);
            }

            var storedItem = ds.FacilityBookings.Find(editedItem.Id);

            if (storedItem == null)
            {
                return(null);
            }
            else
            {
                ds.Entry(storedItem).CurrentValues.SetValues(editedItem);
                ds.SaveChanges();

                return(Mapper.Map <FacilityBookingBase>(storedItem));
            }
        }
Exemple #2
0
        public IEnumerable <FacilityBookingBase> FacilityBookingGetByDateEdit(FacilityBookingEdit 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 Put(int id, [FromBody] FacilityBookingEdit editedItem)
        {
            if (editedItem == null)
            {
                return(BadRequest("Must send an entity body with the request"));
            }

            if (id != editedItem.Id)
            {
                return(BadRequest("Invalid data in the entity body"));
            }

            if (ModelState.IsValid)
            {
                var facility = m.FacilityGetById(editedItem.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 = editedItem.StartTime.Value.Hour;
                    var startTimeMin  = editedItem.StartTime.Value.Minute;

                    var endTimeHour = editedItem.EndTime.Value.Hour;
                    var endTimeMin  = editedItem.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.FacilityBookingGetByDateEdit(editedItem);
                if (booking != null)
                {
                    foreach (FacilityBookingBase book in booking)
                    {
                        if (book.Id != editedItem.Id)
                        {
                            if (editedItem.StartTime >= book.StartTime && editedItem.StartTime <= book.EndTime)
                            {
                                return(Content(HttpStatusCode.Conflict, "There's a booking at this time"));
                            }
                            else if (editedItem.StartTime < book.StartTime && editedItem.EndTime >= book.StartTime)
                            {
                                return(Content(HttpStatusCode.Conflict, "There's a booking at this time"));
                            }
                        }
                    }
                }

                var changedItem = m.FacilityBookingEdit(editedItem);

                if (changedItem == null)
                {
                    // HTTP 400
                    return(BadRequest("Cannot edit the object"));
                }
                else
                {
                    // HTTP 200
                    return(Ok(changedItem));
                }
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }