public void Execute_StoresEvidence() { var existingForm = new ChangeOfCircsBuilder("form123") .With(f => f.Evidence, EvidenceBuilder.NewValid(e => e.SendingByPost = false)) .Insert(f => f.Evidence.AddFiles(f, 2)); existingForm.Evidence.SendingByPost.Should().BeFalse("not set before executing command"); existingForm.Evidence.Files.Count.Should().Be(2, "should have existing uploaded files"); var cmd = new AddEvidence { FormId = "form123", Evidence = EvidenceBuilder.NewValid(), }; cmd.Execute(); var updatedForm = Repository.Load <ChangeOfCircs>("form123"); updatedForm.Evidence.SendingByPost.Should().Be(cmd.Evidence.SendingByPost); updatedForm.Evidence.Files.Count.Should().Be(2, "files should not be overwritten"); }
public ActionResult Evidence(string id, Evidence evidence) { if (WasClicked(BsgButtons.UploadFile)) { if (Request.Files.Count == 0) { ModelState.AddModelError("", "Could not upload file"); return(Evidence_Render(id, evidence)); } var file = Request.Files[0]; if (string.IsNullOrWhiteSpace(file.FileName)) { ModelState.AddModelError("", "Please select a file to upload"); return(Evidence_Render(id, evidence)); } const int maxSize = 1024 * 1024 * 2; if (file.ContentLength > maxSize) { ModelState.AddModelError("", "Please select a file that is smaller than 2MB"); return(Evidence_Render(id, evidence)); } using (var br = new BinaryReader(file.InputStream)) { var addFile = new AddEvidenceFile { FormId = id, Filename = file.FileName, Content = br.ReadBytes((int)file.InputStream.Length), }; return(Exec(addFile, success: () => Redirect(BsgActions.Evidence(id)), failure: () => Evidence_Render(id, evidence))); } } else if (WasClicked(BsgButtons.RemoveFile)) { var cloudName = Request.Form[BsgButtons.RemoveFile]; var delFile = new RemoveEvidenceFile { FormId = id, CloudName = cloudName, }; return(Exec(delFile, success: () => Redirect(BsgActions.Evidence(id)), failure: () => Evidence_Render(id, evidence))); } var cmd = new AddEvidence { FormId = id, Evidence = evidence, }; return(Exec(cmd, success: next => RedirectNext(next), failure: () => Evidence_Render(id, evidence))); }