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));
        }
        public JsonResult BookAppointments(CalendarInputForBooking info)
        {
            BookingResponse output = new BookingResponse();

            if ((Session["floors"] as System.Collections.Generic.IList <CalendarOutput>).Any(i => i.IsAvailable == false))
            {
                output.Errors.Add("For booking all slots whould be available. Please re-check availability.");
            }
            if (string.IsNullOrWhiteSpace(info.Subject))
            {
                output.Errors.Add("Appointment title is required field.");
            }
            if (string.IsNullOrWhiteSpace(Convert.ToString(Session["UserName"])))
            {
                output.Errors.Add("Username cannot be empty.");
            }
            if (!Request.IsAuthenticated)
            {
                output.Errors.Add("Invalid Request.");
            }
            if (!(Session["floors"] != null && (Session["floors"] is System.Collections.Generic.IList <CalendarOutput>) && (Session["floors"] as System.Collections.Generic.IList <CalendarOutput>).Count > 0))
            {
                output.Errors.Add("Session Expired.");
            }

            if (output.Errors.Count > 0)
            {
                return(Json(output, JsonRequestBehavior.AllowGet));
            }

            using (var client = new HttpClient())
            {
                string apiURL = ConfigurationManager.AppSettings["APIRefenenceURL"];
                info.UserId   = (string)Session["UserName"];
                info.Password = (string)Session["Password"];

                foreach (CalendarOutput item in (Session["floors"] as System.Collections.Generic.IList <CalendarOutput>))
                {
                    info.BookingSlots.Add(new SlotForBooking()
                    {
                        StartDateTime = item.BookingSlot.StartDateTime, EndDateTime = item.BookingSlot.EndDateTime, RoomID = item.RoomId
                    });
                }

                Task <HttpResponseMessage> response = client.PostAsJsonAsync(apiURL + "BookCalandar", info);
                response.Wait();
                if (response.Result.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var floorJsonString          = response.Result.Content.ReadAsStringAsync().Result;
                    var calendarOutputForBooking = JsonConvert.DeserializeObject <CalendarOutputForBooking>(floorJsonString);
                    output.Output = calendarOutputForBooking;

                    return(Json(output, JsonRequestBehavior.AllowGet));
                }
            }

            output.Errors.Add("Session Expired.");
            return(Json(output, JsonRequestBehavior.AllowGet));
        }
Exemple #3
0
        private bool BookAppointment(ExchangeService service, SlotForBooking slot, CalendarInputForBooking input, Room objRoom)
        {
            Appointment meeting = new Appointment(service);

            // Set the properties on the meeting object to create the meeting.
            meeting.Subject  = input.Subject;
            meeting.Body     = input.Subject;
            meeting.Location = objRoom.Name;
            meeting.Start    = slot.StartDateTime;
            DateTime startDateWithEndTime = DateTime.Parse(slot.StartDateTime.ToString(dateFormat) + slot.EndDateTime.ToString(timeFormat));

            meeting.End = startDateWithEndTime;
            meeting.RequiredAttendees.Add(objRoom.Email);
            if (input.RecipientsTo != null)
            {
                foreach (var email in input.RecipientsTo)
                {
                    meeting.RequiredAttendees.Add(email);
                }
            }
            if (input.RecipientsCC != null)
            {
                foreach (var email in input.RecipientsCC)
                {
                    meeting.RequiredAttendees.Add(email);
                }
            }
            meeting.ReminderMinutesBeforeStart = input.ReminderMinutesBeforeStart;


            if (input.RecurrenceType == "DailyEveryDay" || input.RecurrenceType == "DailyEveryWorkingDay" || input.RecurrenceType == "DailyEveryNDay" || input.RecurrenceType == "Weekly" || input.RecurrenceType == "Monthly")
            {
                if (input.RecurrenceType == "DailyEveryDay" || input.RecurrenceType == "DailyEveryNDay")
                {
                    meeting.Recurrence           = new Recurrence.DailyPattern(meeting.Start.Date, input.DailyNDayInterval);
                    meeting.Recurrence.StartDate = slot.StartDateTime.Date;
                    meeting.Recurrence.EndDate   = slot.EndDateTime.Date;
                }
                else if (input.RecurrenceType == "DailyEveryWorkingDay")
                {
                    meeting.Recurrence           = new Recurrence.WeeklyPattern(meeting.Start.Date, 1, new DayOfTheWeek[] { DayOfTheWeek.Monday, DayOfTheWeek.Tuesday, DayOfTheWeek.Wednesday, DayOfTheWeek.Thursday, DayOfTheWeek.Friday });
                    meeting.Recurrence.StartDate = slot.StartDateTime.Date;
                    meeting.Recurrence.EndDate   = slot.EndDateTime.Date;
                }
                else if (input.RecurrenceType == "Weekly")
                {
                    meeting.Recurrence           = new Recurrence.WeeklyPattern(meeting.Start.Date, 1, input.DayofWeeksForWeekly);
                    meeting.Recurrence.StartDate = slot.StartDateTime.Date;
                    meeting.Recurrence.EndDate   = slot.EndDateTime.Date;
                }
                else if (input.RecurrenceType == "Monthly" && input.DayOfMonth_Month > 0 && input.DayOfMonthInterval_Month > 0)
                {
                    meeting.Recurrence           = new Recurrence.MonthlyPattern(meeting.Start.Date, input.DayOfMonthInterval_Month, input.DayOfMonth_Month);
                    meeting.Recurrence.StartDate = slot.StartDateTime.Date;
                    meeting.Recurrence.EndDate   = slot.EndDateTime.Date;
                }
                else if (input.RecurrenceType == "Monthly" && input.CustomMonthInterval_Month > 0)
                {
                    meeting.Recurrence           = new Recurrence.RelativeMonthlyPattern(meeting.Start.Date, input.CustomMonthInterval_Month, (DayOfTheWeek)input.DayOfTheWeek_Month, (DayOfTheWeekIndex)input.DayOfTheWeekIndex_Month);
                    meeting.Recurrence.StartDate = slot.StartDateTime.Date;
                    meeting.Recurrence.EndDate   = slot.EndDateTime.Date;
                }
            }

            // Save the meeting to the Calendar folder and send the meeting request.
            meeting.Save(SendInvitationsMode.SendToAllAndSaveCopy);

            // Verify that the meeting was created.
            Item item = Item.Bind(service, meeting.Id, new PropertySet(ItemSchema.Subject));

            if (item != null && !string.IsNullOrWhiteSpace(item.Subject))
            {
                return(true);
            }
            return(false);
        }
Exemple #4
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);
        }