PIbd-21_RazubaevSM_Plumbing.../PlumbingRepair/PlumbingRepairShopApp/Controllers/HomeController.cs

208 lines
6.9 KiB
C#
Raw Normal View History

2024-05-11 20:51:02 +04:00
using PlumbingRepairContracts.BindingModels;
using PlumbingRepairContracts.ViewModels;
using PlumbingRepairClientApp.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using PlumbingRepairDataModels.Models;
using PlumbingRepairContracts.SearchModels;
namespace PlumbingRepairShopApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
if (!APIClient.AuthenticationDone)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<ShopViewModel>>($"api/shop/getshoplist"));
}
[HttpGet]
public IActionResult Privacy()
{
return View();
}
[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 password)
{
if (string.IsNullOrEmpty(password))
{
throw new Exception("Введите пароль");
}
if (!APIClient.Authentication(password))
{
throw new Exception("Неверный пароль");
}
Response.Redirect("Index");
}
[HttpGet]
public IActionResult Create()
{
return View();
}
[HttpPost]
public void Create(string shopName, string address, DateTime dateOpening, int maxCountWorks)
{
if (!APIClient.AuthenticationDone)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (string.IsNullOrEmpty(shopName))
{
throw new Exception("Название магазина не указано");
}
if (string.IsNullOrEmpty(address))
{
throw new Exception("Адрес магазина не указан");
}
if (maxCountWorks <= 0)
{
throw new Exception("Вместимость магазина должна быть больше нуля");
}
APIClient.PostRequest("api/shop/createshop", new ShopBindingModel
{
ShopName = shopName,
Address = address,
DateOpening = dateOpening,
maxCountWorks = maxCountWorks
});
Response.Redirect("Index");
}
[HttpGet]
public Tuple<ShopViewModel, string>? GetShopWorks(int shopId)
{
var result = APIClient.GetRequest<Tuple<ShopViewModel, List<WorkViewModel>, List<int>>>
($"api/shop/getshopworks?shopId={shopId}");
if (result == null)
{
return default;
}
string manufactureTable = "";
for (int i = 0; i < result.Item2.Count; i++)
{
var manufacture = result.Item2[i];
var count = result.Item3[i];
manufactureTable += "<tr>";
manufactureTable += $"<td>{manufacture.WorkName}</td>";
manufactureTable += $"<td>{manufacture.Price}</td>";
manufactureTable += $"<td>{count}</td>";
manufactureTable += "</tr>";
}
return Tuple.Create(result.Item1, manufactureTable);
}
[HttpGet]
public IActionResult Update()
{
if (!APIClient.AuthenticationDone)
{
return Redirect("~/Home/Enter");
}
ViewBag.Shops = APIClient.GetRequest<List<ShopViewModel>>($"api/shop/getshoplist");
return View();
}
[HttpPost]
public void Update(int shop, string shopName, string address, DateTime dateOpening, int maxCountWorks)
{
if (!APIClient.AuthenticationDone)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (string.IsNullOrEmpty(shopName))
{
throw new Exception("Название магазина не указано");
}
if (string.IsNullOrEmpty(address))
{
throw new Exception("Адрес магазина не указан");
}
if (maxCountWorks <= 0)
{
throw new Exception("Вместимость магазина должна быть больше нуля");
}
var shopWorks = APIClient.GetRequest<Dictionary<int, (IWorkModel, int)>>($"api/shop/getlistwork?shopId={shop}");
APIClient.PostRequest("api/shop/updateshop", new ShopBindingModel
{
Id = shop,
ShopName = shopName,
Address = address,
DateOpening = dateOpening.Date,
maxCountWorks = maxCountWorks,
ShopWorks = shopWorks
});
Response.Redirect("Index");
}
[HttpGet]
public IActionResult Delete()
{
if (!APIClient.AuthenticationDone)
{
return Redirect("~/Home/Enter");
}
ViewBag.Shops = APIClient.GetRequest<List<ShopViewModel>>($"api/shop/getshoplist");
return View();
}
[HttpPost]
public void Delete(int shop)
{
if (!APIClient.AuthenticationDone)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
APIClient.PostRequest("api/shop/deleteshop", new ShopBindingModel
{
Id = shop,
});
Response.Redirect("Index");
}
[HttpGet]
public IActionResult AddWork()
{
ViewBag.Shops = APIClient.GetRequest<List<ShopViewModel>>("api/shop/getshoplist");
ViewBag.Works = APIClient.GetRequest<List<WorkViewModel>>("api/main/getworklist");
return View();
}
[HttpPost]
public void AddWork(int shop, int work, int count)
{
if (!APIClient.AuthenticationDone)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (count <= 0)
{
throw new Exception("Количество должно быть больше 0");
}
APIClient.PostRequest("api/shop/storereplenishment", Tuple.Create(
new ShopSearchModel() { Id = shop },
new WorkViewModel() { Id = work },
count
));
Response.Redirect("Index");
}
}
}