JSONDataProvider.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.Serialization.Json;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace wpf_template.Model
  9. {
  10. public class JSONDataProvider : IDataProvider
  11. {
  12. private List<Product> products;
  13. public JSONDataProvider()
  14. {
  15. var serializer = new DataContractJsonSerializer(typeof(Product[]));
  16. using(var sr = new StreamReader("./products.json"))
  17. {
  18. products = ((Product[])serializer.ReadObject(sr.BaseStream)).ToList();
  19. }
  20. }
  21. public IEnumerable<Product> GetProducts()
  22. {
  23. return products;
  24. }
  25. public IEnumerable<ProductCategory> GetCategories()
  26. {
  27. return new ProductCategory[]
  28. {
  29. new ProductCategory {title = "Смартфон"},
  30. new ProductCategory {title = "Ноутбук"},
  31. new ProductCategory {title = "Смарт-Часы"},
  32. new ProductCategory {title = "Монитор"}
  33. };
  34. }
  35. public IEnumerable<ProductPrice> GetPrices()
  36. {
  37. return new ProductPrice[]
  38. {
  39. new ProductPrice{title="Все цены", priceFrom = 0, priceTo = 999999},
  40. new ProductPrice{title="До 10.000", priceFrom = 0, priceTo = 10000},
  41. new ProductPrice{title="От 10.000 до 20.000", priceFrom = 10000, priceTo = 20000},
  42. new ProductPrice{title="От 20.000 до 50.000", priceFrom = 20000, priceTo = 50000},
  43. new ProductPrice{title="Дороже 50.000", priceFrom = 50000, priceTo = 999999 }
  44. };
  45. }
  46. public IEnumerable<ProductCompany> GetCompanies()
  47. {
  48. return new ProductCompany[]
  49. {
  50. new ProductCompany{title = "Apple"},
  51. new ProductCompany{title = "Xiaomi"},
  52. new ProductCompany{title = "Huawei"},
  53. new ProductCompany{title = "POCO"},
  54. new ProductCompany{title = "Asus"},
  55. new ProductCompany{title = "Lenovo"},
  56. new ProductCompany{title = "Samsung"},
  57. new ProductCompany{title = "Acer"},
  58. new ProductCompany{title = "AOC"}
  59. };
  60. }
  61. }
  62. }