public async Task <IActionResult> Edit(BenchmarkDto model) { ApplicationUser user = await this.GetCurrentUserAsync(); if (user == null) { throw new NotLoggedInException("You are not logged in"); } const string ViewName = "Add"; if (!this.ModelState.IsValid) { return(this.View(ViewName, model)); } // Manually parse input var testCases = InputDataParser.ReadTestCases(this.HttpContext.Request); // Check if benchmark code was actually entered if (testCases.Count() < 2) { this.ModelState.AddModelError("TestCases", "At least two test cases are required."); return(this.View(ViewName, model)); } if (testCases.Any(t => string.IsNullOrWhiteSpace(t.BenchmarkCode))) { this.ModelState.AddModelError("TestCases", "Benchmark code must not be empty."); return(this.View(ViewName, model)); } model.TestCases = new List <TestCaseDto>(testCases); // Check that there are no test cases with the same name var set = new HashSet <string>(); foreach (var testCase in model.TestCases) { if (!set.Add(testCase.TestCaseName.ToLowerInvariant().Trim())) { this.ModelState.AddModelError("TestCases", "Test cases must have unique names"); return(this.View("Add", model)); } } try { BenchmarkDto updatedModel = await this.m_benchmarkRepository.Update(model, user.Id); return(this.RedirectToAction("Show", new { Id = updatedModel.Id, Version = updatedModel.Version, name = SeoFriendlyStringConverter.Convert(model.BenchmarkName) })); } catch (Exception ex) { m_logger.LogError("Can't update benchmark: " + ex.Message); return(RedirectToAction(ErrorActionName)); } }
public async Task <IActionResult> Add(BenchmarkDto model) { if (!this.ModelState.IsValid) { return(this.View(model)); } // Manually parse input var testCases = InputDataParser.ReadTestCases(this.HttpContext.Request); // Check if benchmark code was actually entered if (testCases.Count() < 2) { // TODO: use correct error key this.ModelState.AddModelError("TestCases", "At least two test cases are required."); return(this.View(model)); } if (testCases.Any(t => string.IsNullOrWhiteSpace(t.BenchmarkCode))) { this.ModelState.AddModelError("TestCases", "Benchmark code must not be empty."); return(this.View(model)); } model.TestCases = new List <TestCaseDto>(testCases); // Check that there are no test cases with the same name var set = new HashSet <string>(); foreach (var testCase in model.TestCases) { if (!set.Add(testCase.TestCaseName.ToLowerInvariant().Trim())) { this.ModelState.AddModelError("TestCases", "Test cases must have unique names"); return(this.View(model)); } } ApplicationUser user = await this.GetCurrentUserAsync(); model.OwnerId = user?.Id; long id = await this.m_benchmarkRepository.Add(model); return(this.RedirectToAction("Show", new { Id = id, Version = 0, name = SeoFriendlyStringConverter.Convert(model.BenchmarkName) })); }
private void ValidateInputModel(BenchmarkDto model) { if (!this.ModelState.IsValid) { return; } // Manually parse input var testCases = InputDataParser.ReadTestCases(this.HttpContext.Request); // Check if benchmark code was actually entered if (testCases.Count() < 2) { this.ModelState.AddModelError("TestCases", "At least two test cases are required."); } if (testCases.Any(t => string.IsNullOrWhiteSpace(t.BenchmarkCode))) { this.ModelState.AddModelError("TestCases", "Benchmark code must not be empty."); } model.TestCases = new List <TestCaseDto>(testCases); // Check that there are no test cases with the same name var set = new HashSet <string>(); foreach (var testCase in model.TestCases) { if (testCase == null) { continue; } if (string.IsNullOrWhiteSpace(testCase.TestCaseName)) { this.ModelState.AddModelError("TestCases", "Test name must not be empty"); continue; } if (!set.Add(testCase.TestCaseName.ToLowerInvariant().Trim())) { this.ModelState.AddModelError("TestCases", "Test cases must have unique names"); } } }