public ActionResult AddNewTerm(int id, int projectId, ProjectTerm projectTerm)
        {
            // remove out errors that shouldn't be there, project and square type
            ModelState.Remove("projectTerm.Project");
            ModelState.Remove("projectTerm.Squaretype");

            var project = Db.Projects.Where(a => a.Id == projectId).Single();
            var projectStep = Db.ProjectSteps.Include("Step").Include("Step.SquareType").Where(a => a.Id == id).Single();

            if (ModelState.IsValid)
            {
                 projectTerm = _projectService.AddTermToProject(project.Id, projectStep.Step.SquareType.Id
                                                            , term: projectTerm.Term
                                                            , definition: projectTerm.Definition
                                                            , source: projectTerm.Source);

                Message = "Successfully added term to project";

                return RedirectToAction("Step1", projectTerm.SquareType.Name, new {id=id, projectId=projectId});
            }

            projectTerm.Project = project;
            var viewModel = ProjectTermAddNewTermViewModel.Create(Db, id, projectTerm);
            return View(viewModel);
        }
        /// <summary>
        /// Add a brand new term (not necessarily in the db)
        /// </summary>
        /// <param name="id">Project Step Id</param>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public ActionResult AddNewTerm(int id /*step id*/, int projectId)
        {
            var project = Db.Projects.Where(a => a.Id == projectId).Single();
            var projectStep = Db.ProjectSteps.Include("Step").Include("Step.SquareType").Where(a => a.Id == id).Single();

            var projectTerm = new ProjectTerm() { Project = project, SquareType = projectStep.Step.SquareType};

            var viewModel = ProjectTermAddNewTermViewModel.Create(Db, id, projectTerm);
            return View(viewModel);
        }
        public static ProjectTermAddNewTermViewModel Create(SquareContext db, int stepId, ProjectTerm projectTerm)
        {
            Check.Require(db != null, "Square Entities is required.");
            Check.Require(projectTerm != null, "projectTerm is required.");

            var projectStep = db.ProjectSteps.Include("Step").Include("Step.SquareType").Where(a => a.Id == stepId).Single();
            projectTerm.SquareType = projectStep.Step.SquareType;
            var viewModel = new ProjectTermAddNewTermViewModel() {Step = projectStep.Step, ProjectTerm = projectTerm};

            return viewModel;
        }
Exemple #4
0
        /// <summary>
        /// Add a term to a project
        /// </summary>
        /// <remarks>Does not validate access</remarks>
        /// <param name="id">Project Id</param>
        /// <param name="squareTypeId">Square Type Id</param>
        /// <param name="term"></param>
        /// <param name="definition"></param>
        /// <param name="source"></param>
        /// <param name="termId"></param>
        /// <param name="definitionId"></param>
        public ProjectTerm AddTermToProject(int id, int squareTypeId, string term, string definition, string source, int termId, int definitionId)
        {
            using (var db = new SquareContext())
            {
                var project = db.Projects.Where(a => a.Id == id).Single();
                var squareType = db.SquareTypes.Where(a => a.Id == squareTypeId).Single();

                // update the parameters with the values from the database
                if (termId > 0 && definitionId > 0)
                {
                    var termObj = db.Terms.Where(a => a.Id == termId).Single();
                    var definitionObj = db.Definitions.Where(a => a.Id == definitionId).Single();

                    // make sure def matches term
                    if (definitionObj.Term.Id != termObj.Id) throw new ArgumentException("Term/Definition mismatch.");

                    // set the parameters, so it can be generated
                    term = termObj.Name;
                    definition = definitionObj.Description;
                    source = definitionObj.Source;
                }

                // add the term to the project
                if (!string.IsNullOrWhiteSpace(term) && !string.IsNullOrWhiteSpace(definition) && !string.IsNullOrWhiteSpace(source))
                {
                    // not project or square type, wtf?
                    // shouldn't happen if null, would have thrown exception in .single() above.
                    if (project == null || squareType == null)
                    {
                        throw new ArgumentException("Project or Square Type Id are invalid");
                    }

                    // create the new term
                    var projectTerm = new ProjectTerm();
                    projectTerm.Term = term;
                    projectTerm.Definition = definition;
                    projectTerm.Source = source;
                    projectTerm.Project = project;
                    projectTerm.SquareType = squareType;

                    // add the project term to the db
                    db.ProjectTerms.Add(projectTerm);
                    db.SaveChanges();

                    return projectTerm;
                }
            }

            return null;
        }