0
0

ApiDataProvider.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 wpf_connection3.model
  9. {
  10. public class ApiDataProvider : DBDataProvider
  11. {
  12. private static string baseUrl = "http://localhost:5101";
  13. public IEnumerable<Product> getProduct(int pageNum)
  14. {
  15. var client = new HttpClient();
  16. var body = client.GetStringAsync($"{baseUrl}/product?pageNum={pageNum}").Result;
  17. return JsonConvert.DeserializeObject<Product[]>(body);
  18. }
  19. public void setOrder(string condition)
  20. {
  21. var client = new HttpClient();
  22. var jsonString = JsonConvert.SerializeObject(condition);
  23. var json = new StringContent(jsonString, Encoding.UTF8, "aplication/json");
  24. var body = client.PutAsync($"{baseUrl}/setOrder", json);
  25. }
  26. public List<ProductType> getProductTypes()
  27. {
  28. var client = new HttpClient();
  29. var body = client.GetStringAsync($"{baseUrl}/productType").Result;
  30. return JsonConvert.DeserializeObject<List<ProductType>>(body);
  31. }
  32. public void setMinCostForAgent(decimal minCost, int[] ids)
  33. {
  34. var client = new HttpClient();
  35. var jsonString = JsonConvert.SerializeObject(ids);
  36. var json = new StringContent(jsonString, Encoding.UTF8, "application/json");
  37. var body = client.PutAsync($"{baseUrl}/minCostForAgent/{minCost}", json);
  38. }
  39. public int getArticleCheck(string article, int id)
  40. {
  41. var client = new HttpClient();
  42. var body = client.GetStringAsync($"{baseUrl}/articleCheck/{article}/{id}").Result;
  43. return JsonConvert.DeserializeObject<int>(body);
  44. }
  45. public void saveProduct(Product product)
  46. {
  47. var client = new HttpClient();
  48. var jsonString = JsonConvert.SerializeObject(product);
  49. var json = new StringContent(jsonString, Encoding.UTF8, "application/json");
  50. if (product.ID != 0)
  51. {
  52. var body = client.PutAsync($"{baseUrl}/product", json);
  53. }
  54. else
  55. {
  56. var body = client.PostAsync($"{baseUrl}/product", json);
  57. }
  58. }
  59. public int saleCount(int ID)
  60. {
  61. var client = new HttpClient();
  62. var body = client.GetStringAsync($"{baseUrl}/saleCount/{ID}").Result;
  63. return JsonConvert.DeserializeObject<int>(body);
  64. }
  65. public void removeProductCostHistory(int ID)
  66. {
  67. var client = new HttpClient();
  68. var body = client.DeleteAsync($"{baseUrl}/removeProductCostHistory/{ID}");
  69. }
  70. public List<ProductMaterial> getProductMaterials(int ID)
  71. {
  72. var client = new HttpClient();
  73. var body = client.GetStringAsync($"{baseUrl}/productMaterial/{ID}").Result;
  74. return JsonConvert.DeserializeObject<List<ProductMaterial>>(body);
  75. }
  76. public void deleteProductMaterial(int productId, int materialId)
  77. {
  78. var client = new HttpClient();
  79. var body = client.DeleteAsync($"{baseUrl}/productMaterial/{productId}/{materialId}");
  80. }
  81. public List<Material> getMaterials()
  82. {
  83. var client = new HttpClient();
  84. var body = client.GetStringAsync($"{baseUrl}/material").Result;
  85. return JsonConvert.DeserializeObject<List<Material>>(body);
  86. }
  87. public void addProductMaterial(ProductMaterial material)
  88. {
  89. var client = new HttpClient();
  90. var jsonString = JsonConvert.SerializeObject(material);
  91. var json = new StringContent(jsonString, Encoding.UTF8, "application/json");
  92. var body = client.PostAsync($"{baseUrl}/productMaterial", json).Result;
  93. }
  94. }
  95. }