/// <summary> /// Adds report to the stored report list /// </summary> /// <param name="report">the DriveReport to be added to the list</param> /// <returns>true on success, false on failure</returns> public static async Task<bool> AddReportToList(DriveReport report) { try { var content = await FileHandler.ReadFileContent(Definitions.ReportsFileName, Definitions.ReportsFolderName); var list = JsonConvert.DeserializeObject<List<DriveReport>>(content); if (list == null) { list = new List<DriveReport>(); } list.Add(report); var toBeWritten = JsonConvert.SerializeObject(list); var test = await FileHandler.WriteFileContent(Definitions.ReportsFileName, Definitions.ReportsFolderName, toBeWritten); if(test) Definitions.storedReportsCount = list.Count; return test; } catch(Exception e) { return false; } }
/// <summary> /// Removes specific report from the stored list /// </summary> /// <param name="report">the DriveReport to be removed from the list</param> /// <returns>the DriveReport list after removal of the specific DriveReport</returns> public static async Task<List<DriveReport>> RemoveReportFromList(DriveReport report) { try { var content = await FileHandler.ReadFileContent(Definitions.ReportsFileName, Definitions.ReportsFolderName); var list = JsonConvert.DeserializeObject<List<DriveReport>>(content); var item = list.FindIndex(x => x.Date == report.Date && x.Route.TotalDistance == report.Route.TotalDistance); list.RemoveAt(item); var toBeWritten = JsonConvert.SerializeObject(list); await FileHandler.WriteFileContent(Definitions.ReportsFileName, Definitions.ReportsFolderName, toBeWritten); Definitions.storedReportsCount = list.Count; return list; } catch (Exception e) { return new List<DriveReport>(); ; } }
/// <summary> /// Used to submit drivereport after finished drive. /// </summary> /// <param name="report">the report of the drive.</param> /// <param name="authorization">the token belonging to the user.</param> /// <param name="munUrl">the municipalicy url to be called</param> /// <returns>ReturnUserModel</returns> public static async Task<ReturnUserModel> SubmitDrive(DriveReport report, Authorization authorization, string munUrl) { var model = new ReturnUserModel(); try { var sendthis = new DriveSubmit(); sendthis.Authorization = authorization; sendthis.DriveReport = report; var json = JsonConvert.SerializeObject(sendthis); HttpClientHandler handler = new HttpClientHandler(); _httpClient = new HttpClient(handler); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, munUrl + "/report"); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); request.Content = stringContent; // Send request HttpResponseMessage response = await _httpClient.SendAsync(request); // Read response string jsonString = await response.Content.ReadAsStringAsync(); var isValid = IsValidJson(jsonString); if (response.IsSuccessStatusCode) { model.Error = null; } else if (string.IsNullOrEmpty(jsonString) || !isValid) { model.Error = new Error { Message = "Netværksfejl", ErrorCode = "404", }; model.User = null; } else { model.Error = DeserializeError(jsonString); model.User = null; } //return model; return model; } catch (Exception e) { model.Error = new Error { ErrorCode = "Exception", Message = "Der skete en uhåndteret fejl. Kontakt venligst Support" }; return model; } }