CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopRestAPI/Controllers/EmployeeController.cs
2024-05-30 23:49:53 +04:00

152 lines
3.6 KiB
C#

using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.BusinessLogicContracts;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataBaseImplement.Models;
using Microsoft.AspNetCore.Mvc;
namespace ElectronicsShopRestAPI.Controllers {
[Route("api/[controller]/[action]")]
[ApiController]
public class EmployeeController : Controller {
private readonly ILogger _logger;
private readonly IEmployeeLogic _logic;
private readonly ICostItemLogic _costItem;
private readonly IProductLogic _productLogic;
public EmployeeController(ILogger<EmployeeController> logger, IEmployeeLogic logic, ICostItemLogic costItem, IProductLogic productLogic) {
_logger = logger;
_logic = logic;
_costItem = costItem;
_productLogic = productLogic;
}
[HttpGet]
public EmployeeViewModel? Login(string login, string password) {
try {
return _logic.ReadElement(new EmployeeSearchModel {
Login = login,
Password = password
});
}
catch (Exception ex) {
_logger.LogError(ex, "Ошибка входа в систему");
throw;
}
}
[HttpPost]
public void Register(EmployeeBindingModel model) {
try {
_logic.Create(model);
}
catch (Exception ex) {
_logger.LogError(ex, "Ошибка регистрации");
throw;
}
}
[HttpPost]
public void UpdateData(EmployeeBindingModel model) {
try {
_logic.Update(model);
}
catch (Exception ex) {
_logger.LogError(ex, "Ошибка обновления данных");
throw;
}
}
[HttpPost]
public void CreateCostItem(CostItemBindingModel model) {
try {
_costItem.Create(model);
}
catch (Exception ex) {
_logger.LogError(ex, "Ошибка создания статьи");
throw;
}
}
[HttpGet]
public List<CostItemViewModel>? GetCostItems(int _employeeID) {
try {
return _costItem.ReadList(new CostItemSearchModel { EmployeeID = _employeeID });
}
catch (Exception ex) {
_logger.LogError(ex, $"Ошибка получения данных списка заказов сотрудника ID ={_employeeID}");
throw;
}
}
[HttpGet]
public CostItemViewModel? GetCostItem(int _costItemID) {
try {
return _costItem.ReadElement(new CostItemSearchModel { ID = _costItemID });
}
catch (Exception ex) {
_logger.LogError(ex, $"Ошибка получения статьи затрат ID={_costItemID}");
throw;
}
}
[HttpPost]
public void EditCostItem(CostItemBindingModel model) {
try {
_costItem.Update(model);
}
catch (Exception ex) {
_logger.LogError(ex, "Ошибка обновления данных");
throw;
}
}
[HttpPost]
public void DeleteCostItem(CostItemBindingModel model) {
try {
_costItem.Delete(model);
}
catch (Exception ex) {
_logger.LogError(ex, "Ошибка удаления статьи затрат");
throw;
}
}
[HttpPost]
public void CreateProduct(ProductBindingModel model) {
try {
_productLogic.Create(model);
}
catch(Exception ex) {
_logger.LogError(ex, "Ошибка создания товара");
throw;
}
}
[HttpPost]
public void EditProduct(ProductBindingModel model) {
try {
_productLogic.Update(model);
}
catch (Exception ex) {
_logger.LogError(ex, "Ошибка обновления товара");
throw;
}
}
[HttpPost]
public void DeleteProduct(ProductBindingModel model) {
try {
_productLogic.Delete(model);
}
catch (Exception ex) {
_logger.LogError(ex, "Ошибка удаления товара");
throw;
}
}
}
}