public static int _InsertTestResults(MCQTestResults testResults) { //Convert everything to a List of our Data Model TestResult because that guy is simpler to work with var dbTestResults = testResults.MCQTestResult.Select(r => Mapper.Map(r)).ToList(); var duplicateStudentNumbers = dbTestResults.GroupBy(r => r.StudentNumber) .Where(group => group.Count() > 1) .Select(group => group.Key); foreach (int studentNumber in duplicateStudentNumbers) { // Get max MarksObtained for this student var maxMarks = dbTestResults.Where(r => r.StudentNumber == studentNumber).Max(r => r.MarksObtained); //Hold on to that one TestResult which has max MarksObtained var testResultWithMaxMarks = dbTestResults.First(r => r.StudentNumber == studentNumber && r.MarksObtained == maxMarks); //Delete all other TestResult for this student dbTestResults.RemoveAll(r => r.StudentNumber == studentNumber); //Add the max TestResult back to the list dbTestResults.Add(testResultWithMaxMarks); } return(DataAccessLayer.InsertTestResults(dbTestResults)); }
public ImportResults Post(MCQTestResults testResults) { // If testResults is null then it means XML body was either empty or XML parsing failed. // Either way, we need to return appropriate HTTP error if (testResults == null) { HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.BadRequest); message.Content = new StringContent("XML data not in correct format."); throw new HttpResponseException(message); } return(MarkrApiBusinessLogic.ImportTestResults(testResults)); }
public static ImportResults ImportTestResults(MCQTestResults testResults) { int rowsAccepted = 0; foreach (var testResult in testResults.MCQTestResult) { rowsAccepted += InsertTestResult(testResult); } return(new ImportResults { RowsParsed = testResults.MCQTestResult.Count, RowsAccepted = rowsAccepted }); }