diff --git a/SushiBar/SushiBarClientApp/APIClient.cs b/SushiBar/SushiBarClientApp/APIClient.cs index 83268e4..dbebe39 100644 --- a/SushiBar/SushiBarClientApp/APIClient.cs +++ b/SushiBar/SushiBarClientApp/APIClient.cs @@ -10,14 +10,16 @@ namespace SushiBarClientApp public class APIClient { private static readonly HttpClient _client = new(); - public static ClientViewModel? Client { get; set; } = null; + + public static string? Password { get; set; } + public static void Connect(IConfiguration configuration) { _client.BaseAddress = new Uri(configuration["IPAddress"]); _client.DefaultRequestHeaders.Accept.Clear(); - _client.DefaultRequestHeaders.Accept.Add(new - MediaTypeWithQualityHeaderValue("application/json")); + _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); } + public static T? GetRequest(string requestUrl) { var response = _client.GetAsync(requestUrl); @@ -31,12 +33,24 @@ namespace SushiBarClientApp 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 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); + } + } + + public static void DeleteRequest(string requestUrl) + { + var response = _client.DeleteAsync(requestUrl); var result = response.Result.Content.ReadAsStringAsync().Result; if (!response.Result.IsSuccessStatusCode) { @@ -44,5 +58,5 @@ namespace SushiBarClientApp } } } - } + diff --git a/SushiBar/SushiBarClientApp/Controllers/HomeController.cs b/SushiBar/SushiBarClientApp/Controllers/HomeController.cs index b39499e..eb9bffa 100644 --- a/SushiBar/SushiBarClientApp/Controllers/HomeController.cs +++ b/SushiBar/SushiBarClientApp/Controllers/HomeController.cs @@ -10,144 +10,143 @@ namespace SushiBarClientApp.Controllers { public class HomeController : Controller { - private readonly ILogger _logger; - public HomeController(ILogger logger) - { - _logger = logger; - } - public IActionResult Index() - { - if (APIClient.Client == null) - { - return Redirect("~/Home/Enter"); - } - return - View(APIClient.GetRequest>($"api/main/getorders?clientId={APIClient.Client.Id}")); - } - [HttpGet] - public IActionResult Privacy() - { - 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)] - public IActionResult Error() - { - 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.Sushis = - APIClient.GetRequest>("api/main/getsushilist"); - return View(); - } - [HttpPost] - public void Create(int sushi, 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, - SushiId = sushi, - Count = count, - Sum = Calc(count, sushi) - }); - Response.Redirect("Index"); - } - [HttpPost] - public double Calc(int count, int sushi) - { - var prod = - APIClient.GetRequest($"api/main/getsushi?sushiId={sushi}" - ); - return count * (prod?.Price ?? 1); - } + private readonly ILogger _logger; + + public HomeController(ILogger logger) + { + _logger = logger; } - } + public IActionResult Index() + { + if (APIClient.Password == null) + { + return Redirect("~/Home/Enter"); + } + return View(APIClient.GetRequest>($"api/shop/getshoplist?password={APIClient.Password}")); + } + + [HttpGet] + public IActionResult Enter() + { + return View(); + } + + [HttpPost] + public void Enter(string password) + { + bool resout = APIClient.GetRequest($"/api/shop/authentication?password={password}"); + if (!resout) + { + Response.Redirect("../Home/Enter"); + return; + } + APIClient.Password = password; + Response.Redirect("Index"); + } + + [HttpGet] + public IActionResult Create() + { + if (APIClient.Password == null) + { + return Redirect("~/Home/Enter"); + } + return View("Shop"); + } + + [HttpPost] + public void Create(int id, string shopname, string adress, DateTime openingdate, int maxcount) + { + if (string.IsNullOrEmpty(shopname) || string.IsNullOrEmpty(adress)) + { + throw new Exception("Название или адрес не может быть пустым"); + } + if (openingdate == default(DateTime)) + { + throw new Exception("Дата открытия не может быть пустой"); + } + + APIClient.PostRequest($"api/shop/createshop?password={APIClient.Password}", new ShopBindingModel + { + Id = id, + ShopName = shopname, + Adress = adress, + OpeningDate = openingdate, + SushiMaxCount = maxcount + }); + Response.Redirect("Index"); + } + + [HttpGet] + public IActionResult Update(int Id) + { + if (APIClient.Password == null) + { + return Redirect("~/Home/Enter"); + } + return View("Shop", APIClient.GetRequest($"api/shop/getshop?shopId={Id}&password={APIClient.Password}")); + } + + [HttpPost] + public void Update(int id, string shopname, string adress, DateTime openingdate, int maxcount) + { + if (string.IsNullOrEmpty(shopname) || string.IsNullOrEmpty(adress)) + { + throw new Exception("Название или адрес не может быть пустым"); + } + if (openingdate == default(DateTime)) + { + throw new Exception("Дата открытия не может быть пустой"); + } + APIClient.PostRequest($"api/shop/updateshop?password={APIClient.Password}", new ShopBindingModel + { + Id = id, + ShopName = shopname, + Adress = adress, + OpeningDate = openingdate, + SushiMaxCount = maxcount + }); + Response.Redirect("../Index"); + } + + [HttpPost] + public void Delete(int Id) + { + APIClient.DeleteRequest($"api/shop/deleteshop?shopId={Id}&password={APIClient.Password}"); + Response.Redirect("../Index"); + } + + [HttpGet] + public IActionResult Supply() + { + if (APIClient.Password == null) + { + return Redirect("~/Home/Enter"); + } + + ViewBag.Shops = APIClient.GetRequest>($"api/shop/getshoplist?password={APIClient.Password}"); + ViewBag.Sushis = APIClient.GetRequest>($"api/main/getsushilist"); + return View(); + } + + [HttpPost] + public void Supply(int shop, int sushi, int count) + { + APIClient.PostRequest($"api/shop/makesypply?password={APIClient.Password}", new SupplyBindingModel + { + ShopId = shop, + SushiId = sushi, + Count = count + }); + Response.Redirect("Index"); + } + + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] + public IActionResult Error() + { + return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); + } + } +} diff --git a/SushiBar/SushiBarClientApp/Views/Home/Index.cshtml b/SushiBar/SushiBarClientApp/Views/Home/Index.cshtml index 843135f..b669ca6 100644 --- a/SushiBar/SushiBarClientApp/Views/Home/Index.cshtml +++ b/SushiBar/SushiBarClientApp/Views/Home/Index.cshtml @@ -1,75 +1,68 @@ @using SushiBarContracts.ViewModels -@model List +@model List @{ ViewData["Title"] = "Home Page"; }
-

