CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopRestAPI/Controllers/MainController.cs

252 lines
8.8 KiB
C#
Raw Normal View History

using ElectronicsShopContracts.BindingModels;
2024-05-27 17:35:14 +04:00
using ElectronicsShopContracts.BusinessLogicContracts;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.ViewModels;
2024-05-31 16:05:43 +04:00
using ElectronicsShopDataBaseImplement;
2024-05-27 18:33:24 +04:00
using ElectronicsShopDataBaseImplement.Models;
using ElectronicsShopDataModels.Models;
using Microsoft.AspNetCore.Mvc;
2024-05-31 16:05:43 +04:00
using Microsoft.EntityFrameworkCore;
2024-05-30 23:49:53 +04:00
using Newtonsoft.Json;
2024-04-29 21:57:01 +04:00
2024-05-27 17:35:14 +04:00
namespace ElectronicsShopRestAPI.Controllers {
[Route("api/[controller]/[action]")]
[ApiController]
2024-04-29 21:57:01 +04:00
public class MainController : Controller {
private readonly ILogger _logger;
2024-05-27 17:35:14 +04:00
private readonly IProductLogic _product;
2024-05-27 18:33:24 +04:00
private readonly IOrderLogic _order;
2024-06-01 06:31:02 +04:00
private readonly IPaymeantLogic _paymeant;
2024-07-09 19:40:24 +04:00
private readonly IReportClientLogic _reportClientLogic;
2024-05-31 16:05:43 +04:00
private Dictionary<int, (IProductModel, int)> _productlist;
2024-05-27 22:05:47 +04:00
public MainController(ILogger<MainController> logger, IProductLogic product,
2024-07-09 19:40:24 +04:00
IOrderLogic orderLogic, IPaymeantLogic paymeant, IReportClientLogic reportClientLogic) {
2024-06-01 01:23:23 +04:00
_logger = logger;
2024-05-27 17:35:14 +04:00
_product = product;
2024-05-27 18:33:24 +04:00
_order = orderLogic;
2024-05-31 16:05:43 +04:00
_productlist = new Dictionary<int, (IProductModel, int)>();
2024-06-01 06:31:02 +04:00
_paymeant = paymeant;
2024-07-09 19:40:24 +04:00
_reportClientLogic = reportClientLogic;
}
2024-05-28 12:50:24 +04:00
[HttpGet]
2024-06-09 20:35:36 +04:00
public List<ProductViewModel>? GetProducts(int? _costitemid) {
2024-05-28 12:50:24 +04:00
try {
2024-06-09 20:35:36 +04:00
if (_costitemid != null) {
return _product.ReadList(new ProductSearchModel { CostItemID = _costitemid });
}
2024-05-28 12:50:24 +04:00
return _product.ReadList(null);
}
catch (Exception ex) {
_logger.LogError(ex, $"Ошибка получения данных");
throw;
}
}
2024-05-29 15:31:22 +04:00
[HttpGet]
2024-06-09 20:35:36 +04:00
public ProductViewModel? GetProduct(int? _productID, int? _costitemID)
2024-05-29 15:31:22 +04:00
{
try
{
2024-06-09 20:35:36 +04:00
if (_costitemID != null) {
return _product.ReadElement(new ProductSearchModel { CostItemID = _costitemID });
}
2024-05-29 15:31:22 +04:00
return _product.ReadElement(new ProductSearchModel
{
2024-05-29 21:21:17 +04:00
ID = _productID
2024-05-29 15:31:22 +04:00
});
}
catch (Exception ex)
{
2024-05-29 21:21:17 +04:00
_logger.LogError(ex, "Ошибка получения товаров id={Id}", _productID);
2024-05-29 15:31:22 +04:00
throw;
}
}
[HttpGet]
2024-05-27 17:35:14 +04:00
public List<OrderViewModel>? GetOrders(int _clientID) {
2024-05-27 18:33:24 +04:00
try
{
return _order.ReadList(new OrderSearchModel
{
ClientID = _clientID
2024-05-27 18:33:24 +04:00
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка заказов клиента id = {Id} ", _clientID);
throw;
}
}
[HttpGet]
2024-07-24 17:40:56 +04:00
public OrderViewModel? GetOrder(int? _clientID, int? _orderID) {
try {
2024-07-24 17:40:56 +04:00
if (_orderID.HasValue) {
return _order.ReadElement(new OrderSearchModel { ID = _orderID });
}
return _order.ReadElement(new OrderSearchModel { ClientID = _clientID });
}
catch (Exception ex) {
_logger.LogError(ex, $"Ошибка получения данных clientid = {_clientID}");
throw;
}
}
2024-06-01 00:42:46 +04:00
[HttpPost]
public void DeleteOrders(OrderBindingModel model) {
try {
_order.Delete(model);
}
catch (Exception ex) {
_logger.LogError(ex, "Ошибка удаления заказа");
throw;
}
}
2024-05-30 23:49:53 +04:00
[HttpPost]
public void CreateOrder(OrderBindingModel model) {
try {
_order.CreateOrder(model);
}
catch (Exception ex) {
_logger.LogError(ex, "Ошибка создания заказа");
throw;
}
}
2024-05-27 17:35:14 +04:00
2024-05-31 16:05:43 +04:00
[HttpGet]
public List<List<string>> GetOrderProducts(int _orderid) {
var list = new List<List<string>>();
try {
var products = _order.ReadElement(new OrderSearchModel { ID = _orderid});
2024-07-24 09:53:21 +04:00
if (products == null) {
return list;
}
2024-05-31 16:05:43 +04:00
foreach (var pr in products.ProductList) {
var sentence = new List<string> {
JsonConvert.SerializeObject(pr.Value.Item1),
JsonConvert.SerializeObject(pr.Value.Item2.ToString())
};
list.Add(sentence);
}
return list;
}
catch (Exception ex) {
_logger.LogError(ex, "Ошибка получения списка товаров");
throw;
}
}
2024-07-24 09:53:21 +04:00
[HttpPost]
public void AddProduct(List<string> jslist) {
var product = JsonConvert.DeserializeObject<ProductViewModel>(jslist[0]) ?? throw new Exception("Ошибка десериализации");
2024-05-30 23:49:53 +04:00
int count = JsonConvert.DeserializeObject<int>(jslist[1]);
int orderid = JsonConvert.DeserializeObject<int>(jslist[2]);
2024-05-31 16:05:43 +04:00
var view = _order.ReadElement(new OrderSearchModel { ID = orderid });
2024-07-24 09:53:21 +04:00
2024-05-31 16:05:43 +04:00
if (view != null) {
_productlist = view.ProductList;
}
2024-07-24 09:53:21 +04:00
else {
throw new Exception("Такого заказа нет");
}
2024-05-31 16:05:43 +04:00
_logger.LogInformation($"Добавление нового товара: {product.ProductName} - {count}");
if (_productlist.ContainsKey(product.ID)) {
_productlist[product.ID] = (product, count);
}
else {
_productlist.Add(product.ID, (product, count));
}
if (_productlist == null || _productlist.Count == 0) {
_logger.LogInformation("Корзина пуста, ошибка");
}
_logger.LogInformation("Сохранение Заказа");
2024-05-30 23:49:53 +04:00
try {
2024-05-31 16:05:43 +04:00
var model = new OrderBindingModel {
ID = orderid,
ClientID = view.ClientID,
DateCreate = view.DateCreate,
2024-07-24 09:53:21 +04:00
ProductList = _productlist ?? new Dictionary<int, (IProductModel, int)>(),
2024-07-26 12:41:39 +04:00
Sum = Calc(_productlist ?? new Dictionary<int, (IProductModel, int)>()),
Status = view.Status,
2024-05-31 16:05:43 +04:00
};
var operationResult = _order.Update(model);
if (!operationResult) {
throw new Exception("Ошибка при сохранении, дополнительная информация в логах");
}
2024-05-30 23:49:53 +04:00
}
catch (Exception ex) {
2024-05-31 16:05:43 +04:00
_logger.LogError(ex, "Ошибка добавления товара");
2024-05-30 23:49:53 +04:00
throw;
}
2024-05-29 18:32:24 +04:00
}
2024-06-01 00:42:46 +04:00
[HttpPost]
public void DeleteProductOrder(List<string> jslist) {
int _orderId = JsonConvert.DeserializeObject<int>(jslist[0]);
int _productId = JsonConvert.DeserializeObject<int>(jslist[1]);
var view = _order.ReadElement(new OrderSearchModel { ID = _orderId });
if (view == null) {
_logger.LogError("Ошибка получения данных");
return;
}
_productlist = view.ProductList;
foreach (var item in _productlist) {
if (item.Key == _productId) {
_productlist.Remove(_productId);
}
}
try {
var model = new OrderBindingModel {
ID = _orderId,
ClientID = view.ClientID,
DateCreate = view.DateCreate,
2024-06-01 03:16:34 +04:00
ProductList = _productlist,
Sum = Calc(_productlist)
2024-06-01 00:42:46 +04:00
};
var operationResult = _order.DeleteProduct(model);
if (!operationResult) {
throw new Exception("Ошибка при сохранении, дополнительная информация в логах");
}
}
catch (Exception ex) {
_logger.LogError(ex, "Ошибка");
throw;
}
}
2024-06-01 03:16:34 +04:00
[HttpPost]
public double Calc(Dictionary<int, (IProductModel, int)> _productlist) {
double ans = 0;
foreach (var item in _productlist) {
ans += item.Value.Item1.Price * item.Value.Item2;
}
return ans;
}
2024-06-01 06:31:02 +04:00
[HttpPost]
public List<PaymeantViewModel>? GetPaymeants(int _clientID) {
try {
return _paymeant.ReadList(new PaymeantSearchModel { ClientID = _clientID });
}
catch (Exception ex) {
_logger.LogError(ex, $"Ошибка получения списка оплат клиента id = {_clientID}");
throw;
}
}
2024-05-29 18:32:24 +04:00
}
2024-07-24 17:40:56 +04:00
}