Костыли помогают

This commit is contained in:
Sergey Kozyrev 2024-05-01 15:33:09 +04:00
parent 0d19db0093
commit a72b3a46ca
9 changed files with 257 additions and 20 deletions

View File

@ -20,5 +20,6 @@ namespace SewingDressesContracts.ViewModels
[DisplayName("Вмещаемость")]
public int MaxCount { get; set; }
public Dictionary<int, (IDressModel, int)> ShopDresses { get; set; } = new();
public List<Tuple<string, int >> DressesCount { get;set;} = new();
}
}

View File

@ -27,6 +27,10 @@ namespace SewingDressesRestApi.Controllers
try
{
var shops = _logic.ReadList(null);
for (int i = 0; i < shops.Count; i++)
{
shops[i].DressesCount = shops[i].ShopDresses.Values.ToList().Select(x => (x.Item1.DressName, x.Item2).ToTuple()).ToList();
}
return shops;
}
catch (Exception ex)

View File

@ -0,0 +1,52 @@
using SewingDressesContracts.ViewModels;
using Newtonsoft.Json;
using System.Net.Http.Headers;
using System.Text;
namespace SewingDressesShopApp
{
public static class ApiClient
{
private static readonly HttpClient _client = new();
private static string password = string.Empty;
public static bool Access { get; private set; } = false;
public static void Connect(IConfiguration configuration)
{
password = configuration["Password"];
_client.BaseAddress = new Uri(configuration["IPAddress"]);
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Accept.Add(new
MediaTypeWithQualityHeaderValue("application/json"));
}
public static T? GetRequest<T>(string requestUrl)
{
var response = _client.GetAsync(requestUrl);
var result = response.Result.Content.ReadAsStringAsync().Result;
if (response.Result.IsSuccessStatusCode)
{
return JsonConvert.DeserializeObject<T>(result);
}
else
{
throw new Exception(result);
}
}
public static void PostRequest<T>(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);
}
}
public static bool CheckPassword(string _password)
{
return Access = (_password == password);
}
}
}

View File

@ -1,6 +1,9 @@
using Microsoft.AspNetCore.Mvc;
using SewingDressesShopApp.Models;
using System.Diagnostics;
using SewingDressesContracts.ViewModels;
using SewingDressesContracts.SearchModels;
using SewingDressesContracts.BindingModels;
namespace SewingDressesShopApp.Controllers
{
@ -15,18 +18,186 @@ namespace SewingDressesShopApp.Controllers
public IActionResult Index()
{
if (ApiClient.Access == false)
{
return Redirect("~/Home/Enter");
}
return
View(ApiClient.GetRequest<List<ShopViewModel>>($"api/shop/getshoplist"));
}
[HttpGet]
public IActionResult Enter()
{
return View();
}
[HttpPost]
public void Enter(string password)
{
if (string.IsNullOrEmpty(password))
{
throw new Exception("Введите пароль");
}
ApiClient.CheckPassword(password);
if (ApiClient.Access == false)
{
throw new Exception("Неправильный пароль");
}
Response.Redirect("Index");
}
public IActionResult Create()
{
if (ApiClient.Access == false)
{
return Redirect("~/Home/Enter");
}
return View();
}
public IActionResult Privacy()
[HttpPost]
public void Create(string name, string adress, DateTime dateOpen, int maxCount)
{
if (ApiClient.Access == false)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(adress) || maxCount <= 0)
{
throw new Exception("Ошибка в введенных данных");
}
ApiClient.PostRequest("api/shop/createshop", new ShopBindingModel
{
ShopName = name,
Adress = adress,
MaxCount = maxCount,
DateOpen = dateOpen
});
Response.Redirect("Index");
}
public IActionResult Delete()
{
if (ApiClient.Access == false)
{
return Redirect("~/Home/Enter");
}
ViewBag.Shops = ApiClient.GetRequest<List<ShopViewModel>>("api/shop/getshoplist");
return View();
}
[HttpPost]
public void Delete(int shop)
{
if (ApiClient.Access == false)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
ApiClient.PostRequest("api/shop/deleteshop", new ShopBindingModel
{
Id = shop,
Adress = "null",
DateOpen= DateTime.Now,
MaxCount = 1,
ShopName = "null",
ShopDresses = new()
}) ;
Response.Redirect("Index");
}
public IActionResult Update()
{
if (ApiClient.Access == false)
{
return Redirect("~/Home/Enter");
}
ViewBag.Shops = ApiClient.GetRequest<List<ShopViewModel>>("api/shop/getshoplist");
return View();
}
[HttpPost]
public void Update(int shop, string name, string adress, DateTime dateOpen, int maxCount)
{
if (ApiClient.Access == false)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(adress)
|| maxCount <= 0)
{
throw new Exception("Ошибка в введенных данных");
}
ApiClient.PostRequest("api/shop/updateshop", new ShopBindingModel
{
Id = shop,
ShopName = name,
Adress = adress,
DateOpen = dateOpen,
MaxCount = maxCount,
});
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 Tuple<ShopViewModel, string>? GetShop(int shopId)
{
if (ApiClient.Access == false)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
var result = ApiClient.GetRequest<Tuple<ShopViewModel, List<Tuple<string, int>>>>($"api/shop/getshop?shopid={shopId}");
if (result == null)
{
return default;
}
string table = "";
result.Item1.ShopDresses.Clear();
for (int i = 0; i < result.Item2.Count; i++)
{
var dress = result.Item2[i].Item1;
var count = result.Item2[i].Item2;
table += "<tr>";
table += $"<td>{dress}</td>";
table += $"<td>{count}</td>";
table += "</tr>";
}
return Tuple.Create(result.Item1, table);
}
public IActionResult Supply()
{
if (ApiClient.Access == false)
{
return Redirect("~/Home/Enter");
}
ViewBag.Shops = ApiClient.GetRequest<List<ShopViewModel>>("api/shop/getshoplist");
ViewBag.Dresses = ApiClient.GetRequest<List<DressViewModel>>("api/main/GetDressList");
return View();
}
[HttpPost]
public void Supply(int shopid, int dressid, int count)
{
if (ApiClient.Access == false)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
ApiClient.PostRequest("api/shop/makesupply", Tuple.Create(
new ShopSearchModel() { Id = shopid },
new DressViewModel() { Id = dressid },
count
));
Response.Redirect("Index");
}
}
}

