2023-04-25 09:27:24 +04:00

201 lines
5.4 KiB
C#

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
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
if (!ApiStores.Access)
{
return Redirect("~/Home/Enter");
}
return View(ApiStores.GetRequest<List<StoreViewModel>>($"api/Store/GetStores"));
}
[HttpGet]
public IActionResult Enter()
{
return View();
}
[HttpPost]
public void Enter(string password)
{
if (string.IsNullOrEmpty(password))
{
throw new Exception("Enter password");
}
ApiStores.GetAccess(password);
if (!ApiStores.Access)
{
throw new Exception($"Invalid password {password}");
}
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()
{
if (ApiStores.Access == false)
{
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)
{
if (ApiStores.Access == false)
{
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)
{
if (ApiStores.Access == false)
{
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()
{
if (ApiStores.Access == false)
{
return Redirect("~/Home/Enter");
}
ViewBag.Stores = ApiStores.GetRequest<List<StoreViewModel>>("api/store/GetStores");
ViewBag.Packages = ApiStores.GetRequest<List<SushiViewModel>>("api/main/GetSushiList");
return View();
}
[HttpPost]
public void Supply(int store, int package, int count)
{
if (ApiStores.Access == false)
{
throw new Exception("Need auth");
}
ApiStores.PostRequest("api/store/Supply", Tuple.Create(
new StoreSearchModel { Id = store },
new SushiViewModel { Id = package },
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 });
}
}