From dba6bcc6a0a12802f245580657e2de7f88a6073b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=90=D0=BB=D0=B5=D0=B9?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Tue, 25 Apr 2023 00:01:49 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/ClientLogic.cs | 9 +- .../ConfectioneryClientApp/APIClient.cs | 44 ++++ .../ConfectioneryClientApp.csproj | 8 + .../Controllers/HomeController.cs | 121 +++++++++- .../ConfectioneryClientApp/Program.cs | 4 + .../Views/Home/Create.cshtml | 56 +++++ .../Views/Home/Enter.cshtml | 20 ++ .../Views/Home/Index.cshtml | 75 +++++- .../Views/Home/Privacy.cshtml | 32 ++- .../Views/Home/Register.cshtml | 25 ++ .../Views/Shared/_Layout.cshtml | 20 +- .../ConfectioneryClientApp/appsettings.json | 4 +- .../Models/IOrderModel.cs | 2 + .../Implements/ClientStorage.cs | 4 +- .../Implements/OrderStorage.cs | 43 ++-- .../20230424180732_migr3.Designer.cs | 212 +++++++++++++++++ .../Migrations/20230424180732_migr3.cs | 59 +++++ .../20230424190153_migr4.Designer.cs | 214 ++++++++++++++++++ .../Migrations/20230424190153_migr4.cs | 22 ++ .../ConfectioneryDatabaseModelSnapshot.cs | 14 +- .../Models/Order.cs | 4 + .../Models/Pastry.cs | 3 + .../Implements/OrderStorage.cs | 31 ++- .../Models/Order.cs | 6 + .../Implements/OrderStorage.cs | 42 ++-- .../Models/Order.cs | 4 + .../ConfectioneryView/FormClients.cs | 4 +- .../FormCreateOrder.Designer.cs | 29 ++- .../ConfectioneryView/FormCreateOrder.cs | 30 ++- .../ConfectioneryView/FormMain.Designer.cs | 12 +- Confectionery/ConfectioneryView/FormMain.cs | 1 + 31 files changed, 1071 insertions(+), 83 deletions(-) create mode 100644 Confectionery/ConfectioneryClientApp/APIClient.cs create mode 100644 Confectionery/ConfectioneryClientApp/Views/Home/Create.cshtml create mode 100644 Confectionery/ConfectioneryClientApp/Views/Home/Enter.cshtml create mode 100644 Confectionery/ConfectioneryClientApp/Views/Home/Register.cshtml create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Migrations/20230424180732_migr3.Designer.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Migrations/20230424180732_migr3.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Migrations/20230424190153_migr4.Designer.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Migrations/20230424190153_migr4.cs diff --git a/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/ClientLogic.cs b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/ClientLogic.cs index e365849..603ac54 100644 --- a/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/ClientLogic.cs +++ b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/ClientLogic.cs @@ -66,8 +66,7 @@ namespace ConfectioneryBusinessLogic.BusinessLogics public List? ReadList(ClientSearchModel? model) { _logger.LogInformation("ReadList. Email:{Email}.Id:{ Id} ", model?.Email, model?.Id); - var list = (model == null) ? _clientStorage.GetFullList() : - _clientStorage.GetFilteredList(model); + var list = (model == null) ? _clientStorage.GetFullList() : _clientStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); @@ -106,7 +105,11 @@ namespace ConfectioneryBusinessLogic.BusinessLogics { throw new ArgumentNullException("Нет логина клиента", nameof(model.Email)); } - _logger.LogInformation("Client. Id: {Id}, FIO: {fio}, email: {email}", model.Id, model.ClientFIO, model.Email); + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("У клиента отсутствует пароль", nameof(model.Email)); + } + _logger.LogInformation("Client. Id: {Id}, FIO: {fio}, email: {email}", model.Id, model.ClientFIO, model.Email, model.Password); var element = _clientStorage.GetElement(new ClientSearchModel { Email = model.Email, diff --git a/Confectionery/ConfectioneryClientApp/APIClient.cs b/Confectionery/ConfectioneryClientApp/APIClient.cs new file mode 100644 index 0000000..f6bcc7a --- /dev/null +++ b/Confectionery/ConfectioneryClientApp/APIClient.cs @@ -0,0 +1,44 @@ +using ConfectioneryContracts.ViewModels; +using Newtonsoft.Json; +using System.Net.Http.Headers; +using System.Text; + +namespace ConfectioneryClientApp +{ + public static class APIClient + { + private static readonly HttpClient _client = new(); + public static ClientViewModel? Client { get; set; } = null; + public static void Connect(IConfiguration configuration) + { + _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/Confectionery/ConfectioneryClientApp/ConfectioneryClientApp.csproj b/Confectionery/ConfectioneryClientApp/ConfectioneryClientApp.csproj index c78c9c7..bb56ef7 100644 --- a/Confectionery/ConfectioneryClientApp/ConfectioneryClientApp.csproj +++ b/Confectionery/ConfectioneryClientApp/ConfectioneryClientApp.csproj @@ -6,4 +6,12 @@ enable + + + + + + + + diff --git a/Confectionery/ConfectioneryClientApp/Controllers/HomeController.cs b/Confectionery/ConfectioneryClientApp/Controllers/HomeController.cs index 3368f26..9132dff 100644 --- a/Confectionery/ConfectioneryClientApp/Controllers/HomeController.cs +++ b/Confectionery/ConfectioneryClientApp/Controllers/HomeController.cs @@ -1,4 +1,6 @@ using ConfectioneryClientApp.Models; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; @@ -7,20 +9,49 @@ namespace ConfectioneryClientApp.Controllers public class HomeController : Controller { private readonly ILogger _logger; - public HomeController(ILogger logger) { _logger = logger; } - public IActionResult Index() { - return View(); + if (APIClient.Client == null) + { + return Redirect("~/Home/Enter"); + } + return View(APIClient.GetRequest>($"api/main/getorders?clientId={APIClient.Client.Id}")); } - + [HttpGet] public IActionResult Privacy() { - return View(); + if (APIClient.Client == null) + { + return Redirect("~/Home/Enter"); + } + return View(APIClient.Client); + } + [HttpPost] + public void Privacy(string login, string password, string fio) + { + if (APIClient.Client == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio)) + { + throw new Exception("Введите логин, пароль и ФИО"); + } + APIClient.PostRequest("api/client/updatedata", new ClientBindingModel + { + Id = APIClient.Client.Id, + ClientFIO = fio, + Email = login, + Password = password + }); + APIClient.Client.ClientFIO = fio; + APIClient.Client.Email = login; + APIClient.Client.Password = password; + Response.Redirect("Index"); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] @@ -28,5 +59,85 @@ namespace ConfectioneryClientApp.Controllers { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } + + [HttpGet] + public IActionResult Enter() + { + return View(); + } + + [HttpPost] + public void Enter(string login, string password) + { + if (string.IsNullOrEmpty(login) || + string.IsNullOrEmpty(password)) + { + throw new Exception("Введите логин и пароль"); + } + APIClient.Client = APIClient.GetRequest($"api/client/login?login={login}&password={password}"); + if (APIClient.Client == null) + { + throw new Exception("Неверный логин/пароль"); + } + Response.Redirect("Index"); + } + + [HttpGet] + public IActionResult Register() + { + return View(); + } + + [HttpPost] + public void Register(string login, string password, string fio) + { + if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio)) + { + throw new Exception("Введите логин, пароль и ФИО"); + } + APIClient.PostRequest("api/client/register", new ClientBindingModel + { + ClientFIO = fio, + Email = login, + Password = password + }); + Response.Redirect("Enter"); + return; + } + + [HttpGet] + public IActionResult Create() + { + ViewBag.Pastries = APIClient.GetRequest>("api/main/getpastrylist"); + return View(); + } + + [HttpPost] + public void Create(int pastry, int count) + { + if (APIClient.Client == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + if (count <= 0) + { + throw new Exception("Количество и сумма должны быть больше 0"); + } + APIClient.PostRequest("api/main/createorder", new OrderBindingModel + { + ClientId = APIClient.Client.Id, + PastryId = pastry, + Count = count, + Sum = Calc(count, pastry) + }); + Response.Redirect("Index"); + } + + [HttpPost] + public double Calc(int count, int pastry) + { + var prod = APIClient.GetRequest($"api/main/getpastry?pastryId={pastry}"); + return count * (prod?.Price ?? 1); + } } } \ No newline at end of file diff --git a/Confectionery/ConfectioneryClientApp/Program.cs b/Confectionery/ConfectioneryClientApp/Program.cs index 0727468..91bd171 100644 --- a/Confectionery/ConfectioneryClientApp/Program.cs +++ b/Confectionery/ConfectioneryClientApp/Program.cs @@ -1,3 +1,5 @@ +using ConfectioneryClientApp; + 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/Confectionery/ConfectioneryClientApp/Views/Home/Create.cshtml b/Confectionery/ConfectioneryClientApp/Views/Home/Create.cshtml new file mode 100644 index 0000000..5a21b25 --- /dev/null +++ b/Confectionery/ConfectioneryClientApp/Views/Home/Create.cshtml @@ -0,0 +1,56 @@ +@{ + ViewData["Title"] = "Create"; +} +
+

Создание заказа

+
+
+
+
Изделие:
+
+ +
+
+
+
Количество:
+
+ +
+
+
+
Сумма:
+
+ +
+
+
+
+
+ +
+
+
+@section scripts{ + +} \ No newline at end of file diff --git a/Confectionery/ConfectioneryClientApp/Views/Home/Enter.cshtml b/Confectionery/ConfectioneryClientApp/Views/Home/Enter.cshtml new file mode 100644 index 0000000..08aa5f2 --- /dev/null +++ b/Confectionery/ConfectioneryClientApp/Views/Home/Enter.cshtml @@ -0,0 +1,20 @@ +@{ + ViewData["Title"] = "Enter"; +} +
+

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

+
+
+
+
Логин:
+
+
+
+
Пароль:
+
+
+
+
+
+
+
diff --git a/Confectionery/ConfectioneryClientApp/Views/Home/Index.cshtml b/Confectionery/ConfectioneryClientApp/Views/Home/Index.cshtml index d2d19bd..8271f21 100644 --- a/Confectionery/ConfectioneryClientApp/Views/Home/Index.cshtml +++ b/Confectionery/ConfectioneryClientApp/Views/Home/Index.cshtml @@ -1,8 +1,75 @@ -@{ - 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.Id) + + @Html.DisplayFor(modelItem => item.PastryName) + + @Html.DisplayFor(modelItem => item.DateCreate) + + @Html.DisplayFor(modelItem => item.Count) + + @Html.DisplayFor(modelItem => item.Sum) + + @Html.DisplayFor(modelItem => item.Status) +
+ } +
\ No newline at end of file diff --git a/Confectionery/ConfectioneryClientApp/Views/Home/Privacy.cshtml b/Confectionery/ConfectioneryClientApp/Views/Home/Privacy.cshtml index af4fb19..eb2a8e5 100644 --- a/Confectionery/ConfectioneryClientApp/Views/Home/Privacy.cshtml +++ b/Confectionery/ConfectioneryClientApp/Views/Home/Privacy.cshtml @@ -1,6 +1,28 @@ -@{ - ViewData["Title"] = "Privacy Policy"; -} -

@ViewData["Title"]

+@using ConfectioneryContracts.ViewModels; -

Use this page to detail your site's privacy policy.

+@model ClientViewModel + +@{ + ViewData["Title"] = "Privacy Policy"; +} +
+

Личные данные

+
+
+
+
Логин:
+
+
+
+
Пароль:
+
+
+
+
ФИО:
+
+
+
+
+
+
+
\ No newline at end of file diff --git a/Confectionery/ConfectioneryClientApp/Views/Home/Register.cshtml b/Confectionery/ConfectioneryClientApp/Views/Home/Register.cshtml new file mode 100644 index 0000000..9d82ca6 --- /dev/null +++ b/Confectionery/ConfectioneryClientApp/Views/Home/Register.cshtml @@ -0,0 +1,25 @@ +@{ +ViewData["Title"] = "Register"; +} +
+

Регистрация

+
+
+
+
Логин:
+
+
+
+
Пароль:
+
+
+
+
ФИО:
+
+
+
+
+
+
+
\ No newline at end of file diff --git a/Confectionery/ConfectioneryClientApp/Views/Shared/_Layout.cshtml b/Confectionery/ConfectioneryClientApp/Views/Shared/_Layout.cshtml index ede5338..1ea063f 100644 --- a/Confectionery/ConfectioneryClientApp/Views/Shared/_Layout.cshtml +++ b/Confectionery/ConfectioneryClientApp/Views/Shared/_Layout.cshtml @@ -10,20 +10,26 @@
-