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 @@
-