View File

@ -1,10 +1,13 @@
using SewingDressesShopApp;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation();
var app = builder.Build();
ApiClient.Connect(builder.Configuration);
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{

View File

@ -6,8 +6,14 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="6.0.29" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SewingDressesContracts\SewingDressesContracts.csproj" />
<ProjectReference Include="..\SewingDressesRestApi\SewingDressesRestApi.csproj" />
</ItemGroup>
</Project>

View File

@ -13,7 +13,7 @@
</div>
<div class="row mb-3">
<div class="col-4">Название:</div>
<div class="col-8"><input type="text" id="shopname" name="shopname" /></div>
<div class="col-8"><input type="text" id="name" name="name" /></div>
</div>
<div class="row mb-3">
<div class="col-4">Адрес:</div>
@ -48,7 +48,8 @@
<input type="submit" value="Update" class="btn btn-success ps-5 pe-5" />
</div>
</form>
<<script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$('#shop').on('change', function () {
getData();
});
@ -61,18 +62,18 @@
});
if (selectedShop) {
$("#shopname").val(selectedShop.shopname);
$("#dateopen").val(new Date(selectedShop.dateopen).toISOString().substring(0, 16));
$("#name").val(selectedShop.shopName);
$("#dateopen").val(new Date(selectedShop.dateOpen).toISOString().substring(0, 16));
$("#adress").val(selectedShop.adress);
$("#maxcount").val(selectedShop.maxcount);
fillTable(selectedShop.ShopDresses);
$("#maxcount").val(selectedShop.maxCount);
fillTable(selectedShop.dressesCount);
}
}
function fillTable(shopDresses) {
function fillTable(dressesCount) {
$("#dressTable").empty();
for (var dress in shopDresses)
$("#dressTable").append('<tr><td>' + shopDresses[].item1 + '</td><td>' + shopDresses[dress].item2 + '</td></tr>');
for (var dress in dressesCount)
$("#dressTable").append('<tr><td>' + dressesCount[dress].item1 + '</td><td>' + dressesCount[dress].item2 + '</td></tr>');
}
</script>

View File

@ -3,18 +3,15 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - ShopApp</title>
<title>@ViewData["Title"] - SewingDressesShopsApp</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" />
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container-fluid">
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">SewingDressesShopApp</a>
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">SewingDressesShopsApp</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
@ -40,7 +37,7 @@
<footer class="border-top footer text-muted">
<div class="container">
&copy; 2024 - SewingDressesShopApp - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
&copy; 2024 - SewingDressesShopsApp - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</div>
</footer>
<script src="~/lib/jquery/dist/jquery.min.js"></script>

View File

@ -5,5 +5,7 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"IPAddress": "http://localhost:5299/",
"Password": "bober"
}