public async Task <ActionResult> Resolve(DataAccess.Model.Tests.Tests tests, HttpPostedFileBase[] files) { TestsRepository testsRepo = new TestsRepository(); DataAccess.Model.Tests.Tests testsRecord = testsRepo.GetById(tests.Id); TestStatusRepository testStatusRepo = new TestStatusRepository(); ModelState.Remove("files"); ModelState.Remove(nameof(DataAccess.Model.Tests.Tests.Tester)); ModelState.Remove(nameof(DataAccess.Model.Tests.Tests.Status)); ModelState.Remove(nameof(DataAccess.Model.Tests.Tests.Test)); ModelState.Remove(nameof(DataAccess.Model.Tests.Tests.Evidences)); ModelState.Remove(nameof(DataAccess.Model.Tests.Tests.Finished)); ModelState.Remove(nameof(DataAccess.Model.Tests.Tests.Rejected)); ModelState.Remove(nameof(DataAccess.Model.Tests.Tests.Takened)); ModelState.Remove("TestStatus.Status"); if (ModelState.IsValid) { if (testsRecord.Test.Creator.Credits < testsRecord.Test.Reward) { ViewBag.TestStatus = testStatusRepo.GetAll(); TempData["error"] = "Createor of tests don't have required amount of coins. Please try resolve test later, or contact our support team."; return(View("TakenedTests")); } var testStatusTask = testStatusRepo.GetByIdAsync(tests.TestStatus.Id); IList <Evidence> evidences = new List <Evidence>(); if (files[0] != null) { var maxSizeInMb = 20; var byteSize = 1048576; var maxSizeInBytes = byteSize * maxSizeInMb; foreach (var file in files) { if (file.ContentLength > maxSizeInBytes) { TempData["error"] = "File " + file.FileName + " is too big! (max size is " + maxSizeInMb + "MB)"; ViewBag.TestStatus = testStatusRepo.GetAll(); return(View("ResolveTest", testsRecord)); } } foreach (var file in files) { Evidence evidence = new Evidence(); evidence.Id = Guid.NewGuid(); evidence.Name = evidence.Id.ToString(); evidence.RealName = Path.GetFileNameWithoutExtension(file.FileName); evidence.Attached = DateTime.Now; var extension = Path.GetExtension(file.FileName); evidence.Extension = extension; var path = Path.Combine(Server.MapPath($"~/Uploads/{testsRecord.Id}"), evidence.Name + extension); Directory.CreateDirectory(Server.MapPath($"~/Uploads/{testsRecord.Id}")); file.SaveAs(path); evidences.Add(evidence); } testsRecord.Evidences = evidences; } testsRecord.TestStatus = await testStatusTask; testsRecord.Finished = DateTime.Now; testsRecord.Status = TestsStatus.Finished; testsRecord.Tester.Credits = testsRecord.Tester.Credits + testsRecord.Test.Reward; testsRecord.Test.Creator.Credits = testsRecord.Test.Creator.Credits - testsRecord.Test.Reward; EvidenceRepository evidRepo = new EvidenceRepository(); foreach (var evidence in evidences) { evidRepo.Create(evidence); } testsRepo.Update(testsRecord); return(RedirectToAction("FinishedTests")); } ViewBag.TestStatus = testStatusRepo.GetAll(); return(View("ResolveTest", testsRecord)); }
public async Task <ActionResult> UpdateTestCase(TestCase testCase, HttpPostedFileBase[] files) { ModelState.Remove(nameof(testCase.Evidences)); ModelState.Remove(nameof(testCase.Rating)); ModelState.Remove(nameof(testCase.Created)); ModelState.Remove(nameof(testCase.Creator)); ModelState.Remove(nameof(testCase.Reviews)); ModelState.Remove(nameof(testCase.IsInGroup)); ModelState.Remove("SoftwareType.Name"); ModelState.Remove("Category.Name"); ModelState.Remove("files"); if (ModelState.IsValid) { TestCaseRepository testCaseRepo = new TestCaseRepository(); TestCategoryRepository testCatRepo = new TestCategoryRepository(); SoftwareTypeRepository swTypeRepo = new SoftwareTypeRepository(); TestCase oldTestCase = testCaseRepo.GetById(testCase.Id); IList <Evidence> evidences = new List <Evidence>(); if (testCase.AvailableTo <= DateTime.Now) { IList <SoftwareType> swTypes = swTypeRepo.GetAllValid(); IList <TestCategory> testCats = testCatRepo.GetAllValid(); ViewBag.SoftwareTypes = swTypes; ViewBag.TestCategories = testCats; TempData["error"] = "Available To must be in future!"; return(View("EditTestCase", testCase)); } if (files[0] != null) { if (oldTestCase.Evidences == null || oldTestCase.Evidences.Count <= 0) { oldTestCase.Evidences = new List <Evidence>(); } var maxSizeInMb = 20; var byteSize = 1048576; var maxSizeInBytes = byteSize * maxSizeInMb; foreach (var file in files) { if (file.ContentLength > maxSizeInBytes) { IList <SoftwareType> swTypes = swTypeRepo.GetAllValid(); IList <TestCategory> testCats = testCatRepo.GetAllValid(); ViewBag.SoftwareTypes = swTypes; ViewBag.TestCategories = testCats; TempData["error"] = "File " + file.FileName + " is too big! (max size is " + maxSizeInMb + "MB)"; return(View("EditTestCase", testCase)); } } foreach (var file in files) { Evidence evidence = new Evidence(); evidence.Id = Guid.NewGuid(); evidence.Name = evidence.Id.ToString(); evidence.RealName = Path.GetFileNameWithoutExtension(file.FileName); evidence.Attached = DateTime.Now; var extension = Path.GetExtension(file.FileName); evidence.Extension = extension; var path = Path.Combine(Server.MapPath("~/Uploads"), evidence.Name + extension); Directory.CreateDirectory(Server.MapPath("~/Uploads")); file.SaveAs(path); oldTestCase.Evidences.Add(evidence); evidences.Add(evidence); } } var swType = swTypeRepo.GetByIdAsync(testCase.SoftwareType.Id); var testCat = testCatRepo.GetByIdAsync(testCase.Category.Id); oldTestCase.SoftwareType = await swType; oldTestCase.Category = await testCat; oldTestCase.Name = testCase.Name; oldTestCase.SkillDificulty = testCase.SkillDificulty; oldTestCase.TimeDificulty = testCase.TimeDificulty; oldTestCase.AvailableTo = testCase.AvailableTo; oldTestCase.Description = testCase.Description; oldTestCase.Reward = testCase.Reward; EvidenceRepository evidRepo = new EvidenceRepository(); foreach (var evidence in evidences) { evidRepo.Create(evidence); } testCaseRepo.Update(oldTestCase); TempData["success"] = "Test case was edited"; } else { SoftwareTypeRepository swTypeRepo = new SoftwareTypeRepository(); IList <SoftwareType> swTypes = swTypeRepo.GetAllValid(); ViewBag.SoftwareTypes = swTypes; TestCategoryRepository testCatRepo = new TestCategoryRepository(); IList <TestCategory> testCats = testCatRepo.GetAllValid(); ViewBag.TestCategories = testCats; if (!swTypeRepo.IsSwTypeExist(testCase.SoftwareType.Id)) { TempData["error"] = "Please select Software type!"; return(View("EditTestCase", testCase)); } if (!testCatRepo.IsTestCatExist(testCase.Category.Id)) { TempData["error"] = "Please select Category!"; return(View("EditTestCase", testCase)); } return(View("EditTestCase", testCase)); } return(RedirectToAction("CompanyTests")); }