public async Task <HttpResponseMessage> BookCalandar(CalendarInputForBooking inputForRoomBooking)
        {
            CalendarInput            input  = new CalendarInput();
            CalendarOutputForBooking output = new CalendarOutputForBooking();

            input.BookingSlots = inputForRoomBooking.BookingSlots.Select(
                q => new Slot()
            {
                StartDateTime = q.StartDateTime,
                EndDateTime   = q.EndDateTime
            }).ToList();
            input.Capacity = inputForRoomBooking.Capacity;
            input.FloorID  = inputForRoomBooking.FloorID;
            input.UserId   = inputForRoomBooking.UserId;
            input.Password = inputForRoomBooking.Password;

            GetFloorAndRooms       getFloorAndRooms   = new GetFloorAndRooms();
            IList <CalendarOutput> calendarOutputList = getFloorAndRooms.GetRoomsAvailabilityByCalendateInput(System.Configuration.ConfigurationManager.AppSettings["Connection"], input);

            if (calendarOutputList.Count > 0 && !(calendarOutputList.Any(t => t.IsAvailable == false)))
            {
                output = getFloorAndRooms.BookRooms(System.Configuration.ConfigurationManager.AppSettings["Connection"], inputForRoomBooking);
            }
            else
            {
                output.Message = "Conflict occured for provided slots against system bookings.";
            }

            return(Request.CreateResponse(HttpStatusCode.OK, output));
        }
Exemple #2
0
        public CalendarOutputForBooking BookRooms(string connKey, CalendarInputForBooking input)
        {
            CalendarOutputForBooking calendarOutputForBooking = new CalendarOutputForBooking();
            SqlConnection            connection = new SqlConnection(SqlHelper.GetDBConnectionString(connKey));

            try
            {
                IList <Room>    lstRooms = GetRoomsByFloorID(connKey, input.FloorID);
                ExchangeService service  = GetExchangeService(input.UserId, input.Password);

                DateTime startDate = input.BookingSlots.OrderBy(t => t.StartDateTime).FirstOrDefault().StartDateTime.Date;
                DateTime endtDate  = input.BookingSlots.OrderByDescending(t => t.StartDateTime).FirstOrDefault().StartDateTime.Date;

                string strSQL          = @"INSERT INTO BookedMeeting (UserID,StartDateTime,EndDateTime,Description,IsConfirmed) output INSERTED.ID VALUES(@UserID,@StartDateTime,@EndDateTime,@Description,@IsConfirmed)";
                int    bookedMeetingID = 0;
                using (TransactionScope scope = new TransactionScope())
                {
                    using (SqlCommand theSQLCommand = new SqlCommand(strSQL, connection))
                    {
                        theSQLCommand.Parameters.AddWithValue("@UserID", input.UserId);
                        theSQLCommand.Parameters.AddWithValue("@StartDateTime", startDate);
                        theSQLCommand.Parameters.AddWithValue("@EndDateTime", endtDate);
                        theSQLCommand.Parameters.AddWithValue("@Description", input.Subject);
                        theSQLCommand.Parameters.AddWithValue("@IsConfirmed", true);
                        if (theSQLCommand.Connection.State == ConnectionState.Closed)
                        {
                            theSQLCommand.Connection.Open();
                        }
                        bookedMeetingID = (int)theSQLCommand.ExecuteScalar();
                    }

                    List <SlotForBooking> recurrSlots = ExtractGroupsBasedOnRoomAndStartEndTime(input.BookingSlots, input.RecurrenceType);

                    foreach (SlotForBooking slot in recurrSlots)
                    {
                        Room   room        = lstRooms.Where(t => t.Id == slot.RoomID).FirstOrDefault();
                        string strSQLInner = @"INSERT INTO Recurrence (RoomID,BookedMeetingID,StartDateTime,EndDateTime,IsConfirmed) VALUES(@RoomID,@BookedMeetingID,@StartDateTime,@EndDateTime,@IsConfirmed)";

                        using (SqlCommand theSQLCommandInner = new SqlCommand(strSQLInner, connection))
                        {
                            theSQLCommandInner.Parameters.AddWithValue("@RoomID", room.Id);
                            theSQLCommandInner.Parameters.AddWithValue("@BookedMeetingID", bookedMeetingID);
                            theSQLCommandInner.Parameters.AddWithValue("@StartDateTime", slot.StartDateTime);
                            theSQLCommandInner.Parameters.AddWithValue("@EndDateTime", slot.EndDateTime);
                            try
                            {
                                if (BookAppointment(service, slot, input, room))
                                {
                                    theSQLCommandInner.Parameters.AddWithValue("@IsConfirmed", true);
                                }
                                else
                                {
                                    theSQLCommandInner.Parameters.AddWithValue("@IsConfirmed", false);
                                }
                                if (theSQLCommandInner.Connection.State == ConnectionState.Closed)
                                {
                                    theSQLCommandInner.Connection.Open();
                                }
                                theSQLCommandInner.ExecuteNonQuery();
                            }
                            catch (Microsoft.Exchange.WebServices.Data.ServiceRequestException ex)
                            {
                                calendarOutputForBooking.ErrorSlots.Add(new SlotForBooking()
                                {
                                    Message       = string.Format("User do not have access on room '{0}'", room.Name),
                                    StartDateTime = slot.StartDateTime,
                                    EndDateTime   = slot.EndDateTime,
                                    RoomID        = slot.RoomID
                                });
                                calendarOutputForBooking.Message = string.Format("User do not have access on room '{0}'", room.Name);
                            }
                            catch (Exception ex)
                            {
                                calendarOutputForBooking.ErrorSlots.Add(new SlotForBooking()
                                {
                                    Message       = string.Format("Error occured to book meeting for room '{0}'", room.Name),
                                    StartDateTime = slot.StartDateTime,
                                    EndDateTime   = slot.EndDateTime,
                                    RoomID        = slot.RoomID
                                });
                                calendarOutputForBooking.Message = string.Format("Error occured to book meeting for room '{0}'", room.Name);
                            }
                        }
                    }
                    if (calendarOutputForBooking.ErrorSlots != null && calendarOutputForBooking.ErrorSlots.Count() <= 0)
                    {
                        scope.Complete();
                    }
                }
            }
            catch (Exception ex)
            {
                calendarOutputForBooking.Message = "Error occured during meeting booking";
            }
            finally
            {
                if (connection.State != ConnectionState.Closed)
                {
                    connection.Close();
                }
            }
            return(calendarOutputForBooking);
        }