コード例 #1
0
ファイル: BookingManagement.cs プロジェクト: esfdk/itubs
        public RequestStatus AddCateringToBooking(string token, CateringChoice cateringChoice, out Booking editedBooking)
        {
            try
            {
                editedBooking = null;

                if (string.IsNullOrWhiteSpace(token) || cateringChoice == null)
                {
                    return RequestStatus.InvalidInput;
                }

                var p = Person.GetByToken(token);
                editedBooking = Booking.GetBookingByID(cateringChoice.BookingID);
                if (editedBooking == null)
                {
                    return RequestStatus.InvalidInput;
                }

                if (p != null)
                {
                    if (p.ID.Equals(editedBooking.PersonID))
                    {
                        return editedBooking.AddCatering(cateringChoice);
                    }
                }
                return RequestStatus.AccessDenied;
            }

            catch (DbEntityValidationException e)
            {
                System.IO.StreamWriter file = new System.IO.StreamWriter(@"c:\RentItServices\RentIt12\Stuff\test.txt");
                foreach (var eve in e.EntityValidationErrors)
                {
                    file.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        file.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                    }
                    file.Flush();
                }
                editedBooking = null;
                return RequestStatus.AccessDenied;
            }
        }
コード例 #2
0
ファイル: BookingManagement.cs プロジェクト: esfdk/itubs
        public RequestStatus AddEquipmentToBooking(string token, EquipmentChoice equipmentChoice, out Booking editedBooking)
        {
            editedBooking = null;

            if (string.IsNullOrWhiteSpace(token) || equipmentChoice == null)
            {
                return RequestStatus.InvalidInput;
            }

            var p = Person.GetByToken(token);
            var b = Booking.GetBookingByID(equipmentChoice.BookingID);
            if (b == null)
            {
                return RequestStatus.InvalidInput;
            }

            if (p == null || (!p.IsAdmin() && p.ID != b.PersonID))
            {
                return RequestStatus.AccessDenied;
            }

            editedBooking = Booking.GetBookingByID(equipmentChoice.BookingID);
            return editedBooking != null ? editedBooking.AddEquipment(equipmentChoice) : RequestStatus.InvalidInput;
        }
コード例 #3
0
ファイル: BookingManagement.cs プロジェクト: esfdk/itubs
        /// <summary>
        /// Changes the status of a booking.
        /// </summary>
        /// <param name="token">The token of the user.</param>
        /// <param name="changedBooking">The booking which status is to be changed.</param>
        /// <returns>The status of the request.</returns>
        public RequestStatus ChangeBookingStatus(string token, ref Booking changedBooking)
        {
            if (string.IsNullOrWhiteSpace(token) || changedBooking == null)
            {
                return RequestStatus.Error;
            }

            var p = Person.GetByToken(token);
            var booking = Booking.GetBookingByID(changedBooking.ID);
            if (booking == null)
            {
                return RequestStatus.InvalidInput;
            }

            if (p == null || !p.IsAdmin())
            {
                return RequestStatus.AccessDenied;
            }

            var rs = booking.ChangeStatus(changedBooking);
            changedBooking = booking;
            return rs;
        }
コード例 #4
0
ファイル: BookingManagement.cs プロジェクト: esfdk/itubs
        public RequestStatus RemoveEquipmentChoice(string token, EquipmentChoice equipmentChoice, out Booking editedBooking)
        {
            editedBooking = null;

            if (string.IsNullOrWhiteSpace(token) || equipmentChoice == null)
            {
                return RequestStatus.InvalidInput;
            }

            var p = Person.GetByToken(token);
            var ec = EquipmentChoice.GetEquipmentChoiceByID(equipmentChoice.ID);
            if (ec == null)
            {
                return RequestStatus.InvalidInput;
            }

            if (p == null || (!p.IsAdmin() && p.ID != ec.Booking.PersonID))
            {
                return RequestStatus.AccessDenied;
            }

            var rs = ec.Remove();
            editedBooking = Booking.GetBookingByID(equipmentChoice.BookingID);
            return rs;
        }
