view Agendas/trunk/src/Agendas.Google/GCalendarAdapter.cs @ 135:80156ae1493f

Se agrega la eliminación de un evento cuando la acción sea Cancelar. Además se arregla la descripción de los eventos Alt.Net Café y Grupo de estudio debido a que los test se encontraban fallidos.
author alabra
date Sun, 17 Jul 2011 14:13:30 -0400
parents 0ea32a748453
children 734d3f0853bf
line wrap: on
line source

using System;
using Google.GData.Calendar;
using Google.GData.Extensions;

namespace AltNetHispano.Agendas.Google
{
    public class GCalendarAdapter : IGCalendarAdapter
    {
        private readonly CalendarService _service;
        private readonly Uri _feedUri;

        public bool CreateEvent(IGEventDetail gEventDetail, out string message)
        {
            try
            {
                var entry = new EventEntry
                {
                    Title = { Text = gEventDetail.Title },
                    Content = { Content = gEventDetail.Summary }
                };

                var eventLocation = new Where
                {
                    ValueString = gEventDetail.Location
                };

                entry.Locations.Add(eventLocation);

                var eventTime = new When(gEventDetail.StartEvent, gEventDetail.EndEvent);
                entry.Times.Add(eventTime);

                _service.Insert(_feedUri, entry);
                message = "Event create successful";
                return true;
            }
            catch (Exception exception)
            {
                message = exception.Message;
                return false;
            }
        }

        public bool DeleteEvent(DateTime startEvent, DateTime endEvent, out string message)
        {
            try
            {
                var myQuery = new EventQuery
                {
                    StartTime = startEvent,
                    EndTime = endEvent,
                    Uri = _feedUri
                };

                var myResultsFeed = _service.Query(myQuery);
                if (myResultsFeed.Entries.Count > 0)
                {
                    myResultsFeed.Entries[0].Delete();
                    message = "Event delete successful";
                    return true;
                }

                message = "Event not found";
                return false;
            }
            catch (Exception exception)
            {
                message = exception.Message;
                return false;
            }
        }

        public GCalendarAdapter(string applicationName ,string userName, string password, string calendarId)
        {
            _service = new CalendarService(applicationName);
            _service.setUserCredentials(userName, password);
            _feedUri = new Uri("https://www.google.com/calendar/feeds/" + calendarId + "/private/full");
        }
    }
}