public static GoalViewModel Create(SquareContext db, IProjectService projectService, int projectStepId, string login, Goal goal = null) { Check.Require(db != null, "db is required."); var projectStep = projectService.GetProjectStep(projectStepId, login); if (goal == null) { goal = new Goal() { Project = projectStep.Project, SquareType = projectStep.Step.SquareType }; } var viewModel = new GoalViewModel() { ProjectStep = projectStep, Goal = goal, GoalTypes = db.GoalTypes.Include("SquareType").Where(a=>a.SquareType.Id == projectStep.Step.SquareType.Id).ToList() }; return viewModel; }
public ActionResult SaveBusinessGoal(int id /*project step id*/, string businessGoal) { var projectStep = _projectService.GetProjectStep(id, CurrentUserId); var goal = Db.Goals.Include("GoalType").Where(a => a.GoalType.Id == GoalTypes.Business).SingleOrDefault(); // creating new goal if (goal == null) { goal = new Goal(){Name="Business", Description = businessGoal}; _projectService.SaveGoal(id, goal, goalTypeId: GoalTypes.Business); } // updating the existing goal else { goal.Description = businessGoal; _projectService.SaveGoal(id, goal, goal.Id); } return RedirectToAction("Step2", projectStep.Step.SquareType.Name, new { id = id, projectId = projectStep.Project.Id }); }
/// <summary> /// Save a goal /// </summary> /// <param name="id">Project Step Id</param> /// <param name="goal">Goal (Description and GoalType should be populated)</param> /// <param name="goalId">Goal Id for exisitng</param> /// <returns></returns> public Goal SaveGoal(int id, Goal goal, int? goalId = null, string goalTypeId = null) { using (var db = new SquareContext()) { // load the project step var projectStep = db.ProjectSteps .Include("Step").Include("Step.SquareType") .Include("Project") .Where(a => a.Id == id).Single(); // list of goal types for this square type var goalTypes = db.GoalTypes.Where(a => a.SquareType.Id == projectStep.Step.SquareType.Id).Select(a => a.Id).ToList(); var goalType = goal.GoalType ?? db.GoalTypes.Where(a => a.Id == goalTypeId).Single(); // wrong goal type for the project step if (!goalTypes.Contains(goalType.Id) && (goalType.Id != GoalTypes.Business)) return null; // updating an existing goal if (goalId.HasValue) { var goalToSave = db.Goals.Include("SquareType").Include("Project").Include("GoalType") .Where(a => a.Id == goalId.Value).Single(); goalToSave.Name = goal.Name; goalToSave.Description = goal.Description; goalToSave.GoalType = goalType; goal = goalToSave; } else { goal.Name = goal.Name; goal.Description = goal.Description; goal.SquareType = projectStep.Step.SquareType; goal.Project = projectStep.Project; goal.GoalType = goalType; db.Goals.Add(goal); } db.SaveChanges(); return goal; } }