123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- namespace CentralServer
- {
- class Program
- {
- static void Main(string[] args)
- {
- Listener();
- Console.Read();
- }
- public static async void Listener()
- {
- HttpListener listener = new HttpListener();
- listener.Prefixes.Add("http://127.0.0.1:8888/");
- listener.Start();
- Console.WriteLine("Start server...");
- while (true)
- {
- HttpListenerContext context = await listener.GetContextAsync();
- HttpListenerRequest request = context.Request;
- if (request.RawUrl.Contains("/getDataOfStore"))
- {
- string StoreID = request.RawUrl.Split('=')[1];
- var Store = db.Stores.Find(Convert.ToInt32(StoreID));
- HttpListenerResponse response = context.Response;
- if (Store is null)
- {
- byte[] buffer = Encoding.UTF8.GetBytes("Магазин не найден");
- response.ContentLength64 = buffer.Length;
- Stream output = response.OutputStream;
- output.Write(buffer, 0, buffer.Length);
- output.Close();
- }
- else
- {
- List<RootDataOfCashier> dataOfCashier = new List<RootDataOfCashier>();
- List<RootDataOfProductInStore> dataOfProductInStores = new List<RootDataOfProductInStore>();
- foreach (var item in Store.Cashiers)
- {
- dataOfCashier.Add(new RootDataOfCashier { CashierID = item.CashierID });
- }
- foreach (var item in Store.ProductInStores)
- {
- dataOfProductInStores.Add(new RootDataOfProductInStore
- {
- ProductID = item.Product.ProductID,
- NameProduct = item.Product.NameProduct,
- Weight = item.Product.Weight,
- Packaging = item.Product.Packaging,
- PriceOfOne = item.Product.PriceOfOne,
- BarCode = item.Product.BarCode,
- RemainsProduct= item.RemainsProduct
- });
- }
- RootDataOfStore rootDataOfStore = new RootDataOfStore();
- rootDataOfStore.Product = dataOfProductInStores;
- rootDataOfStore.Cashiers = dataOfCashier;
- rootDataOfStore.StoreID = Store.StoreID;
- rootDataOfStore.Address = Store.Address;
- string Json = JsonConvert.SerializeObject(rootDataOfStore);
- byte[] buffer = Encoding.UTF8.GetBytes(Json);
- response.ContentLength64 = buffer.Length;
- Stream output = response.OutputStream;
- output.Write(buffer, 0, buffer.Length);
- output.Close();
- }
- }
- }
- }
- }
- }
|