public static CategoryEditViewModel Create(SquareContext db, IProjectService projectService, int projectId, int projectStepId, string userId, Category category = null) { Check.Require(projectService != null, "projectService is required."); var viewModel = new CategoryEditViewModel() { Category = category ?? new Category()}; viewModel.SetProjectInfo(projectService, projectId, projectStepId, userId); return viewModel; }
public ActionResult Create(int id, int projectId, Category category) { try { var project = _projectService.GetProject(projectId, CurrentUserId); if (ModelState.IsValid) { _projectService.SaveCategory(id, category); Message = string.Format(Messages.Saved, "Category"); return this.RedirectToAction(a => a.Index(id, projectId)); } var viewModel = CategoryEditViewModel.Create(Db, _projectService, projectId, id, CurrentUserId, category); return View(viewModel); } catch (SecurityException) { return this.RedirectToAction<ErrorController>(a => a.NoAccessToStep()); } }
public void SaveCategory(int id, Category category, int? categoryId) { using (var db = new SquareContext()) { var projectStep = db.ProjectSteps.Include("Project") .Include("Step") .Include("Step.SquareType") .Where(a => a.Id == id).Single(); // update existing category if (categoryId.HasValue) { var categoryToSave = db.Categories.Where(a => a.Id == categoryId).Single(); categoryToSave.Name = category.Name; } // setting an existing one else { category.Project = projectStep.Project; category.SquareType = projectStep.Step.SquareType; db.Categories.Add(category); } db.SaveChanges(); } }