Ejemplo n.º 1
0
        public async Task<ActionResult> Create(EventInformation model)
        {
            var profile = _profileService.GetProfile(User.Identity.GetUserId().ToLong());

            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("", "All of the form fields are required");
                var createModel = new CreateEventViewModel(profile);
                return View(createModel);
            }

            var start = new DateTime(model.StartDate.Year, model.StartDate.Month, model.StartDate.Day,
                model.StartTime.Hour, model.StartTime.Minute, model.StartTime.Second);

            var end = new DateTime(model.EndDate.Year, model.EndDate.Month, model.EndDate.Day,
                model.EndTime.Hour, model.EndTime.Minute, model.EndTime.Second);

            var ev = new Event
            {
                DateCreated = DateTime.UtcNow,
                StartTime = start,
                EndTime = end,
                Name = model.EventName,
                Description = model.Description,
                Privacy = model.PrivacyLevel,
                Owner = profile.User,
                Location = new Location
                {
                    Name = model.LocationInput,
                    Address = new Address
                    {
                        AddressLine1 = string.Format("{0} {1}", model.street_number, model.route),
                        City = model.locality,
                        CountryRegion = model.country,
                        PostalCode = model.postal_code,
                        StateProvince = model.state
                    }
                }
            };

            if (!model.lng.IsEmpty() && !model.lat.IsEmpty())
            {
                ev.Location.Geography = DbGeography.FromText(string.Format("POINT ({0} {1})", model.lng, model.lat));
            }

            if (model.Picture != null)
            {
                using (var content = new MultipartContent())
                {
                    content.Add(new StreamContent(model.Picture.InputStream));

                    var upload = new MediaUpload
                    {
                        Content = content,
                        ContentSize = model.Picture.ContentLength,
                        ContentType = model.Picture.ContentType
                    };

                    var picture = await _mediaService.UploadMediaAsync(upload);
                    ev.Picture = picture;
                    Log.InfoFormat("Uploaded picture for event. MediaId={0}", picture.Id);
                }
            }

            // If they want to create on facebook, do it here
            try
            {
                if (model.CreateFacebookEvent)
                {
                    var fbEvent = new FacebookEvent
                    {
                        Name = ev.Name,
                        Description = ev.Description,
                        State_Time = start,
                        Owner = new FacebookIdentity
                        {
                            Id = profile.FacebookId
                        }
                    };

                    var eventId = await _facebookService.PublishEvent(profile.User, fbEvent);
                    Log.InfoFormat("Created Facebook Event for new event.  UserId={0} FacebookId={1}", profile.User.Id, eventId);
                    ev.FacebookId = eventId;
                }
            }
            catch (Exception e)
            {
                Log.ErrorFormat("Failed to create Facebook Event for new event.  UserId={0}", profile.User.Id);
            }


            // Add yourself to the invitees list
            ev.Invitations = new List<Invitation>
            {
                new Invitation
                {
                    FacebookUserId = profile.FacebookId,
                    FacebookName = profile.GetFullName(),
                    Response = RSVP.YES
                }
            };

            // Save event
            _eventService.CreateEvent(ev);
            Log.InfoFormat("Created new Event. UserId={0} EventId={1}", profile.User.Id, ev.Id);

            return RedirectToAction("Event", new {Id = ev.Id});
        }
Ejemplo n.º 2
0
 public CreateEventViewModel(UserProfile profile)
     : base(profile)
 {
     Title = "Create Event";
     EventInformation = new EventInformation();
 }