//
        // GET: /CalendarEvent/Delete?calendarId={calendarId}&eventId={eventId}

        public ActionResult Delete(string calendarId, string eventId)
        {
            var authenticator = GetAuthenticator();
            var service       = new GoogleCalendarServiceProxy(authenticator);

            service.DeleteEvent(calendarId, eventId);

            return(RedirectToAction("Index", "Home"));
        }
        public ActionResult Edit(CalendarEvent calendarEvent)
        {
            if (ModelState.IsValid)
            {
                var authenticator = GetAuthenticator();
                var service       = new GoogleCalendarServiceProxy(authenticator);

                service.UpdateEvent(calendarEvent);
            }

            return(RedirectToAction("Index", "Home"));
        }
        //
        // GET: /CalendarEvent/Edit?calendarId={calendarId}&eventId={eventId}

        public ActionResult Edit(string calendarId, string eventId)
        {
            var authenticator = GetAuthenticator();
            var service       = new GoogleCalendarServiceProxy(authenticator);
            var model         = service.GetEvent(calendarId, eventId);
            var colorList     = Enum.GetValues(typeof(GoogleEventColors)).Cast <GoogleEventColors>()
                                .Select(v => new SelectListItem {
                Text = v.ToString(), Value = ((int)v).ToString()
            });

            ViewBag.Colors = new SelectList(colorList, "Value", "Text");
            return(View(model));
        }
Example #4
0
        public ActionResult Create(CalendarEvent calendarEvent)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index", "Event"));
            }
            var authenticator = GetAuthenticator();
            var service       = new GoogleCalendarServiceProxy(authenticator);

            calendarEvent.Attendees = new List <string>
            {
                calendarEvent.GuestEmailId
            };

            calendarEvent.CalendarId = "*****@*****.**";
            service.CreateEvent(calendarEvent);

            return(RedirectToAction("Index", "Event"));
        }
Example #5
0
        public ActionResult ListEvents(DateTime startDate, DateTime endDate)
        {
            UserProfile userProfile = null;

            using (var context = new CalSiteDbContext())
            {
                userProfile = context.UserProfiles.FirstOrDefault(c => c.UserName == User.Identity.Name);
            }

            if (userProfile == null)
            {
                return(RedirectToAction("Register", "Account"));
            }

            var authenticator = GetAuthenticator();

            var service = new GoogleCalendarServiceProxy(authenticator);
            var model   = service.GetEvents(userProfile.Email, startDate, endDate);

            ViewBag.StartDate = startDate.ToShortDateString();
            ViewBag.EndDate   = endDate.ToShortDateString();
            return(View("Index", model));
        }
        public IHttpActionResult Post(AvailabilityEvent availabilityEvent)
        {
            User userProfile = null;

            using (var context = new CalendarContext())
            {
                userProfile = context.Users.FirstOrDefault(c => c.UserName == User.Identity.Name);
            }

            var refreshToken = new CalendarContext().GoogleRefreshTokens
                               .FirstOrDefault(c => c.UserName == User.Identity.Name)
                               ?.RefreshToken;
            var authenticator = GoogleAuthorizationHelper.RefreshAuthenticator(refreshToken);

            var service = new GoogleCalendarServiceProxy(authenticator);


            var model = service.GetEvents(userProfile.Email, availabilityEvent.StartDate);

            model = model
                    .Where(x => x.StartDate >= availabilityEvent.StartDate && x.StartDate <= availabilityEvent.StartDate)
                    .OrderBy(y => y.StartDate).ToList();
            //var availability = 3;
            var      index     = 0;
            var      startTime = DateTime.Today;
            DateTime?endTime   = null;
            var      diff      = 0;

            //var model1 = new List<CalendarEvent>();

            //model1.Add(new CalendarEvent
            //{
            //    Title = "Kashin standup meeting",
            //    StartDate = DateTime.Parse("2021-10-07 12:00:00"),
            //    EndDate = DateTime.Parse("2021-10-07 12:30:00"),

            //});

            //model1.Add(new CalendarEvent
            //{
            //    Title = "Kashin standup meeting1",
            //    StartDate = DateTime.Parse("2021-10-07 16:00:00"),
            //    EndDate = DateTime.Parse("2021-10-07 16:10:00"),

            //});

            //model1.Add(new CalendarEvent
            //{
            //    Title = "Kashin standup meeting2",
            //    StartDate = DateTime.Parse("2021-10-07 22:00:00"),
            //    EndDate = DateTime.Parse("2021-10-07 23:30:00"),

            //});

            //model1.Add(new CalendarEvent
            //{
            //    Title = "Kashin standup meeting3",
            //    StartDate = DateTime.Parse("2021-10-07 11:00:00"),
            //    EndDate = DateTime.Parse("2021-10-07 12:00:00"),

            //});

            foreach (var item in model)
            {
                if (index != 0)
                {
                    //item.EndDate = model
                }

                diff = Math.Abs((item.StartDate - startTime).Hours);

                if (diff >= availabilityEvent.Hour)
                {
                    // available
                    // starttime + 3
                    endTime = startTime.AddHours(3);

                    var calendarEvent = new CalendarEvent();
                    calendarEvent.Description = availabilityEvent.Description;
                    calendarEvent.Title       = availabilityEvent.Title;
                    calendarEvent.Location    = availabilityEvent.Location;
                    calendarEvent.Description = availabilityEvent.Description;
                    calendarEvent.StartDate   = startTime;
                    calendarEvent.EndDate     = (DateTime)endTime;
                    calendarEvent.Attendees   = new List <string>
                    {
                        availabilityEvent.GuestEmailId
                    };

                    calendarEvent.CalendarId = userProfile.Email;
                    service.CreateEvent(calendarEvent);

                    break;
                }

                startTime = item.EndDate;

                index++;
            }

            if (endTime == null)
            {
                endTime = startTime.AddHours(3);
            }

            return(Json(new { data = model }));
        }
