JsonDataProvider.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Runtime.Serialization.Json;
  8. using System.IO;
  9. namespace exam.Model
  10. {
  11. public class JsonDataProvider : IDataProvider
  12. {
  13. private List<Clothes> clothes;
  14. public JsonDataProvider()
  15. {
  16. var serializer = new DataContractJsonSerializer(typeof(Clothes[]));
  17. using(var sr = new StreamReader("./clothes.json"))
  18. {
  19. clothes = ((Clothes[])serializer.ReadObject(sr.BaseStream)).ToList();
  20. }
  21. }
  22. public IEnumerable<Clothes> GetClothes()
  23. {
  24. return clothes;
  25. }
  26. public IEnumerable<ClothesBrand> GetClothesBrands()
  27. {
  28. return clothes.DistinctBy(b => b.Brand).Select(b=>new ClothesBrand {Title = b.Title});
  29. }
  30. public IEnumerable<ClothesCategory> GetClothesCategories()
  31. {
  32. return clothes.DistinctBy(b => b.Category).Select(b => new ClothesCategory { Title = b.Title});
  33. }
  34. public IEnumerable<ClothesPrice> GetClothesPrices()
  35. {
  36. return new ClothesPrice[]
  37. {
  38. new ClothesPrice{Title = "До 10.000", PriceFrom = 0, PriceTo = 10000},
  39. new ClothesPrice{Title = "От 10.000 до 30.000", PriceFrom = 10000, PriceTo = 30000},
  40. new ClothesPrice{Title = "От 30.000 до 70.000", PriceFrom = 30000, PriceTo = 80000},
  41. new ClothesPrice{Title = "Дороже 70.000", PriceFrom = 70000, PriceTo = 300000}
  42. };
  43. }
  44. }
  45. }