public static void UpdateArticle(Article article, ISystemFail error) { try { string webServiceUrl = string.Concat(AppKeys.WebServiceURL, AppKeys.WebServiceArticleControllerName); string response = Proxy.ProxyService.PutRequest(webServiceUrl, error, article); if (!string.IsNullOrEmpty(response) && !error.Error) { ApiResponse apiResponse = JsonConvert.DeserializeObject <ApiResponse>(response); if (apiResponse != null && apiResponse.success) { error.Message = "The article was succesfully updated"; } else { error.Error = true; error.Message = string.Concat("An error has ocurred updating the Article. Error:", apiResponse.Message); } } else { error.Error = true; error.Message = string.Concat("An error has ocurred updating the Article. Error:", error.Message); } } catch (Exception ex) { error.Error = true; error.Exception = ex; error.Message = string.Concat("An error has ocurred updating the Article. Error:", ex.Message); } }
public static List <Article> GetAllArticles(ISystemFail error) { List <Article> articles = null; try { string webServiceUrl = string.Concat(AppKeys.WebServiceURL, AppKeys.WebServiceArticleControllerName); string response = Proxy.ProxyService.GetRequestURlConcatParameters(webServiceUrl, error); if (!string.IsNullOrEmpty(response) && !error.Error) { GetAllArticleResponse apiResponse = JsonConvert.DeserializeObject <GetAllArticleResponse>(response); if (apiResponse.success) { articles = apiResponse.articles; } else { error.Error = true; error.Message = string.Concat("An error has ocurred obtaining the list of articles."); } } else { error.Error = true; error.Message = string.Concat("An error has ocurred obtaining the list of articles. Error:", error.Message); } } catch (Exception ex) { error.Error = true; error.Exception = ex; error.Message = string.Concat("An error has ocurred obtaining the list of articles. Error:", ex.Message); } return(articles); }
public static void DeleteArticle(int articleId, ISystemFail error) { try { string response = string.Empty; string webServiceUrl = string.Concat(AppKeys.WebServiceURL, AppKeys.WebServiceArticleControllerName); Dictionary <string, string> headers = new Dictionary <string, string>(); //Content Type Header headers.Add(AppKeys.ContentTypeHeaderName, AppKeys.ContentTypeHeaderValue); Article article = GetArticleById(articleId, error); if (article != null && !error.Error) { response = BLL.Proxy.ProxyService.DeleteRequest(webServiceUrl, error, null, article); if (!string.IsNullOrEmpty(response) && !error.Error) { ApiResponse apiResponse = JsonConvert.DeserializeObject <ApiResponse>(response); if (apiResponse != null && apiResponse.success) { error.Error = false; error.Message = "The article was succesfully deleted"; } else { error.Error = true; error.Message = string.Concat("An error has ocurred deleting the article. Error", apiResponse.Message); } } else { error.Error = true; error.Message = string.Concat("An error has ocurred deleting the article. Error:", error.Message); } } else { error.Error = true; error.Message = string.Concat("An error has ocurred deleting the article. Error:", error.Message); } } catch (Exception ex) { error.Error = true; error.Exception = ex; error.Message = string.Concat("An error has ocurred deleting the article. Error:", ex.Message); } }
public void UpdateStore(Store model, ISystemFail error) { try { storeRepository.Update(model); storeRepository.SaveRepository(error); } catch (Exception ex) { error.Error = true; error.Exception = ex; error.Message = ex.Message; } }
public void DeleteArticle(Article model, ISystemFail error) { try { articleRepository.Delete(model); articleRepository.SaveRepository(error); } catch (Exception ex) { error.Error = true; error.Exception = ex; error.Message = ex.Message; } }
public List <Article> GetAllArticles(ISystemFail error) { List <Article> articles = null; try { articles = articleRepository.GetAll().ToList(); } catch (Exception ex) { error.Error = true; error.Exception = ex; error.Message = ex.Message; } return(articles); }
public Article GetArticleById(int id, ISystemFail error) { Article article = null; try { article = articleRepository.GetById(id); } catch (Exception ex) { error.Error = true; error.Exception = ex; error.Message = ex.Message; } return(article); }
public List <Article> GettStoreArticles(int storeId, ISystemFail error) { List <Article> articles = null; try { articles = articleRepository.GetAll().Where(x => x.StoreId == storeId).ToList(); } catch (Exception ex) { error.Error = true; error.Exception = ex; error.Message = ex.Message; } return(articles); }
public List <Store> GetAllStores(ISystemFail error) { List <Store> stores = null; try { stores = storeRepository.GetAll().ToList(); } catch (Exception ex) { error.Error = true; error.Exception = ex; error.Message = ex.Message; } return(stores); }
public Store GetStoreById(int id, ISystemFail error) { Store store = null; try { store = storeRepository.GetById(id); } catch (Exception ex) { error.Error = true; error.Exception = ex; error.Message = ex.Message; } return(store); }
public void SaveRepository(ISystemFail error) { try { context.SaveChanges(); } catch (DbEntityValidationException dbValidationEx) { error.Error = true; error.Exception = dbValidationEx; error.Message = dbValidationEx.Message; } catch (Exception ex) { error.Error = true; error.Exception = ex; error.Message = ex.Message; } }
public static Article GetArticleById(int articleId, ISystemFail error) { Article article = null; try { string webServiceUrl = string.Concat(AppKeys.WebServiceURL, AppKeys.WebServiceArticleControllerName); Dictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("articleId", articleId.ToString()); string response = Proxy.ProxyService.GetRequestURlConcatParameters(webServiceUrl, error, parameters); if (!string.IsNullOrEmpty(response) && !error.Error) { GetArticleByIdResponse apiResponse = JsonConvert.DeserializeObject <GetArticleByIdResponse>(response); if (apiResponse.success) { article = apiResponse.article; } else { error.Error = true; error.Message = string.Concat("An error has ocurred obtaining the specific article"); } } else { error.Error = true; error.Message = string.Concat("An error has ocurred obtaining the specific article. Error:", error.Message); } } catch (Exception ex) { error.Error = true; error.Exception = ex; error.Message = string.Concat("An error has ocurred obtaining the specific article. Error:", ex.Message); } return(article); }
public static void CreateArticle(Article article, ISystemFail error) { try { string webServiceUrl = string.Concat(AppKeys.WebServiceURL, AppKeys.WebServiceArticleControllerName); Dictionary <string, string> headers = new Dictionary <string, string>(); //Content Type Header headers.Add(AppKeys.ContentTypeHeaderName, AppKeys.ContentTypeHeaderValue); Dictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("dummy", "dummy"); string response = Proxy.ProxyService.PostRequest(webServiceUrl, error, parameters, headers, article); if (!string.IsNullOrEmpty(response) && !error.Error) { ApiResponse apiResponse = JsonConvert.DeserializeObject <ApiResponse>(response); if (apiResponse != null && apiResponse.success) { error.Message = "The article was succesfully created"; } else { error.Error = true; error.Message = string.Concat("An error has ocurred creating the article. Error:", apiResponse.Message); } } else { error.Error = true; error.Message = string.Concat("An error has ocurred creating the article. Error:", error.Message); } } catch (Exception ex) { error.Error = true; error.Exception = ex; error.Message = string.Concat("An error has ocurred creating the article. Error:", ex.Message); } }
public static string GetRequest(string methodUrl, Dictionary <string, string> parametersDictionary, ISystemFail error) { string response = null; string parameters = string.Empty; if (parametersDictionary.Count > 0) { parameters = "?"; int counter = 0; foreach (var item in parametersDictionary) { if (counter == (parametersDictionary.Count - 1)) { parameters += string.Format("{0}={1}", item.Key, item.Value); } else { parameters += string.Format("{0}={1}&", item.Key, item.Value); } counter++; } } string completeURL = string.Format("{0}{1}/{2}{3}", WebServiceUrl, methodUrl, NameWordGet, parameters);//string.Concat(WebServiceUrl, methodUrl, parameters); try { using (var client = new System.Net.WebClient()) { Stream stream = client.OpenRead(completeURL); StreamReader reader = new StreamReader(stream); response = reader.ReadToEnd(); } } catch (WebException webEx) { error.Error = true; error.Excepcion = webEx; if (webEx.Response is HttpWebResponse) { switch (((HttpWebResponse)webEx.Response).StatusCode) { case HttpStatusCode.NotFound: response = null; error.Mensaje = "URL not found"; break; case HttpStatusCode.Forbidden: response = null; error.Mensaje = "Access to the URL not allowed"; break; default: response = null; error.Mensaje = "Exception ocurred while executing the Get request"; break; } } } catch (Exception ex) { error.Error = true; error.Excepcion = ex; error.Mensaje = string.Concat("Error al realizar la petición POST:", completeURL); response = null; } return(response); }
public static string PutRequest(string controllerName, string actionName, object requestBody, ISystemFail error, Dictionary <string, string> parametersDictionary = null) { string response = null; string parameters = string.Empty; if (parametersDictionary.Count > 0) { parameters = "?"; int counter = 0; foreach (var item in parametersDictionary) { if (counter == (parametersDictionary.Count - 1)) { parameters += string.Format("{0}={1}", item.Key, item.Value); } else { parameters += string.Format("{0}={1}&", item.Key, item.Value); } counter++; } } string completeURL = string.Concat(WebServiceUrl, controllerName, "/", actionName, parameters); Uri completeUri = new Uri(completeURL); try { using (var client = new System.Net.WebClient()) { client.Headers["Content-Type"] = "application/json"; ASCIIEncoding encoding = new ASCIIEncoding(); byte[] requestBodyBites = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(requestBody)); //byte[] requestBodyBites = encoding.GetBytes(JsonConvert.SerializeObject(requestBody)); byte[] byteArrayResponse = client.UploadData(completeUri, "PUT", requestBodyBites); response = System.Text.Encoding.UTF8.GetString(byteArrayResponse); } } catch (WebException webEx) { error.Error = true; error.Excepcion = webEx; if (webEx.Response is HttpWebResponse) { switch (((HttpWebResponse)webEx.Response).StatusCode) { case HttpStatusCode.NotFound: response = null; error.Mensaje = "URL not found"; break; case HttpStatusCode.Forbidden: response = null; error.Mensaje = "Access to the URL not allowed"; break; default: response = null; error.Mensaje = "Exception ocurred while executing the PUT request to the service"; break; } } } catch (Exception ex) { error.Error = true; error.Excepcion = ex; error.Mensaje = string.Concat("Error al realizar la peticion PUT:", completeURL); response = null; } return(response); }
public static string PostRequest(string requestURL, ISystemFail error, Dictionary <string, string> parametersDictionary = null, Dictionary <string, string> requestAditionalHeaders = null, object requestBody = null) { string response = null; string parameters = string.Empty; if (parametersDictionary != null && parametersDictionary.Count > 0) { parameters = "?"; int counter = 0; foreach (var item in parametersDictionary) { if (counter == (parametersDictionary.Count - 1)) { parameters += string.Format("{0}={1}", item.Key, item.Value); } else { parameters += string.Format("{0}={1}&", item.Key, item.Value); } counter++; } } string completeURL = string.Concat(requestURL, parameters); try { using (var client = new System.Net.WebClient()) { // client.Headers["Content-Type"] = "application/json"; client.Headers.Add("Accept-Language", " en-US"); //client.Headers.Add("Accept", " text/html, application/xhtml+xml, */*"); client.Headers.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"); if (requestAditionalHeaders != null && requestAditionalHeaders.Count > 0) { foreach (var item in requestAditionalHeaders) { client.Headers.Add(item.Key, item.Value); } } ASCIIEncoding encoding = new ASCIIEncoding(); byte[] byteArrayResponse = new byte[0]; if (requestBody != null) { byte[] requestBodyBites = encoding.GetBytes(JsonConvert.SerializeObject(requestBody)); byteArrayResponse = client.UploadData(completeURL, "POST", requestBodyBites); } else { NameValueCollection val = new NameValueCollection(); foreach (var pair in parametersDictionary) { val.Add(pair.Key, pair.Value.ToString()); } byteArrayResponse = client.UploadValues(requestURL, "POST", val); } response = System.Text.Encoding.UTF8.GetString(byteArrayResponse); } } catch (WebException webEx) { var listErrors = webEx.Message; var message = string.Join("; ", listErrors); error.Error = true; error.Message = message; error.Exception = webEx; if (webEx.Response is HttpWebResponse) { switch (((HttpWebResponse)webEx.Response).StatusCode) { case HttpStatusCode.NotFound: response = null; error.Message += string.Concat("Error en realizar la petición", "URL not found"); break; case HttpStatusCode.Forbidden: response = null; error.Message += string.Concat("Error en realizar la petición", "Access to the URL not allowed"); break; default: response = null; error.Message += string.Concat("Error en realizar la petición", "Exception ocurred while executing the Post request to the service"); break; } } } catch (Exception ex) { error.Error = true; error.Message = ex.Message; error.Exception = ex; response = null; } return(response); }
public static string GetRequestURlConcatParameters(string serviceURL, ISystemFail error, Dictionary <string, string> parametersDictionary = null, Dictionary <string, string> requestAditionalHeaders = null) { string response = null; string parameters = string.Empty; if (parametersDictionary != null && parametersDictionary.Count > 0) { parameters = "?"; int counter = 0; foreach (var item in parametersDictionary) { if (counter == (parametersDictionary.Count - 1)) { parameters += string.Format("{0}={1}", item.Key, item.Value); } else { parameters += string.Format("{0}={1}&", item.Key, item.Value); } counter++; } } string completeURL = string.Format("{0}{1}", serviceURL, parameters); try { using (var client = new System.Net.WebClient()) { if (requestAditionalHeaders != null && requestAditionalHeaders.Count > 0) { foreach (var item in requestAditionalHeaders) { client.Headers.Add(item.Key, item.Value); } } Stream stream = client.OpenRead(completeURL); StreamReader reader = new StreamReader(stream); response = reader.ReadToEnd(); } } catch (WebException webEx) { var listErrors = webEx.Message; var message = string.Join("; ", listErrors); error.Error = true; error.Message = message; error.Exception = webEx; if (webEx.Response is HttpWebResponse) { switch (((HttpWebResponse)webEx.Response).StatusCode) { case HttpStatusCode.NotFound: response = null; error.Message += string.Concat("Error en realizar la petición", "URL not found"); break; case HttpStatusCode.Forbidden: response = null; error.Message += string.Concat("Error en realizar la petición", "Access to the URL not allowed"); break; default: response = null; error.Message += string.Concat("Error en realizar la petición", "Exception ocurred while executing the Get request"); break; } } } catch (Exception ex) { error.Error = true; error.Message = ex.Message; error.Exception = ex; response = null; } return(response); }
public static string DeleteRequest(string requestURL, ISystemFail error, Dictionary <string, string> parametersDictionary = null, object requestBody = null) { string response = null; string parameters = string.Empty; if (parametersDictionary != null && parametersDictionary.Count > 0) { parameters = "?"; int counter = 0; foreach (var item in parametersDictionary) { if (counter == (parametersDictionary.Count - 1)) { parameters += string.Format("{0} = {1}", item.Key, item.Value); } else { parameters += string.Format("{0} = {1},", item.Key, item.Value); } counter++; } } string completeURL = string.Concat(requestURL, parameters); try { using (var client = new System.Net.WebClient()) { client.Headers["Content-Type"] = "application/json"; ASCIIEncoding encoding = new ASCIIEncoding(); byte[] requestBodyBites = encoding.GetBytes(JsonConvert.SerializeObject(requestBody)); byte[] byteArrayResponse = client.UploadData(completeURL, "DELETE", requestBodyBites); response = System.Text.Encoding.UTF8.GetString(byteArrayResponse); } } catch (WebException webEx) { var listErrors = webEx.Message; var message = string.Join("; ", listErrors); error.Error = true; error.Message = message; error.Exception = webEx; if (webEx.Response is HttpWebResponse) { switch (((HttpWebResponse)webEx.Response).StatusCode) { case HttpStatusCode.NotFound: response = null; error.Message += string.Concat("Error en realizar la petición", "URL not found"); break; case HttpStatusCode.Forbidden: response = null; error.Message += string.Concat("Error en realizar la petición", "Access to the URL not allowed"); break; default: response = null; error.Message += string.Concat("Error en realizar la petición", "Exception ocurred while executing the Post request to the service"); break; } } } catch (Exception ex) { error.Error = true; error.Message = ex.Message; error.Exception = ex; response = null; } return(response); }
public static string PostFile(string requestURL, ISystemFail error, string filePath, Dictionary <string, string> parametersDictionary = null, Dictionary <string, string> requestAditionalHeaders = null) { string response = null; string parameters = string.Empty; if (parametersDictionary != null && parametersDictionary.Count > 0) { parameters = "?"; int counter = 0; foreach (var item in parametersDictionary) { if (counter == (parametersDictionary.Count - 1)) { parameters += string.Format("{0}={1}", item.Key, item.Value); } else { parameters += string.Format("{0}={1}&", item.Key, item.Value); } counter++; } } string completeURL = string.Concat(requestURL, parameters); try { using (var client = new System.Net.WebClient()) { client.Headers.Add("Accept-Language", " en-US"); client.Headers.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"); if (requestAditionalHeaders != null && requestAditionalHeaders.Count > 0) { foreach (var item in requestAditionalHeaders) { client.Headers.Add(item.Key, item.Value); } } ASCIIEncoding encoding = new ASCIIEncoding(); byte[] byteArrayResponse = new byte[0]; byteArrayResponse = client.UploadFile(completeURL, "POST", filePath); response = System.Text.Encoding.UTF8.GetString(byteArrayResponse); } } catch (WebException webEx) { var listErrors = webEx.Message; var message = string.Join("; ", listErrors); error.Error = true; error.Message = message; error.Exception = webEx; if (webEx.Response is HttpWebResponse) { switch (((HttpWebResponse)webEx.Response).StatusCode) { case HttpStatusCode.NotFound: response = null; error.Message += string.Concat("Error en realizar la petición", "URL not found"); break; case HttpStatusCode.Forbidden: response = null; error.Message += string.Concat("Error en realizar la petición", "Access to the URL not allowed"); break; default: response = null; error.Message += string.Concat("Error en realizar la petición", "Exception ocurred while executing the Post request to the service"); break; } } } catch (Exception ex) { error.Error = true; error.Message = ex.Message; error.Exception = ex; response = null; } return(response); }