public ActionResult DeleteAttachedTestCase(string id, string testCaseId) { TestGroupRepository testGroupRepo = new TestGroupRepository(); TestCaseRepository testCaseRepo = new TestCaseRepository(); TestGroup testGroup = testGroupRepo.GetById(Guid.Parse(id)); if (testGroup.TestCases != null && testGroup.TestCases.Count > 0) { foreach (var tempTestCase in testGroup.TestCases) { if (tempTestCase.Id == Guid.Parse(testCaseId)) { testGroup.TestCases.Remove(tempTestCase); break; } } testGroup.SkillDificulty = testGroup.CountSkillDificulty(); testGroup.TimeDificulty = testGroup.CountTimeDificulty(); testGroupRepo.Update(testGroup); TestCase testCase = testCaseRepo.GetById(Guid.Parse(testCaseId)); testCase.IsInGroup = false; testCaseRepo.Update(testCase); } return(RedirectToAction("EditTestGroup", "TestGroups", new { id = testGroup.Id.ToString() })); }
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")); }
public ActionResult UpdateTestGroup(TestGroup testGroup, string data) { TestGroupRepository testGroupRepo = new TestGroupRepository(); TestGroup oldTestGroup = testGroupRepo.GetById(testGroup.Id); if (string.IsNullOrEmpty(data) && oldTestGroup.TestCases.Count <= 0) { TempData["error"] = "At least one test case must be selected!"; return(View("EditTestGroup", testGroup)); } ModelState.Remove(nameof(TestGroup.TestCases)); ModelState.Remove(nameof(TestGroup.Created)); ModelState.Remove(nameof(TestGroup.Creator)); ModelState.Remove(nameof(TestGroup.Rating)); ModelState.Remove(nameof(TestGroup.TimeDificulty)); ModelState.Remove(nameof(TestGroup.SkillDificulty)); if (ModelState.IsValid) { var testCasesId = data.Split(','); TestCaseRepository testCaseRepo = new TestCaseRepository(); IList <TestCase> testCases = new List <TestCase>(); if (!testCasesId[0].IsNullOrEmpty()) { foreach (var testCaseId in testCasesId) { TestCase tempTestCase = testCaseRepo.GetByIdForCompanyNotInGroup(testGroup.Creator, Guid.Parse(testCaseId)); tempTestCase.IsInGroup = true; oldTestGroup.TestCases.Add(tempTestCase); testCases.Add(tempTestCase); } } DateTime latestAvailability = oldTestGroup.TestCases.OrderByDescending(x => x.AvailableTo).First().AvailableTo; if (testGroup.AvailableTo < latestAvailability) { testGroup.TestCases = oldTestGroup.TestCases; testGroup.RewardMultiplier = oldTestGroup.RewardMultiplier; TempData["error"] = $"Available To must be equal or greater than availability of latest test case ({latestAvailability.ToShortDateString()})!"; return(View("EditTestGroup", testGroup)); } oldTestGroup.SkillDificulty = oldTestGroup.CountSkillDificulty(); oldTestGroup.TimeDificulty = oldTestGroup.CountTimeDificulty(); oldTestGroup.RewardMultiplier = (testGroup.RewardMultiplier + 100) / 100; oldTestGroup.Name = testGroup.Name; oldTestGroup.AvailableTo = testGroup.AvailableTo; foreach (var testCase in testCases) { testCaseRepo.Update(testCase); } testGroupRepo.Update(oldTestGroup); TempData["success"] = "Test group was edited"; } else { return(View("CreateTestGroup")); } return(RedirectToAction("CompanyTestGroups")); }
public ActionResult AddTestGroup(TestGroup testGroup, string data) { if (string.IsNullOrEmpty(data)) { TempData["error"] = "At least one test case must be selected!"; return(View("CreateTestGroup")); } ModelState.Remove(nameof(TestGroup.TestCases)); ModelState.Remove(nameof(TestGroup.Created)); ModelState.Remove(nameof(TestGroup.Creator)); ModelState.Remove(nameof(TestGroup.Rating)); ModelState.Remove(nameof(TestGroup.TimeDificulty)); ModelState.Remove(nameof(TestGroup.SkillDificulty)); if (ModelState.IsValid) { ApplicationUserRepository <Company> userRepo = new ApplicationUserRepository <Company>(); Company currentUser = userRepo.GetByUserName(User.Identity.Name); var testCasesId = data.Split(','); TestCaseRepository testCaseRepo = new TestCaseRepository(); IList <TestCase> testCases = new List <TestCase>(); foreach (var testCaseId in testCasesId) { TestCase tempTestCase = testCaseRepo.GetByIdForCompanyNotInGroup(currentUser, Guid.Parse(testCaseId)); tempTestCase.IsInGroup = true; testCases.Add(tempTestCase); } DateTime latestAvailability = testCases.OrderByDescending(x => x.AvailableTo).First().AvailableTo; if (testGroup.AvailableTo < latestAvailability) { TempData["error"] = $"Available To must be equal or greater than availability of latest test case ({latestAvailability.ToShortDateString()})!"; return(View("CreateTestGroup")); } testGroup.TestCases = testCases; testGroup.SkillDificulty = testGroup.CountSkillDificulty(); testGroup.TimeDificulty = testGroup.CountTimeDificulty(); testGroup.RewardMultiplier = (testGroup.RewardMultiplier + 100) / 100; testGroup.Creator = currentUser; testGroup.Created = DateTime.Now; foreach (var testCase in testCases) { testCaseRepo.Update(testCase); } TestGroupRepository testGroupRepo = new TestGroupRepository(); testGroupRepo.Create(testGroup); TempData["success"] = "Test group was added"; } else { return(View("CreateTestGroup")); } return(RedirectToAction("CompanyTestGroups")); }