public Goal OutOfGoalViewModel(GoalViewModel goalViewModel, string username)
        {
            Sports sports = new Sports(SessionProvider.CurrentSession);
            Venues venues = new Venues(SessionProvider.CurrentSession);
            Goals goals = new Goals(SessionProvider.CurrentSession);
            Users users = new Users(SessionProvider.CurrentSession);

            Goal goal;
            if(goalViewModel.Id == 0)
            {
                goal = new Goal();
                goal.CreatedOn = DateTime.Now;
                goal.UserCreator = users.GetByUserName(username);
            }
            else
            {
                goal = goals.GetById(goalViewModel.Id);
            }
            
            goal.Name = goalViewModel.Name;
            goal.Date = goalViewModel.Date;
            goal.Description = goalViewModel.Description;

            goal.Web = goalViewModel.Web;
            goal.Venue = venues.GetById(Convert.ToInt32(goalViewModel.VenueId));
            goal.Sport = sports.GetById(Convert.ToInt32(goalViewModel.SportId));

            goal.Venue.Latitude = Convert.ToInt32(goalViewModel.VenueLatitude);
            goal.Venue.Longitude = Convert.ToInt32(goalViewModel.VenueLongitude);

            return goal;
        }
        public ActionResult Create_(Goal goal)
        {
            // This method is casting the values from the form and validating the state.
            // However, since there are some fiels as of now that we are still not adding, 
            // the validate state is failing. We have workarounds to add the missing values 
            // and validate the state. The issue is that we wont know anymore if 
            // there was some legit error coming from the site.

            var goals = new Goals(SessionProvider.CurrentSession);
            var anyGoal = goals.GetAll().First();
            goal.CreatedOn = DateTime.Now;
            goal.Venue = anyGoal.Venue;
            goal.Sport = anyGoal.Sport;
            goal.UserCreator = anyGoal.UserCreator;


            ModelState.Where(m => m.Value.Errors != null).ToList().ForEach(e =>
                                                                               {
                                                                                   var error = e.Value.Errors;
                                                                                   error.Clear();
                                                                               });

            UpdateModel(goal);
            if (ModelState.IsValid)
            {
                goals.Add(goal);
                return RedirectToAction("Details", new { Id = goal.Id });
            }

            GoalViewModel goalViewModel = AutoMapper.Mapper.Map<Goal, GoalViewModel>(goal);

            return View(goalViewModel);
        }
Exemple #3
0
 public virtual void AddGoal(Goal goal, DateTime signedOndate)
 {
     this.Goals.Add(
         new GoalParticipant
             {
                 User = this,
                 Goal = goal,
                 SignedOnDate = signedOndate
             });
 }
        public ActionResult Edit_(Goal viewModelGoal)
        {
            // This would be the manual process, using the update hard coded. 
            // Better use the UpdateModel integrated in the framework!
            UpdateModel(viewModelGoal);

            var goals = new Goals(SessionProvider.CurrentSession);
            var currentGoal = goals.GetById(viewModelGoal.Id);

            currentGoal.Description = viewModelGoal.Description;
            currentGoal.Name = viewModelGoal.Name;
            currentGoal.Web = viewModelGoal.Web;

            goals.Update(currentGoal);

            return RedirectToAction("Details", new { Id = currentGoal.Id });
        }