//
 // GET: /Events/New
 public ActionResult New()
 {
     var viewModel = new EventViewModel
     {
         Venues = _venuesRepository.GetAll().Select(v => v.Name),
         Members = _membersRepository.GetAll().Select(m => m.Name)
     };
     return View(viewModel);
 }
        public ActionResult New(EventViewModel eventView)
        {
            Member winner = ValidateWinner(eventView.Winner);

            // create a new event
            if (!ModelState.IsValid)
            {
                eventView.Venues = _venuesRepository.GetAll().Select(v => v.Name);
                eventView.Members = _membersRepository.GetAll().Select(m => m.Name);
                return View(eventView);
            }

            var newEvent = new Event
            {
                Name = eventView.Name,
                WinnerID = winner != null ? winner.ID : (int?)null,
                When = eventView.StartDate,
                Cost = eventView.Cost
            };

            if (!string.IsNullOrEmpty(eventView.Venue))
            {
                var venue = _venuesRepository.Get(eventView.Venue);
                if (venue == null)
                {
                    // venue doesn't exist
                    newEvent.Venue = new Venue
                    {
                        Name = eventView.Venue,
                        AddressLine1 = eventView.AddressLine1,
                        AddressLine2 = eventView.AddressLine2
                    };
                }
                else
                {
                    // update address of existing venue
                    venue.AddressLine1 = eventView.AddressLine1;
                    venue.AddressLine1 = eventView.AddressLine1;

                    newEvent.Venue = venue;
                }
            }

            _eventsRepository.Create(newEvent);
            return RedirectToAction("Index");
        }
        public ActionResult Update(int id, EventViewModel eventView)
        {
            Member winner = ValidateWinner(eventView.Winner);

            if (!ModelState.IsValid)
            {
                eventView.Venues = _venuesRepository.GetAll().Select(v => v.Name);
                eventView.Members = _membersRepository.GetAll().Select(m => m.Name);
                return View(eventView);
            }

            var eventToUpdate = _eventsRepository.Get(id);

            if (eventToUpdate == null)
                return HttpNotFound();

            eventToUpdate.Name = eventView.Name;
            eventToUpdate.WinnerID = winner != null ? winner.ID : (int?)null;
            eventToUpdate.When = eventView.StartDate;
            eventToUpdate.Cost = eventView.Cost;

            if (string.IsNullOrEmpty(eventView.Venue))
            {
                eventToUpdate.VenueReference = null; //Venue = null;
                //eventToUpdate.VenueID = null;
            }
            else
            {
                var venue = _venuesRepository.Get(eventView.Venue);
                if (venue == null)
                {
                    // venue doesn't exist
                    eventToUpdate.Venue = new Venue
                    {
                        Name = eventView.Venue,
                        AddressLine1 = eventView.AddressLine1,
                        AddressLine2 = eventView.AddressLine2
                    };
                }
                else
                {
                    // update address of existing venue
                    venue.AddressLine1 = eventView.AddressLine1;
                    venue.AddressLine1 = eventView.AddressLine1;

                    eventToUpdate.Venue = venue;
                }
            }

            _eventsRepository.Update(eventToUpdate);

            return RedirectToAction("Index");
        }
        //
        // GET: /Events/Update/2
        public ActionResult Update(int id)
        {
            var eventToUpdate = _eventsRepository.Get(id);

            if (eventToUpdate == null)
                return HttpNotFound();

            EventViewModel eventView = new EventViewModel
            {
                Name = eventToUpdate.Name,
                StartDate = eventToUpdate.When,
                Cost = eventToUpdate.Cost,
                Venues = _venuesRepository.GetAll().Select(v => v.Name),
                Members = _membersRepository.GetAll().Select(m => m.Name)
            };

            if (eventToUpdate.Venue != null)
            {
                eventView.Venue = eventToUpdate.Venue.Name;
                eventView.AddressLine1 = eventToUpdate.Venue.AddressLine1;
                eventView.AddressLine2 = eventToUpdate.Venue.AddressLine2;
            }

            if(eventToUpdate.Member != null)
            {
                eventView.Winner = eventToUpdate.Member.Name;
            }

            return View(eventView);
        }