Example #7
0
        public ActionResult Availability(AvailabilityEvent availabilityEvent)
        {
            if (!ModelState.IsValid)
            {
                return(View("Availability", "Event"));
            }
            User userProfile = null;

            using (var context = new CalendarContext())
            {
                userProfile = context.Users.FirstOrDefault(c => c.UserName == User.Identity.Name);
            }

            var authenticator = GetAuthenticator();
            var service       = new GoogleCalendarServiceProxy(authenticator);

            var model = service.GetEvents(userProfile.Email, availabilityEvent.StartDate);

            model = model.OrderBy(y => y.StartDate).ToList();
            //var availability = 3;
            var      index     = 0;
            var      startTime = availabilityEvent.StartDate.Date;
            DateTime?endTime   = null;
            var      diff      = 0;

            var calendarEvent = new CalendarEvent();

            calendarEvent.Description = availabilityEvent.Description;
            calendarEvent.Title       = availabilityEvent.Title;
            calendarEvent.Location    = availabilityEvent.Location;
            calendarEvent.Description = availabilityEvent.Description;
            calendarEvent.Attendees   = new List <string>
            {
                availabilityEvent.GuestEmailId
            };

            calendarEvent.CalendarId = userProfile.Email;

            var isError = true;

            foreach (var item in model)
            {
                diff = Math.Abs((item.StartDate - startTime).Hours);

                if (diff >= availabilityEvent.Hour)
                {
                    // available
                    // starttime + availability
                    endTime = startTime.AddHours(availabilityEvent.Hour);

                    calendarEvent.StartDate = startTime;
                    calendarEvent.EndDate   = (DateTime)endTime;

                    service.CreateEvent(calendarEvent);

                    isError = false;
                    break;
                }

                startTime = item.EndDate;
            }

            if (endTime == null)
            {
                endTime = startTime.AddHours(availabilityEvent.Hour);

                // create event

                calendarEvent.StartDate = startTime;
                calendarEvent.EndDate   = (DateTime)endTime;

                service.CreateEvent(calendarEvent);

                isError = false;
            }

            if (isError)
            {
                ViewBag.error = "We have not found availability. Please check availability and create the event";
            }
            else
            {
                ViewBag.success =
                    $"Successfully created event : start {startTime.ToString("g")} end {endTime.Value.ToString("g")}";
            }

            return(View());
        }