This commit is contained in:
kagbie3nn@mail.ru 2024-06-01 01:06:07 +04:00
parent 24f3bbc540
commit 1d145d370c
2 changed files with 195 additions and 2 deletions

View File

@ -1,6 +1,96 @@
namespace ZooRestApi.Controllers
using Microsoft.AspNetCore.Mvc;
using ZooContracts.BindingModels;
using ZooContracts.BuisnessLogicsContracts;
using ZooContracts.BusinessLogicsContracts;
using ZooContracts.SearchModels;
using ZooContracts.ViewModels;
namespace ZooRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class CostController
{
private readonly ILogger _logger;
private readonly ICostLogic _cost;
public CostController(ILogger<CostController> logger, ICostLogic cost)
{
_logger = logger;
_cost = cost;
}
[HttpGet]
public List<CostViewModel> GetAllCosts()
{
try
{
return _cost.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка маршрутов");
throw;
}
}
[HttpGet]
public Tuple<CostViewModel>? GetCost(int CostId)
{
try
{
var elem = _cost.ReadElement(new CostSearchModel { Id = CostId });
if (elem == null)
{
return null;
}
return Tuple.Create(elem);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения животного по id={Id}", CostId);
throw;
}
}
[HttpPost]
public bool CreateCost(CostBindingModel model)
{
try
{
return _cost.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Не удалось создать животного");
throw;
}
}
[HttpPost]
public bool UpdateCost(CostBindingModel model)
{
try
{
return _cost.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Не удалось обновить животное");
throw;
}
}
[HttpPost]
public bool DeleteCost(CostBindingModel model)
{
try
{
return _cost.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления животного");
throw;
}
}
}
}
}

View File

@ -120,6 +120,9 @@ namespace ZooShowEmployeeApp.Controllers
}
public IActionResult IndexPreserve()
{
if (APIEmployee.Employee == null)
@ -282,5 +285,105 @@ namespace ZooShowEmployeeApp.Controllers
{
return new PhysicalFileResult("C:\\gg\\excelfile.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}
public IActionResult IndexCost()
{
if (APIEmployee.Employee == null)
{
return Redirect("~/Home/Enter");
}
return View(APIEmployee.GetRequest<List<CostViewModel>>($"api/Cost/GetAllCosts?"));
}
public IActionResult CreateCost()
{
if (APIEmployee.Employee == null)
{
return Redirect("~/Home/Enter");
}
return View();
}
[HttpPost]
public void CreateCost(string name, int price)
{
if (APIEmployee.Employee == null)
{
throw new Exception("Вы как суда попали? Сюда вход только авторизованным");
}
if (string.IsNullOrEmpty(name) || price <= 0)
{
throw new Exception("Ошибка в введённых данных");
}
APIEmployee.PostRequest("api/Cost/CreateCost", new CostBindingModel
{
CostName = name,
CostPrice = price,
});
Response.Redirect("Index");
}
public IActionResult UpdateCost()
{
if (APIEmployee.Employee == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Costs = APIEmployee.GetRequest<List<CostViewModel>>($"api/Cost/GetAllCosts?");
return View();
}
[HttpPost]
public void UpdateCost(int pet, string name, double price)
{
if (APIEmployee.Employee == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (string.IsNullOrEmpty(name) || price <= 0)
{
throw new Exception("Ошибка введённых данных");
}
APIEmployee.PostRequest("api/Cost/updateCost", new CostViewModel
{
Id = pet,
CostName = name,
CostPrice = price,
});
Response.Redirect("Index");
}
[HttpGet]
public Tuple<CostViewModel>? GetCost(int CostId)
{
if (APIEmployee.Employee == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
var result = APIEmployee.GetRequest<Tuple<CostViewModel>>($"api/Cost/getCost?CostId={CostId}");
if (result == null)
{
return default;
}
return result;
}
[HttpPost]
public void IndexCost(int id)
{
if (APIEmployee.Employee == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
APIEmployee.PostRequest("api/Cost/deleteCost", new CostBindingModel
{
Id = id
});
Response.Redirect("Index");
}
}
}