private void loadSTG() { Models.StudentGradeTestsResponse resp; Task <Models.StudentGradeTestsResponse> task = Task.Run(() => restService.GetCurrentStudentGradeTestByID(new Models.StudentGradeTestRequest() { ID = selectedStudent.ID })); task.ContinueWith(t => Device.BeginInvokeOnMainThread( async() => { if (t.IsFaulted) { await DisplayAlert("Connection error", "There was an error connecting to the server", "Try Again"); } resp = t.Result; if (!resp.Success) { await DisplayAlert(resp.Error.Message, resp.Error.Detail, "Try Again"); return; } sgt = resp.StudentGradeTests.itemValue; testsColl.ItemsSource = sgt.TestAttempts; } )); }
public async Task <ModifyStudentGradeTestResponse> ModifySGTest(Models.StudentGradeTests _sg) { if (authToken == null) { return new ModifyStudentGradeTestResponse() { Success = false, Error = ApiError.NotLoggedInError() } } ; Uri uri = new Uri(String.Format("{0}/{1}/{2}/{3}", api_url, "grades", "studentgradetest", "modify")); var request = new HttpRequestMessage { Method = HttpMethod.Post, RequestUri = uri, Content = new StringContent(JsonConvert.SerializeObject(_sg), Encoding.UTF8, "application/json") }; request.Headers.Add("Authorization", String.Format("Bearer {0}", authToken)); Task <HttpResponseMessage> response = Task.Run(() => client.SendAsync(request)); if (response.Wait(TimeSpan.FromSeconds(40))) { var responseBody = await response.Result.Content.ReadAsStringAsync().ConfigureAwait(false); ModifyStudentGradeTestResponse res = JsonConvert.DeserializeObject <Swimclub.Models.ModifyStudentGradeTestResponse>(responseBody); return(res); } else { return(new ModifyStudentGradeTestResponse() { Success = false, Error = ApiError.TimeOutResponse() }); } }
public async Task <ActionResult <Models.StudentGradeTestsResponse> > GetSGTestByID([FromBody] Models.StudentGradeTestRequest _req) { Models.StudentGradeTestsResponse response; if (User.Identity.IsAuthenticated) { var policyCheck = await authService.AuthorizeAsync(User, "CoachPolicy"); if (!policyCheck.Succeeded) { return(Unauthorized(response = new Models.StudentGradeTestsResponse() { Success = false, Error = Models.ApiError.UnAuthResponse() })); } } Entities.Student entity = await studentDbContext.Students.FirstOrDefaultAsync <Entities.Student>(e => e.ID == _req.ID); if (entity == null) { return(BadRequest(response = new Models.StudentGradeTestsResponse() { Success = false, Error = new Models.ApiError() { Message = "Cannot retrieve this record", Detail = "This student does not exist", Code = (int)Models.ServerResponse.ErrorCodes.STUDENT_NOT_EXIST } })); } Entities.Grade grade = await resourceDbContext.Grades.FirstOrDefaultAsync <Entities.Grade>(g => g.Number == entity.CurrentGradeNumber); if (grade == null) { return(BadRequest(response = new Models.StudentGradeTestsResponse() { Success = false, Error = new Models.ApiError() { Message = "Cannot retrieve this record", Detail = "This grade does not exist", Code = (int)Models.ServerResponse.ErrorCodes.MISC } })); } Entities.StudentGradeTests sgt = await resourceDbContext.StudentGradeTests.FirstOrDefaultAsync <Entities.StudentGradeTests>(s => s.StudentID == entity.ID && s.GradeID == grade.ID); if (sgt == null) { sgt = new Entities.StudentGradeTests() { GradeID = grade.ID, StudentID = entity.ID }; List <Models.TestAttempt> temp = new List <Models.TestAttempt>(); foreach (Models.Test test in grade.Tests) { temp.Add(new Models.TestAttempt() { AttemptedTest = test, Completed = false, DateTimeCompleted = null, Passed = false }); } sgt.TestAttempts = temp.ToArray(); await resourceDbContext.StudentGradeTests.AddAsync(sgt); await resourceDbContext.SaveChangesAsync(); } Models.StudentGradeTests resp = new Models.StudentGradeTests() { ID = sgt.ID, TestAttempts = sgt.TestAttempts }; return(Ok(new Models.StudentGradeTestsResponse() { Success = true, StudentGradeTests = new Models.standard.Item <Models.StudentGradeTests>() { itemValue = resp } })); }
public async Task <ActionResult <Models.ModifyStudentGradeTestResponse> > ModifySGTest([FromBody] Models.StudentGradeTests sg) { Models.ModifyStudentGradeTestResponse response; if (User.Identity.IsAuthenticated) { var policyCheck = await authService.AuthorizeAsync(User, "CoachPolicy"); if (!policyCheck.Succeeded) { return(Unauthorized(response = new Models.ModifyStudentGradeTestResponse() { Success = false, Error = Models.ApiError.UnAuthResponse() })); } } //Entities.Student entity = await studentDbContext.Students.FirstOrDefaultAsync<Entities.Student>(e => e.ID == sg.ID); Entities.StudentGradeTests ent = await resourceDbContext.StudentGradeTests.FirstOrDefaultAsync <Entities.StudentGradeTests>(e => e.ID == sg.ID); if (ent == null) { return(BadRequest(response = new Models.ModifyStudentGradeTestResponse() { Success = false, Error = new Models.ApiError() { Message = "Cannot modify this record", Detail = "This StudentGradeTest does not exist", Code = (int)Models.ServerResponse.ErrorCodes.MISC } })); } Entities.Student student = await studentDbContext.Students.FirstOrDefaultAsync <Entities.Student>(s => s.ID == ent.StudentID); if (student == null) { return(BadRequest(response = new Models.ModifyStudentGradeTestResponse() { Success = false, Error = new Models.ApiError() { Message = "Cannot modify this record", Detail = "This student does not exist", Code = (int)Models.ServerResponse.ErrorCodes.STUDENT_NOT_EXIST } })); } ent.TestAttempts = sg.TestAttempts; bool allDone = true; response = new Models.ModifyStudentGradeTestResponse(); foreach (Models.TestAttempt ta in ent.TestAttempts) { allDone = allDone & (ta.Completed && ta.Passed); } if (allDone) { int newGrade = student.CurrentGradeNumber + 1; Entities.Grade grade = await resourceDbContext.Grades.FirstOrDefaultAsync <Entities.Grade>(g => g.Number == newGrade); if (grade == null) { response.MaxGrade = true; response.AdvancedGrade = true; } else { response.AdvancedGrade = true; student.CurrentGradeNumber++; await studentDbContext.SaveChangesAsync(); } } await resourceDbContext.SaveChangesAsync(); response.Success = true; response.Student = new Models.standard.Item <Models.Student>() { itemValue = Entities.Student.GetStudent(student) }; return(Ok(response)); }