ApiDataProvider.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Http;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Newtonsoft.Json;
  8. namespace mysql_connector2.res
  9. {
  10. public class ApiDataProvider : DBDataProvider
  11. {
  12. private static string baseUrl = "http://localhost:5000";
  13. public IEnumerable<Product> getProduct(int pageNum)
  14. {
  15. var client = new HttpClient();
  16. var body = client.GetStringAsync(
  17. $"{baseUrl}/product?pageNum={pageNum}"
  18. ).Result;
  19. return JsonConvert.DeserializeObject<Product[]>(body);
  20. }
  21. public void setOrder(string condition)
  22. {
  23. var client = new HttpClient();
  24. var jsonString = JsonConvert.SerializeObject(condition);
  25. var json = new StringContent(jsonString, Encoding.UTF8, "aplication/json");
  26. var body = client.PutAsync($"{baseUrl}/setOrder", json);
  27. }
  28. public List<ProductType> getProductTypes()
  29. {
  30. var client = new HttpClient();
  31. var body = client.GetStringAsync($"{baseUrl}/productType").Result;
  32. return JsonConvert.DeserializeObject<List<ProductType>>(body);
  33. }
  34. public void setMinCostForAgent(decimal minCost, int[] ids)
  35. {
  36. var client = new HttpClient();
  37. var jsonString = JsonConvert.SerializeObject(ids);
  38. var json = new StringContent(jsonString, Encoding.UTF8, "application/json");
  39. var body = client.PutAsync($"{baseUrl}/minCostForAgent/{minCost}", json);
  40. }
  41. public void saveProduct(Product product)
  42. {
  43. var client = new HttpClient();
  44. var jsonString = JsonConvert.SerializeObject(product);
  45. var json = new StringContent(jsonString, Encoding.UTF8, "application/json");
  46. if (product.ID != 0)
  47. {
  48. var body = client.PutAsync($"{baseUrl}/product", json);
  49. }
  50. else
  51. {
  52. var body = client.PostAsync($"{baseUrl}/product", json);
  53. }
  54. }
  55. public int saleCount(int ID)
  56. {
  57. var client = new HttpClient();
  58. var body = client.GetStringAsync($"{baseUrl}/saleCount/{ID}").Result;
  59. return JsonConvert.DeserializeObject<int>(body);
  60. }
  61. public void removePriceHistory(int ID)
  62. {
  63. var client = new HttpClient();
  64. var body = client.DeleteAsync($"{baseUrl}/removePriceHistory/{ID}");
  65. }
  66. public List<ProductMaterial> getProductMaterials(int ID)
  67. {
  68. var client = new HttpClient();
  69. var body = client.GetStringAsync($"{baseUrl}/productMaterial/{ID}").Result;
  70. return JsonConvert.DeserializeObject<List<ProductMaterial>>(body);
  71. }
  72. public void deleteProductMaterial(ProductMaterial productMaterial)
  73. {
  74. var client = new HttpClient();
  75. var body = client.DeleteAsync($"{baseUrl}/productMaterial");
  76. }
  77. public List<Material> getAvailableMaterials()
  78. {
  79. var client = new HttpClient();
  80. var body = client.GetStringAsync($"{baseUrl}/material").Result;
  81. return JsonConvert.DeserializeObject<List<Material>>(body);
  82. }
  83. public void addProductMaterial(ProductMaterial material)
  84. {
  85. var client = new HttpClient();
  86. var jsonString = JsonConvert.SerializeObject(material);
  87. var json = new StringContent(jsonString, Encoding.UTF8, "application/json");
  88. var body = client.PostAsync($"{baseUrl}/productMaterial", json).Result;
  89. }
  90. }
  91. }