Beispiel #1
0
        public ActionResult Create(int projectId, UseCase useCase)
        {
            var project = _projectRepository.GetNullableById(projectId);

            if (project == null)
            {
                Message = string.Format(Messages.NotFound, "Project", projectId);
                return this.RedirectToAction<ProjectController>(a => a.Index());
            }

            // validate access
            var redirect = _accessValidator.CheckReadAccess(CurrentUser.Identity.Name, project);
            if (redirect != null)
            {
                Message = "Not authorized to edit project.";
                return redirect;
            }

            var newUseCase = new UseCase(){Project = project};
            TransferValues(useCase, newUseCase);
            newUseCase.UseCaseId = string.Format("U{0}", project.UseCases.Count + 1);

            if (project.UseCases.Count > 0)
            {
                // set the task id
                // get the highest use case id
                var uid = project.UseCases.Select(a => a.Id).Max();
                var maxId = project.UseCases.Where(a => a.Id == uid).Select(a => a.UseCaseId).FirstOrDefault();
                // parse the int
                var mid = maxId.Substring(1, maxId.Length - 1);
                newUseCase.UseCaseId = string.Format("T{0}", Convert.ToInt32(mid) + 1);
            }
            else
            {
                newUseCase.UseCaseId = "U1";
            }

            ModelState.Clear();
            newUseCase.TransferValidationMessagesTo(ModelState);

            if (ModelState.IsValid)
            {
                _useCaseRepository.EnsurePersistent(newUseCase);

                Message = "UseCase Created Successfully";

                return RedirectToAction("Index", new {id=projectId});
            }

            var viewModel = UseCaseViewModel.Create(Repository, project);
            viewModel.UseCase = useCase;

            return View(viewModel);
        }