Заказы

+

Магазины

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

Авторизируйтесь

- return; - } +
+

+ Создать магазин +

-

- Создать заказ -

- - - - - - - - - - - - - @foreach (var item in Model) - { - - - - - - - - - } - -
- Номер - - Суши - - Дата создания - - Количество - - Сумма - - Статус -
- @Html.DisplayFor(modelItem => item.Id) - - @Html.DisplayFor(modelItem => item.SushiName) - - @Html.DisplayFor(modelItem => item.DateCreate) - - @Html.DisplayFor(modelItem => item.Count) - - @Html.DisplayFor(modelItem => item.Sum) - - @Html.DisplayFor(modelItem => item.Status) -
- } + + + + + + + + + + + + + @foreach (var item in Model) + { + + + + + + + + + } + +
+ Номер + + Название + + Адрес + + Дата открытия + + Максимальная вместимость + + +
+ @Html.DisplayFor(modelItem => item.Id) + + @Html.DisplayFor(modelItem => item.ShopName) + + @Html.DisplayFor(modelItem => item.Adress) + + @Html.DisplayFor(modelItem => item.OpeningDate) + + @Html.DisplayFor(modelItem => item.SushiMaxCount) + + Изменить +
\ No newline at end of file diff --git a/SushiBar/SushiBarClientApp/Views/Home/Shop.cshtml b/SushiBar/SushiBarClientApp/Views/Home/Shop.cshtml new file mode 100644 index 0000000..3c7dca9 --- /dev/null +++ b/SushiBar/SushiBarClientApp/Views/Home/Shop.cshtml @@ -0,0 +1,74 @@ +@using SushiBarDataModels.Models; +@using SushiBarContracts.ViewModels; + @model ShopSushiViewModel + +@{ + ViewData["Title"] = "Shop"; +} + +
+ @{ + if (Model == null) + { +

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

+ } + else + { +

Изменение магазина

+ } + } +
+ +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + @{ + if (Model != null && Model.Shop != null) + { + + } + } +
+
+@{ + if (Model != null && Model.Shop != null) + { +
+
Содержимое магазина
+
+ + + + + + + + + @foreach (var item in Model.ShopSushi) + { + + + + + } + + +
НазваниеКоличество
@Html.DisplayFor(modelItem => item.Value.Sushi.SushiName)@Html.DisplayFor(modelItem => item.Value.Count)
+ } +} \ No newline at end of file diff --git a/SushiBar/SushiBarClientApp/Views/Home/Supply.cshtml b/SushiBar/SushiBarClientApp/Views/Home/Supply.cshtml new file mode 100644 index 0000000..a5a73ae --- /dev/null +++ b/SushiBar/SushiBarClientApp/Views/Home/Supply.cshtml @@ -0,0 +1,22 @@ +@{ + ViewData["Title"] = "Supply"; +} +
+

Создание поставки

+
+
+
+ + +
+
+ + +
+
+ + +
+ + Отмена +
diff --git a/SushiBar/SushiBarClientApp/Views/Shared/_Layout.cshtml b/SushiBar/SushiBarClientApp/Views/Shared/_Layout.cshtml index 386e637..3357f45 100644 --- a/SushiBar/SushiBarClientApp/Views/Shared/_Layout.cshtml +++ b/SushiBar/SushiBarClientApp/Views/Shared/_Layout.cshtml @@ -14,24 +14,18 @@