Добавил контроллеры для rest api (проверить)

This commit is contained in:
ujijrujijr 2024-04-27 22:46:09 +04:00
parent 9b2cb6e071
commit a141bd778e
6 changed files with 275 additions and 37 deletions

View File

@ -41,7 +41,6 @@ namespace ComputerShopDatabaseImplement.Models
{ {
if (_requestOrders == null) if (_requestOrders == null)
{ {
//!!!ТУТ ПРОПИСАТЬ ЛОГИКУ (НЕ ЗНАЮ ПОКА, КАК)
if (_requestOrders == null) if (_requestOrders == null)
{ {
_requestOrders = Orders.ToDictionary(x => x.OrderId, x => (x.Order as IOrderModel)); _requestOrders = Orders.ToDictionary(x => x.OrderId, x => (x.Order as IOrderModel));

View File

@ -0,0 +1,80 @@
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.BusinessLogicContracts;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace ComputerShopRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class OrderController : Controller
{
private readonly ILogger _logger;
private readonly IOrderLogic _logic;
public OrderController(IOrderLogic logic, ILogger<OrderController> logger)
{
_logger = logger;
_logic = logic;
}
[HttpGet]
public OrderViewModel? GetOrder(int id)
{
try
{
return _logic.ReadElement(new OrderSearchModel
{
Id = id
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения заказа");
throw;
}
}
[HttpPost]
public void CreateOrder(OrderBindingModel model)
{
try
{
_logic.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания заказа");
throw;
}
}
[HttpPost]
public void UpdateOrder(OrderBindingModel model)
{
try
{
_logic.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления заказа");
throw;
}
}
//!!!мб HttpPut
[HttpDelete]
public void DeleteOrder(OrderBindingModel model)
{
try
{
_logic.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления заказа");
throw;
}
}
}
}

View File

@ -0,0 +1,82 @@
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.BusinessLogicContracts;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace ComputerShopRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class RequestController : Controller
{
private readonly ILogger _logger;
private readonly IRequestLogic _logic;
public RequestController(IRequestLogic logic, ILogger<RequestController> logger)
{
_logger = logger;
_logic = logic;
}
[HttpGet]
public RequestViewModel? GetRequest(int id)
{
try
{
return _logic.ReadElement(new RequestSearchModel
{
Id = id
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения заказа");
throw;
}
}
[HttpPost]
public void CreateRequest(RequestBindingModel model)
{
try
{
_logic.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания заказа");
throw;
}
}
[HttpPost]
public void UpdateRequest(RequestBindingModel model)
{
try
{
_logic.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления заказа");
throw;
}
}
//!!!мб HttpPut
[HttpDelete]
public void DeleteRequest(RequestBindingModel model)
{
try
{
_logic.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления заказа");
throw;
}
}
//!!!МБ НУЖЕН ОТДЕЛЬНЫЙ МЕТОД ДЛЯ СВЯЗЫВАНИЯ ЗАЯВКИ СО СБОРКОЙ.
//!!!тогда его надо будет добавлять в IRequestLogic
}
}

View File

@ -0,0 +1,81 @@
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.BusinessLogicContracts;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
using System.Linq.Expressions;
namespace ComputerShopRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ShipmentController : Controller
{
private readonly ILogger _logger;
private readonly IShipmentLogic _logic;
public ShipmentController(IShipmentLogic logic, ILogger<ShipmentController> logger)
{
_logger = logger;
_logic = logic;
}
[HttpGet]
public ShipmentViewModel? GetShipment(int id)
{
try
{
return _logic.ReadElement(new ShipmentSearchModel
{
Id = id
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения партии товара");
throw;
}
}
[HttpPost]
public void CreateShipment(ShipmentBindingModel model)
{
try
{
_logic.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания партии товаров");
throw;
}
}
[HttpPost]
public void UpdateShipment(ShipmentBindingModel model)
{
try
{
_logic.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления партии товаров");
throw;
}
}
//!!!мб HttpPut
[HttpDelete]
public void DeleteShipment(ShipmentBindingModel model)
{
try
{
_logic.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления партии товаров");
throw;
}
}
}
}

View File

@ -1,4 +1,5 @@
using ComputerShopContracts.BusinessLogicContracts; using ComputerShopContracts.BindingModels;
using ComputerShopContracts.BusinessLogicContracts;
using ComputerShopContracts.SearchModels; using ComputerShopContracts.SearchModels;
using ComputerShopContracts.ViewModels; using ComputerShopContracts.ViewModels;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -17,6 +18,7 @@ namespace ComputerShopRestApi.Controllers
_logic = logic; _logic = logic;
_logger = logger; _logger = logger;
} }
//!!!мб тут нужен пароль
[HttpGet] [HttpGet]
public UserViewModel? Login(string login, string password) public UserViewModel? Login(string login, string password)
@ -26,8 +28,7 @@ namespace ComputerShopRestApi.Controllers
return _logic.ReadElement(new UserSearchModel return _logic.ReadElement(new UserSearchModel
{ {
Login = login, Login = login,
Email = login, Email = login
//Password = password
}); });
} }
catch (Exception ex) catch (Exception ex)
@ -36,5 +37,33 @@ namespace ComputerShopRestApi.Controllers
throw; throw;
} }
} }
[HttpPost]
public void Register(UserBindingModel model)
{
try
{
_logic.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка регистрации");
throw;
}
}
[HttpPost]
public void UpdateData(UserBindingModel model)
{
try
{
_logic.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления данных");
throw;
}
}
} }
} }

View File

@ -1,33 +0,0 @@
using Microsoft.AspNetCore.Mvc;
namespace ComputerShopRestApi.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}