2023-04-25 08:27:59 +04:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using SushiBarContracts.BindingModels;
|
|
|
|
|
using SushiBarContracts.SearchModels;
|
|
|
|
|
using SushiBarContracts.ViewModels;
|
|
|
|
|
using SushiBarStoresMvc.Models;
|
|
|
|
|
|
|
|
|
|
namespace SushiBarStoresMvc.Controllers;
|
|
|
|
|
|
|
|
|
|
public class HomeController : Controller
|
|
|
|
|
{
|
|
|
|
|
public IActionResult Index()
|
|
|
|
|
{
|
2023-04-25 09:27:24 +04:00
|
|
|
|
if (!ApiStores.Access)
|
|
|
|
|
{
|
|
|
|
|
return Redirect("~/Home/Enter");
|
|
|
|
|
}
|
2023-04-25 10:31:41 +04:00
|
|
|
|
return View(ApiStores.GetRequest<List<StoreViewModel>>("api/Store/GetStores"));
|
2023-04-25 08:27:59 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public IActionResult Enter()
|
|
|
|
|
{
|
|
|
|
|
return View();
|
|
|
|
|
}
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public void Enter(string password)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(password))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Enter password");
|
|
|
|
|
}
|
2023-04-25 09:27:24 +04:00
|
|
|
|
ApiStores.GetAccess(password);
|
|
|
|
|
if (!ApiStores.Access)
|
2023-04-25 08:27:59 +04:00
|
|
|
|
{
|
2023-04-25 09:27:24 +04:00
|
|
|
|
throw new Exception($"Invalid password {password}");
|
2023-04-25 08:27:59 +04:00
|
|
|
|
}
|
|
|
|
|
Response.Redirect("Index");
|
|
|
|
|
}
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public IActionResult Create()
|
|
|
|
|
{
|
|
|
|
|
if (ApiStores.Access == false)
|
|
|
|
|
{
|
|
|
|
|
return Redirect("~/Home/Enter");
|
|
|
|
|
}
|
|
|
|
|
return View();
|
|
|
|
|
}
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public void Create(string name, string address, DateTime date, int count)
|
|
|
|
|
{
|
|
|
|
|
if (ApiStores.Access == false)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Need auth");
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(name))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Enter name");
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(address))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Enter address");
|
|
|
|
|
}
|
|
|
|
|
if (count <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Enter count");
|
|
|
|
|
}
|
|
|
|
|
ApiStores.PostRequest("api/store/CreateStore", new StoreBindingModel
|
|
|
|
|
{
|
|
|
|
|
StoreName = name,
|
|
|
|
|
StoreAddress = address,
|
|
|
|
|
maxSushi = count,
|
|
|
|
|
OpeningDate = date
|
|
|
|
|
});
|
|
|
|
|
Response.Redirect("Index");
|
|
|
|
|
}
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public IActionResult Delete()
|
|
|
|
|
{
|
|
|
|
|
if (ApiStores.Access == false)
|
|
|
|
|
{
|
|
|
|
|
return Redirect("~/Home/Enter");
|
|
|
|
|
}
|
|
|
|
|
ViewBag.Stores = ApiStores.GetRequest<List<StoreViewModel>>("api/store/getstores");
|
|
|
|
|
return View();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public void Delete(int store)
|
|
|
|
|
{
|
|
|
|
|
if (ApiStores.Access == false)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Need auth");
|
|
|
|
|
}
|
|
|
|
|
ApiStores.PostRequest("api/store/DeleteStore", new StoreBindingModel
|
|
|
|
|
{
|
|
|
|
|
Id = store
|
|
|
|
|
});
|
|
|
|
|
Response.Redirect("Index");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public IActionResult Update()
|
|
|
|
|
{
|
2023-04-25 10:31:41 +04:00
|
|
|
|
if (!ApiStores.Access)
|
2023-04-25 08:27:59 +04:00
|
|
|
|
{
|
|
|
|
|
return Redirect("~/Home/Enter");
|
|
|
|
|
}
|
|
|
|
|
ViewBag.Stores = ApiStores.GetRequest<List<StoreViewModel>>("api/store/GetStores");
|
|
|
|
|
return View();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public void Update(int store, string name, string address, DateTime date, int count)
|
|
|
|
|
{
|
2023-04-25 10:31:41 +04:00
|
|
|
|
if (!ApiStores.Access)
|
2023-04-25 08:27:59 +04:00
|
|
|
|
{
|
|
|
|
|
throw new Exception("Need auth");
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(name))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Name of store must be not null");
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(address))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Address of store must be not null");
|
|
|
|
|
}
|
|
|
|
|
if (count <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Quantity of store must be more then zero");
|
|
|
|
|
}
|
|
|
|
|
ApiStores.PostRequest("api/store/UpdateData", new StoreBindingModel
|
|
|
|
|
{
|
|
|
|
|
Id = store,
|
|
|
|
|
StoreName = name,
|
|
|
|
|
StoreAddress = address,
|
|
|
|
|
OpeningDate = date,
|
|
|
|
|
maxSushi = count
|
|
|
|
|
});
|
|
|
|
|
Response.Redirect("Index");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public Tuple<StoreViewModel, string>? GetStore(int storeId)
|
|
|
|
|
{
|
2023-04-25 10:31:41 +04:00
|
|
|
|
if (!ApiStores.Access)
|
2023-04-25 08:27:59 +04:00
|
|
|
|
{
|
|
|
|
|
throw new Exception("Need auth");
|
|
|
|
|
}
|
|
|
|
|
var result = ApiStores.GetRequest<Tuple<StoreViewModel, List<Tuple<string, int>>>>($"api/store/GetStore?storeId={storeId}");
|
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
var table = "";
|
|
|
|
|
foreach (var (sushi, count) in result.Item2)
|
|
|
|
|
{
|
|
|
|
|
table += $"<tr><td>{sushi}</td><td>{count}</td></tr>";
|
|
|
|
|
}
|
|
|
|
|
return Tuple.Create(result.Item1, table);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IActionResult Supply()
|
|
|
|
|
{
|
2023-04-25 10:31:41 +04:00
|
|
|
|
if (!ApiStores.Access)
|
2023-04-25 08:27:59 +04:00
|
|
|
|
{
|
|
|
|
|
return Redirect("~/Home/Enter");
|
|
|
|
|
}
|
2023-04-25 10:31:41 +04:00
|
|
|
|
ViewBag.Stores = ApiStores.GetRequest<List<StoreViewModel>>("api/Store/GetStores");
|
|
|
|
|
ViewBag.Sushi = ApiStores.GetRequest<List<SushiViewModel>>("api/Main/GetProductList");
|
2023-04-25 08:27:59 +04:00
|
|
|
|
return View();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
2023-04-25 10:31:41 +04:00
|
|
|
|
public void Supply(int store, int sushi, int count)
|
2023-04-25 08:27:59 +04:00
|
|
|
|
{
|
2023-04-25 10:31:41 +04:00
|
|
|
|
if (!ApiStores.Access)
|
2023-04-25 08:27:59 +04:00
|
|
|
|
{
|
|
|
|
|
throw new Exception("Need auth");
|
|
|
|
|
}
|
|
|
|
|
ApiStores.PostRequest("api/store/Supply", Tuple.Create(
|
|
|
|
|
new StoreSearchModel { Id = store },
|
2023-04-25 10:31:41 +04:00
|
|
|
|
new SushiViewModel { Id = sushi },
|
2023-04-25 08:27:59 +04:00
|
|
|
|
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 });
|
|
|
|
|
}
|
|
|
|
|
}
|