private void FormatResponseMessageForBulk(TerminologyAPIResponse <CreateFormularyAPIResponse> response, StringBuilder responseStringBuilder) { if (response.StatusCode != DataService.APIModel.StatusCode.Success) { responseStringBuilder.AppendLine($"{string.Join('\n', response.ErrorMessages.ToArray())} \n"); } else if (response.StatusCode == DataService.APIModel.StatusCode.Success) { if (response.Data.Status != null && response.Data.Status.ErrorMessages.IsCollectionValid()) { var errors = string.Join('\n', response.Data.Status.ErrorMessages.ToArray()); responseStringBuilder.AppendLine($"{errors} \n"); } else { if (response.Data != null && response.Data.Data != null) { response.Data.Data.Each(res => { responseStringBuilder.AppendLine($"{res.Code}-Success \n"); }); } } } }
private static async Task <TerminologyAPIResponse <T> > InvokeService <T>(string apiEndpoint, string token, HttpMethod method, dynamic payload = null, Func <string, bool> onError = null) { var response = new TerminologyAPIResponse <T> { StatusCode = StatusCode.Success }; string baseUrl = Environment.GetEnvironmentVariable("connectionString_TerminologyServiceBaseURL"); using (var client = new HttpClient()) { var url = baseUrl.EndsWith("/") ? $"{baseUrl}{apiEndpoint}" : $"{baseUrl}/{apiEndpoint}"; UriBuilder builder = new UriBuilder(url); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); client.Timeout = TimeSpan.FromHours(24); var requestMessage = new HttpRequestMessage(method, builder.Uri); if ((payload != null) && (method == HttpMethod.Post || method == HttpMethod.Put)) { var json = JsonConvert.SerializeObject(payload); var stringContent = new StringContent(json, UnicodeEncoding.UTF8, "application/json"); requestMessage.Content = stringContent; } var result = await client.SendAsync(requestMessage); using (StreamReader sr = new StreamReader(result.Content.ReadAsStreamAsync().Result)) { string content = sr.ReadToEnd(); if (!result.IsSuccessStatusCode) { response.StatusCode = StatusCode.Fail; response.ErrorMessages = new List <string> { content }; onError?.Invoke(content); //try //{ // Log.Error(result.ToString()); //} //catch { } //var isErrorHandledInCallback = onError?.Invoke(content); //if (isErrorHandledInCallback.GetValueOrDefault() == true) // return default(T); } else { var data = JsonConvert.DeserializeObject <T>(content); response.Data = data; } } return(response); } }
public async Task <IActionResult> UpdateFormulary(FormularyEditModel editedData) { BulkFormularyEditModel bulkEditedData = null; string token = HttpContext.Session.GetString("access_token"); var partialView = GetFormularyEditView(editedData.ProductType); if (editedData.IsBulkEdit) { bulkEditedData = editedData as BulkFormularyEditModel; partialView = GetFormularyEditViewForBulkEdit(editedData.ProductType); } if (!ModelState.IsValid) { await FillLookupsToViewBag(editedData.OriginalStatus); editedData.IsSaveSuccessful = false; return(PartialView(partialView, editedData)); } var apiModels = new List <FormularyHeaderAPIModel>(); var uniqueIds = editedData.FormularyVersionIds; foreach (var rec in uniqueIds) { FormularyHeaderAPIModel apiModel = null; if (editedData.IsBulkEdit) { var orginalObj = JsonConvert.DeserializeObject <BulkFormularyEditModel>(bulkEditedData.SerializedOriginalObj); var edDataDeserializedObj = _mapper.Map <BulkFormularyEditModel>(bulkEditedData); await MergeOnlyModified(edDataDeserializedObj, orginalObj, rec); apiModel = _mapper.Map <FormularyHeaderAPIModel>(edDataDeserializedObj); } else { apiModel = _mapper.Map <FormularyHeaderAPIModel>(editedData); } apiModel.FormularyVersionId = rec; apiModel.RecStatusCode = apiModel.RecStatusCode; //editedData.IsBulkEdit ? null : apiModel.RecStatusCode;//For bulk-edit mode - Record status is not editable apiModels.Add(apiModel); } TerminologyAPIResponse <UpdateFormularyAPIResponse> response = await TerminologyAPIService.UpdateFormulary(apiModels, token); if (response.StatusCode != DataService.APIModel.StatusCode.Success) { await FillLookupsToViewBag(editedData.OriginalStatus); _toastNotification.AddErrorToastMessage(string.Join('\n', response.ErrorMessages)); editedData.IsSaveSuccessful = false; return(PartialView(partialView, editedData)); } if (response.StatusCode == DataService.APIModel.StatusCode.Success) { if (response.Data.Status != null && response.Data.Status.ErrorMessages.IsCollectionValid()) { var errors = string.Join('\n', response.Data.Status.ErrorMessages); _toastNotification.AddInfoToastMessage(errors); await FillLookupsToViewBag(editedData.OriginalStatus); editedData.IsSaveSuccessful = true; return(PartialView(partialView, editedData)); } } _toastNotification.AddSuccessToastMessage("Successfully Updated Formulary details"); editedData.IsSaveSuccessful = true; return(Json(null)); }