private async void submitInspection() { List <ObjectModel.User> userContent = App.database.getUser(); ObjectModel.User user = userContent[userContent.Count - 1]; var majAList = new List <object>(); foreach (MajorAssembly majA in selected) { var subAList = new List <object>(); foreach (SubAssembly subA in majA.subAssemblies) { subAList.Add(new { subAssembly = subA.id, }); } majAList.Add(new { majorAssembly = majA.id, subAssemblies = subAList, }); } var client = new HttpClient(); var jsonRequest = new { machine = machine.id, timeScheduled = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz"), technician = "1", domainExpert = "1", majorAssemblies = majAList, }; var serializedJsonRequest = JsonConvert.SerializeObject(jsonRequest); HttpContent content = new StringContent(serializedJsonRequest, Encoding.UTF8, "application/json"); String site = "http://seng3150-api.wingmanwebdesign.com.au/inspections/bulk"; Uri apiSite = new Uri(site); List <ObjectModel.Token> token; token = App.database.getToken(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token[token.Count - 1].token); var response = await client.PostAsync(apiSite, content); if (response.IsSuccessStatusCode) { await DisplayAlert("Success", "Click Ok to start the inspection", "Ok"); Inspections.Inspection inspection = new Inspections.Inspection(); inspection = JsonConvert.DeserializeObject <Inspections.Inspection>(await response.Content.ReadAsStringAsync()); apiSite = new Uri("http://seng3150-api.wingmanwebdesign.com.au/inspections/" + inspection.id + "?include=majorAssemblies.majorAssembly,majorAssemblies.subAssemblies.subAssembly.tests,machine.model"); response = await client.GetAsync(apiSite); if (response.IsSuccessStatusCode) { inspection = JsonConvert.DeserializeObject <Inspections.Inspection>(await response.Content.ReadAsStringAsync()); } await Navigation.PushAsync(new InspectMachine(inspection)); } else { await DisplayAlert("Error", await response.Content.ReadAsStringAsync(), "Ok"); await Navigation.PushAsync(new SelectUserType()); } //await Navigation.PushAsync(new InspectMachine(inspection)); }
private async Task submitData() { try { using (var c = new HttpClient()) { List <ObjectModel.User> userContent = App.database.getUser(); ObjectModel.User user = userContent[userContent.Count - 1]; inspection.timeCompleted = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz"); var majAList = new List <object>(); foreach (MajorAssembly majA in inspection.majorAssemblies) { var subAList = new List <object>(); foreach (SubAssembly subA in majA.subAssemblies) { var commentsSubmit = new List <object>(); var actionItemSubmit = new object(); var technicianSubmit = (new { id = 1, }); var comment = (new { timeCommented = "", authorType = "", text = "", author = new ObjectModel.User(), }); var uniqueDetailsSubmit = (new { Test = "Type of Test", Value = "Result" }); uniqueDetailsSubmit = null; comment = null; actionItemSubmit = (new { status = "", issue = "", action = "", timeActioned = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz"), technician = technicianSubmit, }); actionItemSubmit = null; var machineGeneralTestSubmit = (new { }); var oilTestSubmit = (new { lead = 0, copper = 0, tin = 0, iron = 0, pq90 = 0, silicon = 0, sodium = 0, aluminium = 0, water = "0", viscosity = 0, comments = new List <object>(), actionItem = new object(), }); var wearTestSubmit = (new { description = "Desc", @new = "New", limit = "Limit", lifeLower = "Lower", lifeUpper = "Upper", smu = 0, timeStart = "", uniqueDetails = uniqueDetailsSubmit, comments = new List <object>(), actionItem = new object() }); machineGeneralTestSubmit = null; oilTestSubmit = null; wearTestSubmit = null; if (subA.subAssembly.tests[0].machineGeneral.test) { machineGeneralTestSubmit = (new { }); } if (subA.subAssembly.tests[0].oil.test) { if (subA.oilTest.comments != null) { comment = (new { timeCommented = subA.oilTest.comments[0].timeCommented, authorType = subA.oilTest.comments[0].authorType, text = subA.oilTest.comments[0].text, author = user, }); commentsSubmit = new List <object>(); commentsSubmit.Add(comment); } if (subA.oilTest.actionItem != null) { actionItemSubmit = (new { status = subA.oilTest.actionItem.status, issue = subA.oilTest.actionItem.issue, action = subA.oilTest.actionItem.action, timeActioned = subA.oilTest.actionItem.timeActioned, technician = technicianSubmit, }); } oilTestSubmit = (new { lead = subA.oilTest.lead, copper = subA.oilTest.copper, tin = subA.oilTest.tin, iron = subA.oilTest.iron, pq90 = subA.oilTest.pq90, silicon = subA.oilTest.silicon, sodium = subA.oilTest.sodium, aluminium = subA.oilTest.aluminium, water = subA.oilTest.water, viscosity = subA.oilTest.viscosity, comments = commentsSubmit, actionItem = actionItemSubmit }); } commentsSubmit = null; comment = null; if (subA.subAssembly.tests[0].wear.test) { if (subA.wearTest.comments != null) { comment = (new { timeCommented = subA.wearTest.comments[0].timeCommented, authorType = subA.wearTest.comments[0].authorType, text = subA.wearTest.comments[0].text, author = user, }); commentsSubmit = new List <object>(); commentsSubmit.Add(comment); } if (subA.wearTest.actionItem != null) { actionItemSubmit = (new { status = subA.wearTest.actionItem.status, issue = subA.wearTest.actionItem.issue, action = subA.wearTest.actionItem.action, timeActioned = subA.wearTest.actionItem.timeActioned, technician = technicianSubmit }); } uniqueDetailsSubmit = (new { Test = subA.wearTest.uniqueDetails.Test, Value = subA.wearTest.uniqueDetails.value.ToString(), }); wearTestSubmit = (new { description = subA.wearTest.description, @new = subA.wearTest.@new, limit = subA.wearTest.limit, lifeLower = subA.wearTest.lifeLower, lifeUpper = subA.wearTest.lifeUpper, smu = subA.wearTest.smu, timeStart = subA.wearTest.timeStart, uniqueDetails = uniqueDetailsSubmit, comments = commentsSubmit, actionItem = actionItemSubmit }); } subAList.Add(new { id = subA.id, comments = subA.comments, photos = subA.photos, machineGeneralTest = machineGeneralTestSubmit, oilTest = oilTestSubmit, wearTest = wearTestSubmit, }); } majAList.Add(new { id = majA.id, comments = majA.comments, photos = majA.photos, subAssemblies = subAList, }); } var client = new HttpClient(); var jsonRequest = new { timeStarted = inspection.timeStarted, timeCompleted = inspection.timeCompleted, majorAssemblies = majAList, }; var serializedJsonRequest = JsonConvert.SerializeObject(jsonRequest); HttpContent content = new StringContent(serializedJsonRequest, Encoding.UTF8, "application/json"); String site = "http://seng3150-api.wingmanwebdesign.com.au/inspections/" + inspection.id.ToString() + "/bulk"; Uri apiSite = new Uri(site); List <ObjectModel.Token> token; token = App.database.getToken(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token[token.Count - 1].token); var response = await client.PostAsync(apiSite, content); if (response.IsSuccessStatusCode) { await DisplayAlert("Success", "Data has been submitted!", "Okay"); await Navigation.PushAsync(new Menu()); } else { await DisplayAlert("Error", await response.Content.ReadAsStringAsync(), "Ok"); await Navigation.PushAsync(new SelectUserType()); } } Navigation.RemovePage(this); } catch (Exception e) { Debug.WriteLine(e); } }