コード例 #5
0
ファイル: BookingManagement.cs プロジェクト: esfdk/itubs
        public RequestStatus RemoveCateringChoice(string token, CateringChoice cateringChoice, out Booking editedBooking)
        {
            editedBooking = null;

            if (string.IsNullOrWhiteSpace(token) || cateringChoice == null)
            {
                return RequestStatus.InvalidInput;
            }

            var p = Person.GetByToken(token);
            var cc = CateringChoice.GetCateringChoice(cateringChoice.ID);
            if (cc == null)
            {
                return RequestStatus.InvalidInput;
            }

            if (p == null || (!p.IsAdmin() && p.ID != cc.Booking.PersonID))
            {
                return RequestStatus.AccessDenied;
            }

            var rs = cc.Remove();
            editedBooking = Booking.GetBookingByID(cateringChoice.ID);
            return rs;
        }
コード例 #6
0
ファイル: BookingManagement.cs プロジェクト: esfdk/itubs
        /// <summary>
        /// Gets a booking.
        /// </summary>
        /// <param name="booking">The booking to get. Must not be null.</param>
        /// <returns>The status of the request.</returns>
        public RequestStatus GetBooking(ref Booking booking)
        {
            if (booking == null)
            {
                return RequestStatus.InvalidInput;
            }

            booking = Booking.GetBookingByID(booking.ID);
            return booking != null ? RequestStatus.Success : RequestStatus.InvalidInput;
        }
コード例 #7
0
ファイル: BookingManagement.cs プロジェクト: esfdk/itubs
        /// <summary>Creates a new booking.</summary>
        /// <param name="token">The token of the user.</param>
        /// <param name="newBooking">The booking to create.</param>
        /// <returns>The status of the request.</returns>
        public RequestStatus CreateBooking(string token, ref Booking newBooking)
        {
            if (string.IsNullOrWhiteSpace(token) || newBooking == null)
            {
                return RequestStatus.InvalidInput;
            }

            var p = Person.GetByToken(token);
            if (p == null)
            {
                return RequestStatus.AccessDenied;
            }

            if (!p.IsAdmin() && (p.ID != newBooking.PersonID && !newBooking.Status.Equals("Pending")))
            {
                return RequestStatus.AccessDenied;
            }

            newBooking.PersonID = p.ID;

            newBooking = Booking.CreateNewBooking(newBooking);

            return newBooking != null ? RequestStatus.Success : RequestStatus.InvalidInput;
        }
コード例 #8
0
ファイル: Booking.cs プロジェクト: esfdk/itubs
 /// <summary>
 /// Finds all the bookings that overlap with a specific booking.
 /// </summary>
 /// <param name="booking">The booking to check.</param>
 /// <returns>The list of bookings that over with input booking. If empty, no bookings overlap.</returns>
 private static List<Booking> OverlappingBookings(Booking booking)
 {
     return All.Where(
                 b =>
                 b.RoomID == booking.RoomID
                 &&
                 ((b.StartTime <= booking.StartTime && b.EndTime > booking.StartTime)        /* Any starting before or same time as booking &
                                                                                             * ends after start time of booking. */
                  || (b.StartTime >= booking.StartTime && b.EndTime <= booking.EndTime)      /* Any starting after or same time as booking &
                                                                                             * ends after or same time as booking. */
                  || (b.StartTime <= booking.StartTime && b.EndTime >= booking.EndTime)      /* Any starting before or same time as booking &
                                                                                             * ends same time or after booking. */
                  || (b.StartTime >= booking.StartTime && b.StartTime < booking.EndTime)))   /* Any starting after or same time as booking &
                                                                                             * starting before end time of booking. */
         .ToList();
 }
コード例 #9
0
ファイル: Booking.cs プロジェクト: esfdk/itubs
 /// <summary>
 /// Checks if time of a booking is within acceptable time.
 /// </summary>
 /// <param name="booking">Booking to check.</param>
 /// <returns>True if time of booking is valid, false if not.</returns>
 private static bool IsBookingTimeValid(Booking booking)
 {
     return booking.StartTime < booking.EndTime
            && booking.StartTime.Hour >= Configuration.EarliestBooking.Hour
            && booking.EndTime.Hour <= Configuration.LatestBooking.Hour
            && booking.StartTime.Date.Equals(booking.EndTime.Date);
 }
