using ComputerHardwareStoreContracts.BindingModels; using ComputerHardwareStoreContracts.ViewModels; using ComputerHardwareStoreDataModels.Enums; using HardwareShopContracts.ViewModels; using Microsoft.AspNetCore.Mvc; using StoreKeeperClient; using System.ComponentModel; namespace StorekeeperClient.Controllers { public class StorekeeperController : Controller { private readonly ILogger _logger; public StorekeeperController(ILogger logger) { _logger = logger; } public IActionResult CreateOrder() { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } ViewBag.Products = APIClient.GetRequest>($"api/product/getproducts?userId={APIClient.User.Id}"); return View(); } [HttpPost] public void CreateOrder(int product, int count, string sum) { if (APIClient.User == null) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } if (product <= 0) { throw new Exception("Некорректный идентификатор товара"); } if (count <= 0) { throw new Exception("Количество должно быть больше 0"); } if (Convert.ToDouble(sum.Replace('.', ',')) <= 0) { throw new Exception("Цена должна быть больше 0"); } //APIClient.PostRequest("api/order/createorder", new OrderBindingModel //{ // //UserId = APIClient.User.Id, // ProductId = product, // Count = count, // Sum = Convert.ToDouble(sum.Replace('.', ',')) //}); Response.Redirect("Orders"); } [HttpPost] public void DeleteOrder(int Id) { if (APIClient.User == null) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } if (Id <= 0) { throw new Exception("Некорректный идентификатор"); } APIClient.PostRequest("api/order/deleteorder", new OrderBindingModel { Id = Id }); Response.Redirect("Orders"); } [HttpPost] public void UpdateOrder(int id, int status) { if (APIClient.User == null) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } if (id <= 0) { throw new Exception("Некорректный идентификатор"); } if (status <= 0) { throw new Exception("Некорректный статус"); } APIClient.PostRequest("api/order/updatedata", new OrderBindingModel { Id = id, Status = (OrderStatus)status }); Response.Redirect("Orders"); } [HttpPost] public double Calc(int count, int product) { var prod = APIClient.GetRequest($"api/product/getproduct?id={product}"); double result = Math.Round(count * (prod?.Price ?? 1), 2); return result; } public IActionResult CreateProduct() { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } ViewBag.Components = APIClient.GetRequest>($"api/component/getcomponents?userId={APIClient.User.Id}"); return View(); } [HttpPost] public void CreateProduct([FromBody] ProductBindingModel productModel) { if (APIClient.User == null) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } if (string.IsNullOrEmpty(productModel.Name)) { throw new Exception("Название не должно быть пустым"); } if (productModel.Price <= 0) { throw new Exception("Цена должна быть больше 0"); } productModel.StoreKeeperId = APIClient.User.Id; APIClient.PostRequest("api/product/createproduct", productModel); } public IActionResult UpdateProduct(int productid) { if (APIClient.User == null) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } ViewBag.Components = APIClient.GetRequest>($"api/component/getcomponents?userId={APIClient.User.Id}"); return View(productid); } [HttpPost] public void UpdateProduct([FromBody] ProductBindingModel productModel) { if (APIClient.User == null) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } if (string.IsNullOrEmpty(productModel.Name)) { throw new Exception("Название не должно быть пустым"); } if (productModel.Price <= 0) { throw new Exception("Цена должна быть больше 0"); } productModel.StoreKeeperId = APIClient.User.Id; APIClient.PostRequest("api/product/updatedata", productModel); } [HttpGet] public ProductViewModel? GetProduct(int Id) { if (APIClient.User == null) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } if (Id <= 0) { throw new Exception($"Идентификатор товара не может быть меньше или равен 0"); } var result = APIClient.GetRequest($"api/product/getproduct?id={Id}"); return result; } [HttpGet] public Tuple>>? GetProductUpdate(int productid) { if (APIClient.User == null) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } var result = APIClient.GetRequest>>?>($"api/product/getproductupdate?id={productid}&userId={APIClient.User.Id}"); return result; } [HttpPost] public void DeleteProduct(int product) { if (APIClient.User == null) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } if (product <= 0) { throw new Exception($"Идентификатор товара не может быть меньше или равен 0"); } APIClient.PostRequest("api/product/deleteproduct", new ProductBindingModel { Id = product }); } public IActionResult LinkBuilds(int componentid) { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } ViewBag.Builds = APIClient.GetRequest>($"api/build/getbuilds"); return View(componentid); } [HttpPost] public void LinkBuilds([FromBody] ComponentBindingModel componentModel) { if (APIClient.User == null) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } componentModel.StoreKeeperId = APIClient.User.Id; APIClient.PostRequest($"api/component/updatedata", componentModel); } [HttpGet] public BuildViewModel? GetBuild(int buildId) { if (APIClient.User == null) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } if (buildId <= 0) { throw new Exception($"Идентификатор сборки не может быть меньше или равен 0"); } var result = APIClient.GetRequest($"api/build/getbuild?buildId={buildId}"); return result; } [HttpGet] public List>? GetComponentBuilds(int componentid) { if (APIClient.User == null) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } var result = APIClient.GetRequest>?>($"api/component/getcomponentbuilds?id={componentid}"); return result; } public IActionResult CreateComponent() { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } return View(); } [HttpPost] public void CreateComponent(string name, string cost) { if (APIClient.User == null) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } if (string.IsNullOrEmpty(name)) { throw new Exception("Название не должно быть пустым"); } if (string.IsNullOrEmpty(cost) || Convert.ToDouble(cost.Replace('.', ',')) <= 0) { throw new Exception("Цена должна быть больше 0"); } APIClient.PostRequest("api/component/createcomponent", new ComponentBindingModel { StoreKeeperId = APIClient.User.Id, Name = name, Cost = Convert.ToDouble(cost.Replace('.', ',')) }); Response.Redirect("Components"); } [HttpGet] public ComponentViewModel? GetComponent(int Id) { if (APIClient.User == null) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } var result = APIClient.GetRequest($"api/component/getcomponent?id={Id}"); if (result == null) { return default; } return result; } [HttpPost] public void UpdateComponent(string name, string cost, DateTime date, int component) { if (APIClient.User == null) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } if (component <= 0) { throw new Exception($"Идентификатор комплектующего не может быть меньше или равен 0"); } if (string.IsNullOrEmpty(name)) { throw new Exception($"Имя комплектующего не должно быть пустым"); } if (Convert.ToDouble(cost.Replace('.', ',')) <= 0) { throw new Exception($"Цена комплектующего не может быть меньше или равна 0"); } APIClient.PostRequest("api/component/updatecomponent", new ComponentBindingModel { Id = component, Name = name, Cost = Convert.ToDouble(cost.Replace('.', ',')), StoreKeeperId = APIClient.User.Id, }); Response.Redirect("Components"); } [HttpPost] public void DeleteComponent(int component) { if (APIClient.User == null) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } if (component <= 0) { throw new Exception($"Идентификатор комплектующего не может быть меньше или равен 0"); } APIClient.PostRequest("api/component/deletecomponent", new ComponentBindingModel { Id = component }); } public IActionResult MainStorekeeper() { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } return View(); } public IActionResult Components() { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } return View(APIClient.GetRequest>($"api/component/getcomponents?userId={APIClient.User.Id}")); } public IActionResult Products() { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } return View(APIClient.GetRequest>($"api/product/getproducts?userId={APIClient.User.Id}")); } public IActionResult Orders() { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } return View(APIClient.GetRequest>($"api/order/getorders?userId={APIClient.User.Id}")); } public IActionResult ListBuilds() { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } ViewBag.Products = APIClient.GetRequest>($"api/product/getproducts?userId={APIClient.User.Id}"); return View(); } [HttpPost] public int[]? ListBuilds([FromBody] ProductBindingModel productModel, [FromQuery] string format) { if (APIClient.User == null) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } if (string.IsNullOrEmpty(format)) { throw new FormatException("Неправильный формат файла"); } byte[]? file = APIClient.PostRequestWithResult($"api/report/buildproductreport?format={format}", productModel); return file!.Select(b => (int)b).ToArray(); } public IActionResult Report() { if (APIClient.User == null) { return Redirect("~/Home/Enter"); } return View(); } [HttpPost] public List Report([FromBody] ReportBindingModel reportModel) { if (APIClient.User == null) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } //reportModel.UserId = APIClient.User.Id; List? list = APIClient.PostRequestWithResult >("api/report/componentsreport", reportModel); return list!; } [HttpPost] public void ReportSendOnMail([FromBody] ReportBindingModel reportModel) { if (APIClient.User == null) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } reportModel.UserId = APIClient.User.Id; reportModel.UserEmail = APIClient.User.Login; APIClient.PostRequest("api/report/componentsreportsendonmail", reportModel); } [HttpGet] public List GetCommentsOnBuild(int buildId) { if (APIClient.User == null) { throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); } return APIClient.GetRequest>($"api/comment/GetCommentsOnBuild?buildId={buildId}")!; } } }