From ed9206d5168932492a9c837506d083c725f4ec7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Mon, 6 Mar 2023 02:22:10 +0400 Subject: [PATCH] =?UTF-8?q?.=20............=E2=80=A6=E2=80=A6=E2=80=A6?= =?UTF-8?q?=E2=80=A6=E2=80=A6=E2=80=A6=E2=80=A6=E2=80=A6.=E2=80=A6?= =?UTF-8?q?=E2=80=A6=E2=80=A6=E2=80=A6...=E2=80=9E--~*'=C2=AF=E2=80=A6?= =?UTF-8?q?=E2=80=A6.'\=20............=E2=80=A6=E2=80=A6=E2=80=A6=E2=80=A6?= =?UTF-8?q?.=E2=80=A6=E2=80=A6=E2=80=A6=E2=80=A6=E2=80=A6=E2=80=A6?= =?UTF-8?q?=E2=80=A6=E2=80=A6=20(=E2=80=9E-~~--=E2=80=9E=C2=B8=5F=E2=80=A6?= =?UTF-8?q?.,/=C3=AC=20...........=E2=80=A6=E2=80=A6.=E2=80=A6=E2=80=A6?= =?UTF-8?q?=E2=80=A6=E2=80=A6=E2=80=A6=E2=80=A6=E2=80=A6=E2=80=A6.=C2=B8?= =?UTF-8?q?=E2=80=9E-^"=C2=AF=20:=20:=20:=20:=20:=C2=B8-=C2=AF"=C2=AF/'=20?= =?UTF-8?q?............=E2=80=A6=E2=80=A6=E2=80=A6=E2=80=A6=E2=80=A6?= =?UTF-8?q?=E2=80=A6=E2=80=A6=E2=80=A6=C2=B8=E2=80=9E=E2=80=9E-^"=C2=AF=20?= =?UTF-8?q?:=20:=20:=20:=20:=20:=20:=20'\=C2=B8=E2=80=9E=E2=80=9E,-"=20**?= =?UTF-8?q?=C2=AF=C2=AF=C2=AF'^^~-=E2=80=9E=E2=80=9E=E2=80=9E----~^*'"?= =?UTF-8?q?=C2=AF=20:=20:=20:=20:=20:=20:=20:=20:=20:=20:=C2=B8-"=20.:.:.:?= =?UTF-8?q?.:.=E2=80=9E-^"=20:=20:=20:=20:=20:=20:=20:=20:=20:=20:=20:=20:?= =?UTF-8?q?=20:=20:=20:=20:=20:=E2=80=9E-"=20:.:.:.:.:.:.:.:.:.:.:=20:=20:?= =?UTF-8?q?=20:=20:=20:=20:=20:=20:=20:=20=C2=B8=E2=80=9E-^=C2=AF=20.::.:.?= =?UTF-8?q?:.:.:.:.:.:.=20:=20:=20:=20:=20:=20:=20:=20=C2=B8=E2=80=9E?= =?UTF-8?q?=E2=80=9E-^=C2=AF=20:.'=20:=20:=20'\=20:=20:=20:=20:=20:=20:=20?= =?UTF-8?q?:=20;=C2=B8=E2=80=9E=E2=80=9E-~"=C2=AF=20:.:.::=20:"-=E2=80=9E"?= =?UTF-8?q?"***/*'=C3=AC=C2=B8'=C2=AF=20:.':=20:=20:=20:=20:"-=E2=80=9E=20?= =?UTF-8?q?:=20:=20:"\=20.:.:.:=20:=20:=20:=20:"=20:=20:=20:=20:=20\,=20:.?= =?UTF-8?q?:=20:=20:=20:=20:=20:=20:=20:=20:=20:=20:=20:=20'=C3=8C=20:=20:?= =?UTF-8?q?=20:=20:=20:=20:=20:,=20:=20:=20:=20:=20:=20:/=20"-=E2=80=9E=5F?= =?UTF-8?q?::::=5F=E2=80=9E-*=5F=5F=E2=80=9E=E2=80=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/ShopController.cs | 16 ++ ConfectioneryShopApp/APIClient.cs | 51 ++++++ .../ConfectioneryShopApp.csproj | 8 + .../Controllers/HomeController.cs | 170 +++++++++++++++++- ConfectioneryShopApp/Program.cs | 4 + .../Views/Home/AddPastry.cshtml | 33 ++++ ConfectioneryShopApp/Views/Home/Create.cshtml | 24 +++ ConfectioneryShopApp/Views/Home/Delete.cshtml | 19 ++ ConfectioneryShopApp/Views/Home/Enter.cshtml | 15 ++ ConfectioneryShopApp/Views/Home/Index.cshtml | 66 ++++++- ConfectioneryShopApp/Views/Home/Update.cshtml | 70 ++++++++ .../Views/Shared/_Layout.cshtml | 21 +-- ConfectioneryShopApp/appsettings.json | 4 +- 13 files changed, 478 insertions(+), 23 deletions(-) create mode 100644 ConfectioneryShopApp/APIClient.cs create mode 100644 ConfectioneryShopApp/Views/Home/AddPastry.cshtml create mode 100644 ConfectioneryShopApp/Views/Home/Create.cshtml create mode 100644 ConfectioneryShopApp/Views/Home/Delete.cshtml create mode 100644 ConfectioneryShopApp/Views/Home/Enter.cshtml create mode 100644 ConfectioneryShopApp/Views/Home/Update.cshtml diff --git a/ConfectioneryRestApi/Controllers/ShopController.cs b/ConfectioneryRestApi/Controllers/ShopController.cs index fe42df0..ae3a625 100644 --- a/ConfectioneryRestApi/Controllers/ShopController.cs +++ b/ConfectioneryRestApi/Controllers/ShopController.cs @@ -4,6 +4,7 @@ using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.ViewModels; using ConfectioneryDataModels.Models; using Microsoft.AspNetCore.Mvc; +using System.Text.Json; namespace ConfectioneryRestApi.Controllers { @@ -35,6 +36,21 @@ namespace ConfectioneryRestApi.Controllers } } + [HttpGet] + public ShopViewModel? GetJsonShop(int id) + { + try + { + + return _logic.ReadElement(new() { Id = id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения магазина"); + throw; + } + } + [HttpPost] public void CRUDShop(Action action) { diff --git a/ConfectioneryShopApp/APIClient.cs b/ConfectioneryShopApp/APIClient.cs new file mode 100644 index 0000000..bcd90c5 --- /dev/null +++ b/ConfectioneryShopApp/APIClient.cs @@ -0,0 +1,51 @@ +using Newtonsoft.Json; +using System.Net.Http.Headers; +using System.Text; + +namespace ConfectioneryShopApp +{ + public class APIClient + { + private static readonly HttpClient _client = new(); + + public static bool IsAccessAllowed { get; set; } = false; + + public static string AccessPassword { get; private set; } = string.Empty; + + public static void Connect(IConfiguration configuration) + { + AccessPassword = configuration["PasswordToAccessShop"]; + _client.BaseAddress = new Uri(configuration["IPAddress"]); + _client.DefaultRequestHeaders.Accept.Clear(); + _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + } + + public static T? GetRequest(string requestUrl) + { + var response = _client.GetAsync(requestUrl); + var result = response.Result.Content.ReadAsStringAsync().Result; + if (response.Result.IsSuccessStatusCode) + { + return JsonConvert.DeserializeObject(result); + } + else + { + throw new Exception(result); + } + } + + public static void PostRequest(string requestUrl, T model) + { + var json = JsonConvert.SerializeObject(model); + var data = new StringContent(json, Encoding.UTF8, "application/json"); + + var response = _client.PostAsync(requestUrl, data); + + var result = response.Result.Content.ReadAsStringAsync().Result; + if (!response.Result.IsSuccessStatusCode) + { + throw new Exception(result); + } + } + } +} diff --git a/ConfectioneryShopApp/ConfectioneryShopApp.csproj b/ConfectioneryShopApp/ConfectioneryShopApp.csproj index 43b21cc..2955a3a 100644 --- a/ConfectioneryShopApp/ConfectioneryShopApp.csproj +++ b/ConfectioneryShopApp/ConfectioneryShopApp.csproj @@ -6,6 +6,14 @@ enable + + + + + + + + true diff --git a/ConfectioneryShopApp/Controllers/HomeController.cs b/ConfectioneryShopApp/Controllers/HomeController.cs index 362d866..b2ea4a5 100644 --- a/ConfectioneryShopApp/Controllers/HomeController.cs +++ b/ConfectioneryShopApp/Controllers/HomeController.cs @@ -1,6 +1,10 @@ -using ConfectioneryShopApp.Models; +using ConfectioneryShopApp; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryShopApp.Models; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; +using ConfectioneryContracts.SearchModels; namespace ConfectioneryShopApp.Controllers { @@ -15,11 +19,20 @@ namespace ConfectioneryShopApp.Controllers public IActionResult Index() { - return View(); + if (APIClient.IsAccessAllowed is false) + { + return Redirect("~/Home/Enter"); + } + return View(APIClient.GetRequest>($"api/shop/getshops")); } + [HttpGet] public IActionResult Privacy() { + if (APIClient.IsAccessAllowed is false) + { + return Redirect("~/Home/Enter"); + } return View(); } @@ -28,5 +41,158 @@ namespace ConfectioneryShopApp.Controllers { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } + + [HttpGet] + public IActionResult Enter() + { + return View(); + } + + [HttpPost] + public void Enter(string password) + { + if (string.IsNullOrEmpty(password)) + { + throw new Exception("Введите пароль"); + } + APIClient.IsAccessAllowed = password.Equals(APIClient.AccessPassword); + if (APIClient.IsAccessAllowed is false) + { + throw new Exception("Неверный пароль"); + } + Response.Redirect("Index"); + } + + [HttpGet] + public IActionResult Create() + { + return View(); + } + + [HttpPost] + public void Create(string name, string address, int maxCount) + { + if (APIClient.IsAccessAllowed is false) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + if (maxCount <= 0) + { + throw new Exception("Количество и сумма должны быть больше 0"); + } + if (string.IsNullOrEmpty(name)) + { + throw new Exception($"Имя магазина не должно быть пустым"); + } + if (string.IsNullOrEmpty(address)) + { + throw new Exception($"Адрес магазина не должен быть пустым"); + } + APIClient.PostRequest("api/shop/createshop", new ShopBindingModel + { + Name = name, + Address = address, + MaxCountPastries = maxCount, + }); + Response.Redirect("Index"); + } + + [HttpGet] + public Tuple GetTablePastriesFromShop(int shop) + { + var sh = APIClient.GetRequest($"api/shop/getjsonshop?id={shop}"); + var resultHtml = ""; + if (sh != null) + { + foreach (var (item, count) in sh.Pastries.Values) + { + resultHtml += ""; + resultHtml += $"{item?.PastryName ?? string.Empty}"; + resultHtml += $"{item?.Price ?? 0}"; + resultHtml += $"{count}"; + resultHtml += ""; + } + } + return Tuple.Create(resultHtml, sh); + } + + [HttpGet] + public IActionResult Update() + { + ViewBag.Shops = APIClient.GetRequest>("api/shop/getshops"); + return View(); + } + + [HttpPost] + public void Update(int shop, string name, string address) + { + if (APIClient.IsAccessAllowed is false) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + if (string.IsNullOrEmpty(name)) + { + throw new Exception($"Имя магазина не должно быть пустым"); + } + if (string.IsNullOrEmpty(address)) + { + throw new Exception($"Адрес магазина не должен быть пустым"); + } + APIClient.PostRequest("api/shop/updateshop", new ShopBindingModel + { + Id = shop, + Name = name, + Address = address, + }); + Response.Redirect("Index"); + } + + [HttpGet] + public IActionResult Delete() + { + ViewBag.Shops = APIClient.GetRequest>("api/shop/getshops"); + return View(); + } + + [HttpPost] + public void Delete(int shop) + { + if (APIClient.IsAccessAllowed is false) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + APIClient.PostRequest("api/shop/deleteshop", new ShopBindingModel + { + Id = shop, + }); + Response.Redirect("Index"); + } + + [HttpGet] + public IActionResult AddPastry() + { + ViewBag.Shops = APIClient.GetRequest>("api/shop/getshops"); + ViewBag.Pastries = APIClient.GetRequest>("api/main/getpastrylist"); + return View(); + } + + [HttpPost] + public void AddPastry(int shop, int pastry, int count) + { + if (APIClient.IsAccessAllowed is false) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + if (count <= 0) + { + throw new Exception("Количество должно быть больше 0"); + } + APIClient.PostRequest("api/shop/addpastryinshop", Tuple.Create( + new ShopSearchModel() { Id = shop }, + new PastryBindingModel() { Id = pastry }, + count + )); + Response.Redirect("Index"); + } } } \ No newline at end of file diff --git a/ConfectioneryShopApp/Program.cs b/ConfectioneryShopApp/Program.cs index 559dd3a..42a8f70 100644 --- a/ConfectioneryShopApp/Program.cs +++ b/ConfectioneryShopApp/Program.cs @@ -1,3 +1,5 @@ +using ConfectioneryShopApp; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. @@ -5,6 +7,8 @@ builder.Services.AddControllersWithViews(); var app = builder.Build(); +APIClient.Connect(builder.Configuration); + // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { diff --git a/ConfectioneryShopApp/Views/Home/AddPastry.cshtml b/ConfectioneryShopApp/Views/Home/AddPastry.cshtml new file mode 100644 index 0000000..5b4e5b5 --- /dev/null +++ b/ConfectioneryShopApp/Views/Home/AddPastry.cshtml @@ -0,0 +1,33 @@ +@using ConfectioneryContracts.ViewModels; +@using ConfectioneryDataModels.Models; + +@model Dictionary + +@{ + ViewData["Title"] = "AddPastry"; +} +
+

Пополнения магазина изделием

+
+
+
+
Выбранный магазин:
+
+ +
+
+
+
Выбранное изделие:
+
+ +
+
+
+
Количество:
+
+
+
+
+
+
+
\ No newline at end of file diff --git a/ConfectioneryShopApp/Views/Home/Create.cshtml b/ConfectioneryShopApp/Views/Home/Create.cshtml new file mode 100644 index 0000000..30bb21e --- /dev/null +++ b/ConfectioneryShopApp/Views/Home/Create.cshtml @@ -0,0 +1,24 @@ +@{ + ViewData["Title"] = "Create"; +} +
+

Создание магазина

+
+
+
+
Название магазина:
+
+
+
+
Адрес магазина:
+
+
+
+
Максимальное кол-во изделий:
+
+
+
+
+
+
+
diff --git a/ConfectioneryShopApp/Views/Home/Delete.cshtml b/ConfectioneryShopApp/Views/Home/Delete.cshtml new file mode 100644 index 0000000..96a21e1 --- /dev/null +++ b/ConfectioneryShopApp/Views/Home/Delete.cshtml @@ -0,0 +1,19 @@ +@{ + ViewData["Title"] = "Update"; +} +
+

Редактирование заказа

+
+
+
+
Выбранный магазин:
+
+ +
+
+
+
+
+
+ +
diff --git a/ConfectioneryShopApp/Views/Home/Enter.cshtml b/ConfectioneryShopApp/Views/Home/Enter.cshtml new file mode 100644 index 0000000..9c2eed6 --- /dev/null +++ b/ConfectioneryShopApp/Views/Home/Enter.cshtml @@ -0,0 +1,15 @@ +@{ + ViewData["Title"] = "Enter"; +} + +
+

Вход в приложение

+
+
+
+
Пароль:
+
+
+
+
+
\ No newline at end of file diff --git a/ConfectioneryShopApp/Views/Home/Index.cshtml b/ConfectioneryShopApp/Views/Home/Index.cshtml index d2d19bd..fd14390 100644 --- a/ConfectioneryShopApp/Views/Home/Index.cshtml +++ b/ConfectioneryShopApp/Views/Home/Index.cshtml @@ -1,8 +1,66 @@ -@{ - ViewData["Title"] = "Home Page"; +@using ConfectioneryContracts.ViewModels + +@model List + +@{ + ViewData["Title"] = "Home Page"; }
-

Welcome

-

Learn about building Web apps with ASP.NET Core.

+

Заказы

+
+ + +
+ @{ + if (Model == null) + { +

Введите пароль

+ return; + } + +

+ Создать магазин + Редактировать магазин + Удалить магазин + Добавить изделие в магазин +

+ + + + + + + + + + + @foreach (var item in Model) + { + + + + + + + } + +
+ Название магазина + + Адрес + + Дата открытия + + Максимальное количество изделий +
+ @Html.DisplayFor(modelItem => item.Name) + + @Html.DisplayFor(modelItem => item.Address) + + @Html.DisplayFor(modelItem => item.DateOpening) + + @Html.DisplayFor(modelItem => item.MaxCountPastries) +
+ }
diff --git a/ConfectioneryShopApp/Views/Home/Update.cshtml b/ConfectioneryShopApp/Views/Home/Update.cshtml new file mode 100644 index 0000000..2892ef5 --- /dev/null +++ b/ConfectioneryShopApp/Views/Home/Update.cshtml @@ -0,0 +1,70 @@ +@using ConfectioneryContracts.ViewModels; +@using ConfectioneryDataModels.Models; + +@model Dictionary + +@{ + ViewData["Title"] = "Update"; +} +
+

Редактирование заказа

+
+
+
+
Выбранный магазин:
+
+ +
+
+
+
Новое название магазина:
+
+
+
+
Адрес магазина:
+
+
+ + + + + + + + + + + +
+ Название изделия + + Цена + + Количество +
+ +
+
+
+
+
+ + \ No newline at end of file diff --git a/ConfectioneryShopApp/Views/Shared/_Layout.cshtml b/ConfectioneryShopApp/Views/Shared/_Layout.cshtml index 857f9ba..d5f6e80 100644 --- a/ConfectioneryShopApp/Views/Shared/_Layout.cshtml +++ b/ConfectioneryShopApp/Views/Shared/_Layout.cshtml @@ -3,10 +3,13 @@ - @ViewData["Title"] - ConfectioneryClientApp + @ViewData["Title"] - ConfectioneryShopApp - + + + +
@@ -22,15 +25,6 @@ - - - @@ -44,12 +38,9 @@
- © 2023 - ConfectioneryClientApp - Privacy + © 2023 - ConfectioneryShopApp - Privacy
- - - @await RenderSectionAsync("Scripts", required: false) \ No newline at end of file diff --git a/ConfectioneryShopApp/appsettings.json b/ConfectioneryShopApp/appsettings.json index dad7978..a6e728e 100644 --- a/ConfectioneryShopApp/appsettings.json +++ b/ConfectioneryShopApp/appsettings.json @@ -7,6 +7,6 @@ }, "AllowedHosts": "*", - "IPAddress": "http://localhost:5010/" - + "IPAddress": "http://localhost:5010/", + "PasswordToAccessShop": "12345" }