Program.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace CentralServer
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. Listener();
  16. Console.Read();
  17. }
  18. public static async void Listener()
  19. {
  20. HttpListener listener = new HttpListener();
  21. listener.Prefixes.Add("http://127.0.0.1:8888/");
  22. listener.Start();
  23. Console.WriteLine("Start server...");
  24. while (true)
  25. {
  26. HttpListenerContext context = await listener.GetContextAsync();
  27. HttpListenerRequest request = context.Request;
  28. if (request.RawUrl.Contains("/getDataOfStore"))
  29. {
  30. string StoreID = request.RawUrl.Split('=')[1];
  31. var Store = db.Stores.Find(Convert.ToInt32(StoreID));
  32. HttpListenerResponse response = context.Response;
  33. if (Store is null)
  34. {
  35. byte[] buffer = Encoding.UTF8.GetBytes("Магазин не найден");
  36. response.ContentLength64 = buffer.Length;
  37. Stream output = response.OutputStream;
  38. output.Write(buffer, 0, buffer.Length);
  39. output.Close();
  40. }
  41. else
  42. {
  43. List<RootDataOfCashier> dataOfCashier = new List<RootDataOfCashier>();
  44. List<RootDataOfProductInStore> dataOfProductInStores = new List<RootDataOfProductInStore>();
  45. foreach (var item in Store.Cashiers)
  46. {
  47. dataOfCashier.Add(new RootDataOfCashier { CashierID = item.CashierID });
  48. }
  49. foreach (var item in Store.ProductInStores)
  50. {
  51. dataOfProductInStores.Add(new RootDataOfProductInStore
  52. {
  53. ProductID = item.Product.ProductID,
  54. NameProduct = item.Product.NameProduct,
  55. Weight = item.Product.Weight,
  56. Packaging = item.Product.Packaging,
  57. PriceOfOne = item.Product.PriceOfOne,
  58. BarCode = item.Product.BarCode,
  59. RemainsProduct= item.RemainsProduct
  60. });
  61. }
  62. RootDataOfStore rootDataOfStore = new RootDataOfStore();
  63. rootDataOfStore.Product = dataOfProductInStores;
  64. rootDataOfStore.Cashiers = dataOfCashier;
  65. rootDataOfStore.StoreID = Store.StoreID;
  66. rootDataOfStore.Address = Store.Address;
  67. string Json = JsonConvert.SerializeObject(rootDataOfStore);
  68. byte[] buffer = Encoding.UTF8.GetBytes(Json);
  69. response.ContentLength64 = buffer.Length;
  70. Stream output = response.OutputStream;
  71. output.Write(buffer, 0, buffer.Length);
  72. output.Close();
  73. }
  74. }
  75. }
  76. }
  77. }
  78. }