using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; namespace mysql_connector2.res { public class ApiDataProvider : DBDataProvider { private static string baseUrl = "http://localhost:5000"; public IEnumerable getProduct(int pageNum) { var client = new HttpClient(); var body = client.GetStringAsync( $"{baseUrl}/product?pageNum={pageNum}" ).Result; return JsonConvert.DeserializeObject(body); } public void setOrder(string condition) { var client = new HttpClient(); var jsonString = JsonConvert.SerializeObject(condition); var json = new StringContent(jsonString, Encoding.UTF8, "aplication/json"); var body = client.PutAsync($"{baseUrl}/setOrder", json); } public List getProductTypes() { var client = new HttpClient(); var body = client.GetStringAsync($"{baseUrl}/productType").Result; return JsonConvert.DeserializeObject>(body); } public void setMinCostForAgent(decimal minCost, int[] ids) { var client = new HttpClient(); var jsonString = JsonConvert.SerializeObject(ids); var json = new StringContent(jsonString, Encoding.UTF8, "application/json"); var body = client.PutAsync($"{baseUrl}/minCostForAgent/{minCost}", json); } public void saveProduct(Product product) { var client = new HttpClient(); var jsonString = JsonConvert.SerializeObject(product); var json = new StringContent(jsonString, Encoding.UTF8, "application/json"); if (product.ID != 0) { var body = client.PutAsync($"{baseUrl}/product", json); } else { var body = client.PostAsync($"{baseUrl}/product", json); } } public int saleCount(int ID) { var client = new HttpClient(); var body = client.GetStringAsync($"{baseUrl}/saleCount/{ID}").Result; return JsonConvert.DeserializeObject(body); } public void removePriceHistory(int ID) { var client = new HttpClient(); var body = client.DeleteAsync($"{baseUrl}/removePriceHistory/{ID}"); } public List getProductMaterials(int ID) { var client = new HttpClient(); var body = client.GetStringAsync($"{baseUrl}/productMaterial/{ID}").Result; return JsonConvert.DeserializeObject>(body); } public void deleteProductMaterial(ProductMaterial productMaterial) { var client = new HttpClient(); var body = client.DeleteAsync($"{baseUrl}/productMaterial"); } public List getAvailableMaterials() { var client = new HttpClient(); var body = client.GetStringAsync($"{baseUrl}/material").Result; return JsonConvert.DeserializeObject>(body); } public void addProductMaterial(ProductMaterial material) { var client = new HttpClient(); var jsonString = JsonConvert.SerializeObject(material); var json = new StringContent(jsonString, Encoding.UTF8, "application/json"); var body = client.PostAsync($"{baseUrl}/productMaterial", json).Result; } } }