public bool DeleteEvent(Stream eventData)
        {
            // First validate the user priviledges
            var         reader        = new StreamReader(eventData);
            string      content       = reader.ReadToEnd();
            LCPostModel postDataModel = JsonConvert.DeserializeObject <LCPostModel>(content);

            if (validateUser(postDataModel) == false)
            {
                return(false);
            }
            using (dbEntities)
            {
                // Unpack the Event
                EventModel anEvent = JsonConvert.DeserializeObject <EventModel>(postDataModel.ContentData);
                // Check whether the event exists
                LCEvent eventToDelete = dbEntities.LCEvents.FirstOrDefault(e => e.EventId == anEvent.EventId);
                if (eventToDelete == null)
                {
                    return(false);
                }
                foreach (var creation in dbEntities.LCEventCreations.Where(creation => creation.EventId == eventToDelete.EventId))
                {
                    dbEntities.LCEventCreations.Remove(creation);
                }
                foreach (var participation in dbEntities.LCParticipations.Where(participation => participation.EventId == eventToDelete.EventId))
                {
                    dbEntities.LCParticipations.Remove(participation);
                }
                dbEntities.LCEvents.Remove(eventToDelete);
                dbEntities.SaveChanges();
            }
            return(true);
        }
        public string EventVote(Stream requestData)
        {
            var         reader        = new StreamReader(requestData);
            string      content       = reader.ReadToEnd();
            LCPostModel postDataModel = JsonConvert.DeserializeObject <LCPostModel>(content);

            if (validateUser(postDataModel) == false)
            {
                return(null);
            }
            else
            {
                EventModel eventModel = JsonConvert.DeserializeObject <EventModel>(postDataModel.ContentData);
                int        eventId    = eventModel.EventId;
                LCEvent    theEvent   = dbEntities.LCEvents.FirstOrDefault(e => e.EventId == eventId);
                if (theEvent == null)
                {
                    return(null);
                }
                string url = baseURI + "/event/vote/" + theEvent.EventGUID;
                return(JsonConvert.SerializeObject(url));
            }
        }
        public bool EditEvent(Stream eventData)
        {
            // First validate the user priviledges
            var         reader        = new StreamReader(eventData);
            string      content       = reader.ReadToEnd();
            LCPostModel postDataModel = JsonConvert.DeserializeObject <LCPostModel>(content);

            if (validateUser(postDataModel) == false)
            {
                return(false);
            }
            using (dbEntities)
            {
                // Unpack the Event
                EventModel anEvent = JsonConvert.DeserializeObject <EventModel>(postDataModel.ContentData);
                // Check whether the event exists
                LCEvent         eventToUpdate = dbEntities.LCEvents.FirstOrDefault(e => e.EventId == anEvent.EventId);
                LCStateProvince stateProvince = dbEntities.LCStateProvinces.FirstOrDefault(p => p.StateProvinceCode == anEvent.StateProvinceCode &&
                                                                                           p.CountryRegionCode == anEvent.CountryRegionCode); // Find the stateProvince instance matching the codes
                if (eventToUpdate == null)
                {
                    return(false);
                }
                eventToUpdate.Title       = anEvent.Title;
                eventToUpdate.Description = anEvent.Description;
                if (eventToUpdate.LCAddress != null)
                {
                    eventToUpdate.LCAddress.AddressLine1    = anEvent.AddressLine1;
                    eventToUpdate.LCAddress.AddressLine2    = anEvent.AddressLine2;
                    eventToUpdate.LCAddress.City            = anEvent.City;
                    eventToUpdate.LCAddress.PostalCode      = anEvent.PostCode;
                    eventToUpdate.LCAddress.LCStateProvince = stateProvince;
                }
                dbEntities.SaveChanges();
            }
            return(true);
        }
        public string EventDetail(Stream requestData)
        {
            var         reader        = new StreamReader(requestData);
            string      content       = reader.ReadToEnd();
            LCPostModel postDataModel = JsonConvert.DeserializeObject <LCPostModel>(content);

            if (validateUser(postDataModel) == false)
            {
                return(null);
            }
            else
            {
                EventModel eventModel = JsonConvert.DeserializeObject <EventModel>(postDataModel.ContentData);
                int        eventId    = eventModel.EventId;
                LCEvent    theEvent   = dbEntities.LCEvents.FirstOrDefault(e => e.EventId == eventId);
                if (theEvent == null)
                {
                    return(null);
                }
                LCAddress address = theEvent.LCAddress;

                var eventDetails = new
                {
                    Title             = theEvent.Title,
                    Description       = theEvent.Description,
                    TimeSlots         = theEvent.TimeSlots,
                    AddressLine1      = (address == null ? null : address.AddressLine1),
                    AddressLine2      = (address == null ? null : address.AddressLine2),
                    City              = (address == null ? null : address.City),
                    StateProvinceCode = (address == null ? null : address.LCStateProvince.StateProvinceCode.Trim()),
                    CountryRegionCode = (address == null ? null : address.LCStateProvince.CountryRegionCode.Trim())
                };
                var str = JsonConvert.SerializeObject(eventDetails);
                return(str);
            }
        }
        public string CreateEvent(Stream eventData)
        {
            // First validate the user priviledges
            var         reader        = new StreamReader(eventData);
            string      content       = reader.ReadToEnd();
            LCPostModel postDataModel = JsonConvert.DeserializeObject <LCPostModel>(content);

            if (validateUser(postDataModel) == false)
            {
                return(null);
            }
            using (dbEntities)
            {
                // Create Event
                EventModel anEvent = JsonConvert.DeserializeObject <EventModel>(postDataModel.ContentData);
                // Add the address
                LCStateProvince stateProvince =
                    dbEntities.LCStateProvinces.FirstOrDefault(p => p.StateProvinceCode == anEvent.StateProvinceCode &&
                                                               p.CountryRegionCode == anEvent.CountryRegionCode);
                if (stateProvince == null)
                {
                    return(null);
                }
                LCAddress addressToAdd = new LCAddress()
                {
                    AddressLine1       = anEvent.AddressLine1,
                    AddressLine2       = anEvent.AddressLine2,
                    City               = anEvent.City,
                    StateProvinceID    = stateProvince.StateProvinceID,
                    PostalCode         = anEvent.PostCode,
                    SpatialGeolocation = null //TODO string parsing needed.
                };
                dbEntities.LCAddresses.Add(addressToAdd);
                dbEntities.SaveChanges(); //TODO Address should be checked before being added

                // Add the event
                LCEvent eventToAdd = new LCEvent()
                {
                    TimeSlots   = anEvent.TimeSlot,
                    Title       = anEvent.Title,
                    Description = anEvent.Description,
                    AddressID   = addressToAdd.AddressID,
                    EventGUID   = Guid.NewGuid().ToString()
                };
                dbEntities.LCEvents.Add(eventToAdd);
                dbEntities.SaveChanges();

                // Link the creator to the event
                //We've checked this user is in the authdb, thereofore should also be in the transactional db
                var creator = dbEntities.LCUsers.FirstOrDefault(u => u.Email == postDataModel.Email);
                if (creator == null)
                {
                    return(null);
                }
                LCEventCreation creation = new LCEventCreation()
                {
                    EventId      = eventToAdd.EventId,
                    UserId       = creator.UserId,
                    CreationTime = DateTime.Now
                };
                dbEntities.LCEventCreations.Add(creation);
                dbEntities.SaveChanges();
                string url = baseURI + "/event/vote/" + eventToAdd.EventGUID;
                return(JsonConvert.SerializeObject(url));
            }
            return(null);
        }