コード例 #10
0
ファイル: Booking.cs プロジェクト: esfdk/itubs
        public RequestStatus ChangeTime(Booking changedBooking)
        {
            if (this.ID == changedBooking.ID
                && this.RoomID == changedBooking.RoomID
                && this.PersonID == changedBooking.PersonID
                && IsBookingTimeValid(changedBooking))
            {
                var overlapping = OverlappingBookings(changedBooking);
                if (overlapping.Count > 0 && overlapping.All(b => b.PersonID == changedBooking.PersonID))
                {
                    var updatedBooking = overlapping.First();
                    updatedBooking.StartTime = changedBooking.StartTime;
                    updatedBooking.EndTime = changedBooking.EndTime;
                    foreach (var b in overlapping)
                    {
                        foreach (var ec in b.EquipmentChoices)
                        {
                            ec.BookingID = updatedBooking.ID;
                        }

                        foreach (var cc in b.CateringChoices)
                        {
                            cc.BookingID = updatedBooking.ID;
                        }
                    }

                    foreach (var b in overlapping.Where(b => b.ID != updatedBooking.ID))
                    {
                        BookITContext.Db.Bookings.Remove(b);
                    }

                    BookITContext.Db.SaveChanges();
                    return RequestStatus.Success;
                }

                if (overlapping.Count == 0)
                {
                    this.StartTime = changedBooking.StartTime;
                    this.EndTime = changedBooking.EndTime;

                    foreach (var ec in this.EquipmentChoices)
                    {
                        BookITContext.Db.EquipmentChoices.Remove(ec);
                    }

                    foreach (var cc in this.CateringChoices)
                    {
                        BookITContext.Db.CateringChoices.Remove(cc);
                    }

                    BookITContext.Db.SaveChanges();
                    return RequestStatus.Success;
                }

                return RequestStatus.InvalidInput;
            }

            return RequestStatus.InvalidInput;
        }
コード例 #11
0
ファイル: Booking.cs プロジェクト: esfdk/itubs
        public RequestStatus ChangeStatus(Booking changedBooking)
        {
            if (!string.IsNullOrWhiteSpace(changedBooking.Status))
            {
                if (changedBooking.Status.Equals("Approved"))
                {
                    this.Status = "Approved";
                    BookITContext.Db.SaveChanges();
                    return RequestStatus.Success;
                }

                return changedBooking.Status.Equals("Rejected") ? this.Remove() : RequestStatus.InvalidInput;
            }

            return RequestStatus.InvalidInput;
        }
コード例 #12
0
ファイル: Booking.cs プロジェクト: esfdk/itubs
        /// <summary>Creates a new booking in the database.</summary>
        /// <param name="booking">The booking to create.</param>
        /// <returns>The newly created booking object.</returns>
        public static Booking CreateNewBooking(Booking booking)
        {
            // Gets the room that the booking is for.
            var room = Room.GetRoomByID(booking.RoomID);

            // Checks if input is valid
            if (!IsBookingTimeValid(booking) || OverlappingBookings(booking).Any()
                || (booking.NumberOfParticipants > room.MaxParticipants)
                || !IsAStatus(booking.Status)
                || booking.PersonID <= 0
                || booking.RoomID <= 0)
            {
                return null;
            }

            // Adds a new booking object (with the input parameters) to the Bookings collection
            BookITContext.Db.Bookings.Add(
                new Booking
                    {
                        PersonID = booking.PersonID,
                        RoomID = booking.RoomID,
                        StartTime = booking.StartTime,
                        EndTime = booking.EndTime,
                        Comments = booking.Comments,
                        NumberOfParticipants = booking.NumberOfParticipants,
                        Status = booking.Status,
                    });

            // Saves the change to the database
            BookITContext.Db.SaveChanges();

            // Finds the booking that was just created (to make sure the object is fully updated).
            return
                All.FirstOrDefault(
                    b =>
                    b.NumberOfParticipants == booking.NumberOfParticipants && b.PersonID == booking.PersonID
                    && b.RoomID == booking.RoomID && b.Status.Equals(booking.Status)
                    && b.Comments.Equals(booking.Comments) && b.StartTime.Equals(booking.StartTime)
                    && b.EndTime.Equals(booking.EndTime));
        }