public void CloseExamData() { var center = repoOnSite.GetCenter(); var listSheet = repoOnSite.ListExamData(center._id).Where(x => !String.IsNullOrEmpty(x.TestRegisID)).ToList(); foreach (var sheet in listSheet) { if (sheet.LatestStatus == "DONE") { foreach (var q in sheet.RandomQuestions) { if (q.UserAnswer != null) { q.UserAnswer.IsCorrect = q.Choices.Where(x => x._id == q.UserAnswer._id).FirstOrDefault().IsCorrect; } } sheet.CorrectScore = sheet.RandomQuestions.Where(x => x.UserAnswer.IsCorrect.Value).Count(); sheet.InCorrectScore = sheet.RandomQuestions.Where(x => !x.UserAnswer.IsCorrect.Value).Count(); if (sheet.CorrectScore >= sheet.Subject.PassScore) { sheet.LatestStatus = "PASS"; } else { sheet.LatestStatus = "FAIL"; } var newStatus = new StatusExtension { _id = Guid.NewGuid().ToString(), ClientId = "admin", CreateDateTime = DateTime.Now, Status = sheet.LatestStatus }; sheet.StatusExtensions.Add(newStatus); sheet.ExamDateTime = DateTime.Now; } sheet.IsCloseExam = true; sheet.IsSync = true; } var regisList = repoOnSite.ListTestRegis(center._id).ToList(); foreach (var item in regisList) { item.IsCloseExam = true; } List <ExamSheetOnline> sheetForOnline = new List <ExamSheetOnline>(); var listReg = repoOnSite.GetTestRegisByIds(listSheet.Select(x => x.TestRegisID).ToList()); foreach (var item in listSheet) { var regis = listReg.Where(x => x._id == item.TestRegisID).FirstOrDefault(); var regisOnline = new TestRegistrationOnline { _id = regis._id, Title = regis.Title, FirstName = regis.FirstName, LastName = regis.LastName, SubjectCode = regis.SubjectCode, SubjectName = regis.SubjectName, ExamLanguage = regis.ExamLanguage, VoiceLanguage = regis.VoiceLanguage, RegDate = regis.RegDate, RegDateString = regis.RegDateString, ExpriedDate = regis.ExpriedDate, SiteId = regis.SiteId, CenterId = regis.CenterId, ForTestSystem = regis.ForTestSystem, ForPractice = regis.ForPractice, Status = regis.Status, ExamStatus = regis.ExamStatus, PID = regis.PID, ExamNumber = regis.ExamNumber, ExamPeriod = regis.ExamPeriod, AppointDate = regis.AppointDate, Email = regis.Email, Mobile = regis.Mobile, Address = regis.Address, MaxCount = regis.MaxCount, CertData = regis.CertData, LatestCount = regis.LatestCount, }; var sheet = new ExamSheetOnline { _id = item._id, Subject = item.Subject, TestReg = regisOnline, TestCount = item.TestCount, LatestStatus = item.LatestStatus, ExamDateTime = item.ExamDateTime, StatusExtensions = item.StatusExtensions, RandomQuestion = item.RandomQuestions, CenterId = item.CenterId, CorrectScore = item.CorrectScore, InCorrectScore = item.InCorrectScore, ReviewDuration = item.ReviewDuration, CreateDate = item.CreateDate, ClientId = item.ClientId }; sheetForOnline.Add(sheet); } var missTestReg = regisList.Where(x => !listSheet.Select(y => y.TestRegisID).Contains(x._id)).Select(regis => new TestRegistrationOnline() { _id = regis._id, Title = regis.Title, FirstName = regis.FirstName, LastName = regis.LastName, SubjectCode = regis.SubjectCode, SubjectName = regis.SubjectName, ExamLanguage = regis.ExamLanguage, VoiceLanguage = regis.VoiceLanguage, RegDate = regis.RegDate, RegDateString = regis.RegDateString, ExpriedDate = regis.ExpriedDate, SiteId = regis.SiteId, CenterId = regis.CenterId, ForTestSystem = regis.ForTestSystem, ForPractice = regis.ForPractice, Status = regis.Status, ExamStatus = regis.ExamStatus, PID = regis.PID, ExamNumber = regis.ExamNumber, ExamPeriod = regis.ExamPeriod, AppointDate = regis.AppointDate, Email = regis.Email, Mobile = regis.Mobile, Address = regis.Address, MaxCount = regis.MaxCount, CertData = regis.CertData, LatestCount = regis.LatestCount, }).ToList(); CloseExamRequest request = new CloseExamRequest() { ResultSheet = sheetForOnline, MissTestReg = missTestReg, }; using (var client = new WebClient()) { try { var json = Newtonsoft.Json.JsonConvert.SerializeObject(request); var dataByte = System.Text.Encoding.UTF8.GetBytes(json); client.Headers[HttpRequestHeader.ContentType] = "application/json"; dataByte = client.UploadData("http://eexamthaiex.azurewebsites.net/api/ExamSheet/UpdateExamSheetFromOnSite/", "POST", dataByte); //dataByte = client.UploadData("http://localhost:10585/api/ExamSheet/UpdateExamSheetFromOnSite/", "POST", dataByte); //update sheet repoOnSite.CloseSheet(listSheet); //update Regis repoOnSite.CloseTestRegis(regisList); } catch (Exception e) { throw new Exception(e.Message); } } }