Program.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. public static gr672_pgvEntities db = new gr672_pgvEntities();
  14. static void Main(string[] args)
  15. {
  16. Listener();
  17. Console.Read();
  18. }
  19. public static async void Listener()
  20. {
  21. HttpListener listener = new HttpListener();
  22. listener.Prefixes.Add("http://127.0.0.1:8888/");
  23. listener.Start();
  24. Console.WriteLine("Start server...");
  25. while (true)
  26. {
  27. HttpListenerContext context = await listener.GetContextAsync();
  28. HttpListenerRequest request = context.Request;
  29. if (request.RawUrl.Contains("/getDataOfStore"))
  30. {
  31. string StoreID = request.RawUrl.Split('=')[1];
  32. int ID = Convert.ToInt32(StoreID);
  33. var Store = db.Stores.Find(ID);
  34. HttpListenerResponse response = context.Response;
  35. if (Store is null)
  36. {
  37. byte[] buffer = Encoding.UTF8.GetBytes("Магазин не найден");
  38. response.ContentLength64 = buffer.Length;
  39. Stream output = response.OutputStream;
  40. output.Write(buffer, 0, buffer.Length);
  41. output.Close();
  42. }
  43. else
  44. {
  45. List<RootDataOfCashier> dataOfCashier = new List<RootDataOfCashier>();
  46. foreach (var item in Store.Cashiers)
  47. {
  48. dataOfCashier.Add(new RootDataOfCashier { CashierID = item.CashierID });
  49. }
  50. RootDataOfStore rootDataOfStore = new RootDataOfStore();
  51. rootDataOfStore.Cashiers = dataOfCashier;
  52. rootDataOfStore.StoreID = Store.StoreID;
  53. rootDataOfStore.Address = Store.Address;
  54. string Json = JsonConvert.SerializeObject(rootDataOfStore);
  55. byte[] buffer = Encoding.UTF8.GetBytes(Json);
  56. response.ContentLength64 = buffer.Length;
  57. Stream output = response.OutputStream;
  58. output.Write(buffer, 0, buffer.Length);
  59. output.Close();
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }