public ActionResult AddProjectConfig(string projectName)
        {
            if (string.IsNullOrWhiteSpace(projectName))
            {
                return(View("Error"));
            }

            Project project;

            using (var db = new BuildVersioningDataContext())
            {
                project =
                    db.Projects
                    .SingleOrDefault(p =>
                                     string.Compare(p.Name, projectName, StringComparison.OrdinalIgnoreCase) == 0
                                     );
            }

            if (null == project)
            {
                return(View("Error"));
            }

            var model =
                new AddProjectConfigModel
            {
                ParentProjectId   = project.Id,
                ParentProjectName = project.Name
            };

            return(View(model));
        }
        public ActionResult AddProjectConfig(AddProjectConfigModel addProjectConfig)
        {
            if (null == addProjectConfig)
            {
                return(View("Error"));
            }

            // Validate ParentProjectId
            var parentProjectId = addProjectConfig.ParentProjectId;

            if (parentProjectId < 1)
            {
                return(View("Error"));
            }

            // Validate ParentProjectName
            var parentProjectName = addProjectConfig.ParentProjectName;

            if (string.IsNullOrWhiteSpace(parentProjectName))
            {
                return(View("Error"));
            }

            // Validate Name
            var name = addProjectConfig.Name;

            if (string.IsNullOrWhiteSpace(name))
            {
                return(View("Error"));
            }

            var desc = addProjectConfig.Description;

            if (string.IsNullOrWhiteSpace(desc))
            {
                desc = null;
            }

            var buildNumberPostion = (int)addProjectConfig.BuildNumberPosition;

            if (buildNumberPostion < 3 || buildNumberPostion > 4)
            {
                return(View("Error"));
            }

            // Force the GeneratedVersion part that corresponds to the build number position to a value of zero.
            switch (buildNumberPostion)
            {
            case 3:
                addProjectConfig.GeneratedVersionPart3 = 0;
                break;

            case 4:
                addProjectConfig.GeneratedVersionPart4 = 0;
                break;
            }

            using (var db = new BuildVersioningDataContext())
            {
                if (db.ProjectConfigs
                    .Include(c => c.Project)
                    .Any(c =>
                         c.Project.Id == parentProjectId &&
                         string.Compare(c.Name, name, StringComparison.OrdinalIgnoreCase) == 0
                         ))
                {
                    return(View("Error"));                    // <-- A ProjectConfig with the same parent Project and the same name already exists.
                }
                var projectConfig =
                    new ProjectConfig
                {
                    Description = desc,
                    GeneratedBuildNumberPosition = buildNumberPostion,
                    GeneratedVersionPart1        = addProjectConfig.GeneratedVersionPart1,
                    GeneratedVersionPart2        = addProjectConfig.GeneratedVersionPart2,
                    GeneratedVersionPart3        = addProjectConfig.GeneratedVersionPart3,
                    GeneratedVersionPart4        = addProjectConfig.GeneratedVersionPart4,
                    Name = name,
                    ProductVersionPart1 = addProjectConfig.ProductVersionPart1,
                    ProductVersionPart2 = addProjectConfig.ProductVersionPart2,
                    ProductVersionPart3 = addProjectConfig.ProductVersionPart3,
                    ProductVersionPart4 = addProjectConfig.ProductVersionPart4,
                    ProjectId           = parentProjectId,
                    ReleaseType         = addProjectConfig.ReleaseType
                };

                db.ProjectConfigs.Add(projectConfig);
                db.SaveChanges();
            }

            return(RedirectToRoute("ViewProject", new { name = parentProjectName }));
        }