public ActionResult Add(int id, Artifact artifact, int artifactTypeId, HttpPostedFileBase file) { // validate access and load project step var projectStep = _projectService.GetProjectStep(id, CurrentUserId); ModelState.Remove("artifact.ArtifactType"); ModelState.Remove("artifact.Project"); ModelState.Remove("artifact.CreatedBy"); ModelState.Remove("artifact.Data"); ModelState.Remove("artifact.ContentType"); if (file == null) ModelState.AddModelError("File", "File is required."); if (!Db.ArtifactTypes.Where(a => a.Id == artifactTypeId).Any()) ModelState.AddModelError("Artifact Type", "Artifact Type is Required"); if (ModelState.IsValid) { artifact.CreatedBy = CurrentUserId; // read the file var reader = new BinaryReader(file.InputStream); var data = reader.ReadBytes(file.ContentLength); artifact.Data = data; artifact.ContentType = file.ContentType; _projectService.SaveArtifact(id, artifact, null, artifactTypeId); Message = string.Format(Messages.Saved, "Artifact"); return LinkGenerator.CreateRedirectResult(Request.RequestContext, _projectService.GetProjectStep(id, CurrentUserId)); } var viewModel = ArtifactViewModel.Create(Db, _projectService, artifact, id, CurrentUserId); return View(viewModel); }
public static ArtifactViewModel Create(SquareContext db, IProjectService projectService, Artifact artifact, int projectStepId, string loginId) { Check.Require(db != null, "Repository is required."); var projectStep = projectService.GetProjectStep(projectStepId, loginId); var viewModel = new ArtifactViewModel() { ArtifactTypes = db.ArtifactTypes.Where(a=>a.SquareType.Id == projectStep.Step.SquareType.Id).ToList(), ProjectStep = projectStep, Artifact = artifact ?? new Artifact() }; return viewModel; }
/// <summary> /// Save an artifact /// </summary> /// <param name="id">Project Step Id</param> /// <param name="artifact"></param> /// <param name="loginId"></param> /// <returns></returns> public Artifact SaveArtifact(int id, Artifact artifact, int? artifactId, int? artifactTypeId) { 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 artifact types for this square type var artifactTypes = db.ArtifactTypes.Where(a => a.SquareType.Id == projectStep.Step.SquareType.Id).Select(a => a.Id).ToList(); var artifactType = artifact.ArtifactType ?? db.ArtifactTypes.Include("SquareType").Where(a => a.Id == artifactTypeId).Single(); // wrong artifact type for the project step if (!artifactTypes.Contains(artifactType.Id)) return null; // update an existing artifact if (artifactId.HasValue) { var artifactToSave = db.Artifacts.Where(a => a.Id == artifactId).Single(); artifactToSave.Name = artifact.Name; artifactToSave.Description = artifact.Description; artifactToSave.ArtifactType = artifactType; // only update file contents if there is a new file, otherwise keep old contents if (artifact.ContentType != null && artifact.Data != null) { artifactToSave.ContentType = artifact.ContentType; artifactToSave.Data = artifact.Data; } } // fill in the new artifact else { artifact.ArtifactType = artifactType; artifact.Project = projectStep.Project; db.Artifacts.Add(artifact); } db.SaveChanges(); return artifact; } }
public ActionResult Edit(int id, int artifactId, Artifact artifact, int artifactTypeId, HttpPostedFileBase file) { ModelState.Remove("artifact.ArtifactType"); ModelState.Remove("artifact.Project"); ModelState.Remove("artifact.CreatedBy"); ModelState.Remove("artifact.Data"); ModelState.Remove("artifact.ContentType"); // load the existing one var existingArtifact = _projectService.LoadArtifact(artifactId); if (existingArtifact == null) { ModelState.AddModelError("Artifact", "Unable to load artifact."); } if (!Db.ArtifactTypes.Where(a => a.Id == artifactTypeId).Any()) { ModelState.AddModelError("Artifact Type", "Artifact Type is Required"); } if (ModelState.IsValid) { // load in the file if there is one if (file != null) { // read the file var reader = new BinaryReader(file.InputStream); var data = reader.ReadBytes(file.ContentLength); artifact.Data = data; artifact.ContentType = file.ContentType; } else { artifact.Data = null; artifact.ContentType = null; } // save the artifact _projectService.SaveArtifact(id, artifact, artifactId, artifactTypeId); // display message Message = string.Format(Messages.Saved, "Artifact"); // redirect return LinkGenerator.CreateRedirectResult(Request.RequestContext, _projectService.GetProjectStep(id, CurrentUserId)); } var viewModel = ArtifactViewModel.Create(Db, _projectService, existingArtifact, id, CurrentUserId); return View(viewModel); }