/// <summary> /// Adds a booking to the database /// </summary> /// <param name="booking">The booking to be added</param> public void AddBooking(Booking booking) { var db = new ReScrumEntities(); var slot = db.Slots.Where(s => s.SlotId == booking.Slot.SlotId).FirstOrDefault(); var user = db.Users.Where(u => u.UserId == booking.User.UserId).FirstOrDefault(); //check if a current booking exist for the user, date and time slot var newBooking = db.Booking.Where(b => b.User.UserId == user.UserId && b.Slot.SlotId == slot.SlotId && b.Date == booking.Date).FirstOrDefault(); //if not create a new booking parameter if (newBooking == null) { newBooking = new DataLayer.Models.Booking(); } //set/update the booking values newBooking.Date = booking.Date; newBooking.Slot = slot; newBooking.Resource = db.Resources.Where(r => r.ResourceId == booking.Resource.ResourceId).FirstOrDefault(); newBooking.User = user; newBooking.BookedBy = user; //if it is a new booking add it (creating new guid id), else the exisitng booking will be updated => no double booking if (newBooking.BookingId == null) { db.Booking.Add(newBooking); } db.SaveChanges(); }
/// <summary> /// Adds a block of bookings to the database /// </summary> /// <param name="startDate">The start date</param> /// <param name="endDate">The end date</param> /// <param name="startTime">The start time</param> /// <param name="endTime">The end time</param> /// <param name="resourceId">The resource</param> /// <param name="userId">The user</param> public void AddBlockBooking(DateTime startDate, DateTime endDate, Guid?startTime, Guid?endTime, Guid?resourceId, Guid?userId) { var db = new ReScrumEntities(); var startSlot = db.Slots.Where(s => s.SlotId == startTime).FirstOrDefault(); var endSlot = db.Slots.Where(s => s.SlotId == endTime).FirstOrDefault(); var user = db.Users.Where(u => u.UserId == userId).FirstOrDefault(); var resource = db.Resources.Where(r => r.ResourceId == resourceId).FirstOrDefault(); var slotList = db.Slots.Where(s => s.StartTime >= startSlot.StartTime && s.EndTime <= endSlot.EndTime).ToList(); var date = startDate; //for every date in the date range given while (date <= endDate) { foreach (DataLayer.Models.Slot slot in slotList) { //check if a booking exist for the user on the given date and time var booking = db.Booking.Where(b => b.User.UserId == user.UserId && b.Slot.SlotId == slot.SlotId && b.Date == date).FirstOrDefault(); //if no booking exists create a new booking object if (booking == null) { booking = new DataLayer.Models.Booking(); } //Add or update booking values booking.Date = date; booking.Slot = slot; booking.Resource = resource; booking.User = user; booking.BookedBy = user; //if a new booking was created add it to the database(giving a new guid id), else update the existing booking if (booking.BookingId == null) { db.Booking.Add(booking); } } date = date.AddDays(1); } db.SaveChanges(); }
/// <summary> /// Removed the unconformed booking and adds it to the booking table /// </summary> /// <param name="unconfirmedBookingId">The unconfirmed booking</param> public void ConfirmBooking(Guid?unconfirmedBookingId) { var db = new ReScrumEntities(); //get the unconfirmed booking var unconfirmedBooking = db.UnconfirmedBooking.Where(b => b.UnconfirmedBookingId == unconfirmedBookingId).FirstOrDefault(); //check to see if the user already has a booking for the date and time given var newBooking = db.Booking.Where(b => b.User.UserId == unconfirmedBooking.User.UserId && b.Slot.SlotId == unconfirmedBooking.Slot.SlotId && b.Date == unconfirmedBooking.Date).FirstOrDefault(); //if no booking exists create a new bookng object, else the exisitng booking will be updated if (newBooking == null) { newBooking = new DataLayer.Models.Booking(); } //add or update booking object newBooking.Date = unconfirmedBooking.Date; newBooking.Slot = unconfirmedBooking.Slot; newBooking.Resource = unconfirmedBooking.Resource; newBooking.User = unconfirmedBooking.User; newBooking.BookedBy = unconfirmedBooking.BookedBy; newBooking.GroupBooking = true; //if it is a new booking add it to the database (give a new guid id), else update the existing booking if (newBooking.BookingId == null) { db.Booking.Add(newBooking); } //Remove the unconfirmed booking db.UnconfirmedBooking.Remove(unconfirmedBooking); db.SaveChanges(); }
/// <summary> /// Adds bookings for the users for all of the time slots not already booked /// </summary> /// <param name="date">The start date</param> /// <param name="userId">The user</param> /// <returns>True if a resource is avaible and booking was successful, false otherwise</returns> public bool AutoBook(DateTime date, Guid?userId) { var db = new ReScrumEntities(); //get the user var user = db.Users.Where(u => u.UserId == userId).FirstOrDefault(); //get all active slots var slotList = db.Slots.Where(s => s.CancellationDate == null).ToList(); //find the end date var endDate = date.AddDays(4); //find the start time slot var startSlot = slotList.First(); foreach (DataLayer.Models.Slot slot in slotList) { if (startSlot.StartTime.CompareTo(slot.StartTime) == 1) { startSlot = slot; } } //find the end time slot var endSlot = slotList.Last(); foreach (DataLayer.Models.Slot slot in slotList) { if (endSlot.EndTime.CompareTo(slot.EndTime) == -1) { endSlot = slot; } } //find resource that is available all week var resourceList = GetAvailableResourcesForBlockBooking(date, endDate, startSlot.SlotId, endSlot.SlotId).ToList(); //get the resource with the smallest capacity if (resourceList.Count < 1) { return(false); } var resource = resourceList.OrderBy(r => r.Capacity).First(); //add a booking for every time slot that is not already booked in the week => does NOT override existing bookings while (date <= endDate) { foreach (DataLayer.Models.Slot slot in slotList) { var booking = db.Booking.Where(b => b.User.UserId == user.UserId && b.Slot.SlotId == slot.SlotId && b.Date == date).FirstOrDefault(); //Do not want to override existing bookings in auto functionality if (booking == null) { booking = new DataLayer.Models.Booking() { Date = date, Slot = slot, Resource = db.Resources.Where(r => r.ResourceId == resource.ResourceId).FirstOrDefault(), User = user, BookedBy = user, }; db.Booking.Add(booking); } } date = date.AddDays(1); } db.SaveChanges(); return(true); }