2023-05-15 19:26:52 +04:00
|
|
|
|
using HardwareShopContracts.BindingModels;
|
|
|
|
|
using HardwareShopContracts.BusinessLogicsContracts;
|
|
|
|
|
using HardwareShopContracts.SearchModels;
|
|
|
|
|
using HardwareShopContracts.ViewModels;
|
2023-05-16 17:54:45 +04:00
|
|
|
|
using HardwareShopDatabaseImplement.Models.Storekeeper;
|
2023-05-16 19:49:58 +04:00
|
|
|
|
using HardwareShopDataModels.Enums;
|
2023-05-16 17:54:45 +04:00
|
|
|
|
using HardwareShopDataModels.Models;
|
2023-05-15 22:33:12 +04:00
|
|
|
|
using HardwareShopRestApi.Controllers;
|
2023-05-15 19:26:52 +04:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-05-15 22:33:12 +04:00
|
|
|
|
using System.Xml.Linq;
|
2023-05-15 19:26:52 +04:00
|
|
|
|
|
|
|
|
|
namespace HardwareShopRestApi.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Route("api/[controller]/[action]")]
|
|
|
|
|
[ApiController]
|
2023-05-15 22:33:12 +04:00
|
|
|
|
public class PurchaseController : Controller
|
2023-05-15 19:26:52 +04:00
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
2023-05-15 22:33:12 +04:00
|
|
|
|
private readonly IPurchaseLogic _purchaseLogic;
|
2023-05-15 19:26:52 +04:00
|
|
|
|
|
2023-05-15 22:33:12 +04:00
|
|
|
|
public PurchaseController(IPurchaseLogic purchaseLogic, ILogger<UserController> logger)
|
2023-05-15 19:26:52 +04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
2023-05-15 22:33:12 +04:00
|
|
|
|
_purchaseLogic = purchaseLogic;
|
2023-05-16 17:54:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-17 19:36:40 +04:00
|
|
|
|
[HttpGet]
|
2023-05-19 16:49:20 +04:00
|
|
|
|
public Tuple<PurchaseViewModel, List<Tuple<GoodViewModel, int>>>? GetPurchaseUpdate(int purchaseId, int userId)
|
2023-05-17 19:36:40 +04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var purchase = _purchaseLogic.ReadElement(new() { Id = purchaseId });
|
2023-05-19 16:49:20 +04:00
|
|
|
|
if (purchase == null || purchase.UserId != userId)
|
2023-05-17 19:36:40 +04:00
|
|
|
|
return null;
|
|
|
|
|
var tuple = Tuple.Create(purchase,
|
|
|
|
|
purchase.PurchaseGoods.Select(x => Tuple.Create(new GoodViewModel
|
|
|
|
|
{
|
|
|
|
|
Id = x.Value.Item1.Id,
|
|
|
|
|
GoodName = x.Value.Item1.GoodName,
|
|
|
|
|
Price = x.Value.Item1.Price
|
|
|
|
|
}, x.Value.Item2)).ToList());
|
|
|
|
|
return tuple;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка получения покупки");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 22:47:00 +04:00
|
|
|
|
[HttpPost]
|
|
|
|
|
public List<PurchaseViewModel>? GetPurchases(PurchaseSearchModel model)
|
2023-05-16 17:54:45 +04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-05-19 22:47:00 +04:00
|
|
|
|
return _purchaseLogic.ReadList(model);
|
2023-05-16 17:54:45 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка получения списка товаров");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 22:07:03 +04:00
|
|
|
|
[HttpGet]
|
|
|
|
|
public List<PurchaseViewModel>? GetPurchasesNotDelivery(int userId)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return _purchaseLogic.ReadList(new() { UserId = userId, PurchaseStatus = PurchaseStatus.Выполняется });
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка получения списка товаров");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
2023-05-17 11:00:56 +04:00
|
|
|
|
public PurchaseViewModel? GetPurchase(int purchaseId)
|
2023-05-16 17:54:45 +04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-05-17 11:00:56 +04:00
|
|
|
|
return _purchaseLogic.ReadElement(new() { Id = purchaseId });
|
2023-05-16 17:54:45 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка получения товара");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public void CreatePurchase(PurchaseBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < model.PurchaseGoodsCounts.Count; i++)
|
|
|
|
|
{
|
2023-05-16 19:49:58 +04:00
|
|
|
|
model.PurchaseGoods.Add(model.ListPurchaseGoods[i].Id,(model.ListPurchaseGoods[i] as IGoodModel, model.PurchaseGoodsCounts[i]));
|
2023-05-16 17:54:45 +04:00
|
|
|
|
}
|
|
|
|
|
_purchaseLogic.Create(model);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка создания товара");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
2023-05-17 19:36:40 +04:00
|
|
|
|
public void UpdateStatusPurchase(PurchaseBindingModel model)
|
2023-05-16 17:54:45 +04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-05-16 19:49:58 +04:00
|
|
|
|
var oldModel = _purchaseLogic.ReadElement(new() {
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
});
|
|
|
|
|
model.Sum = oldModel.Sum;
|
2023-05-16 18:35:08 +04:00
|
|
|
|
_purchaseLogic.DeliveryPurchase(model);
|
2023-05-16 17:54:45 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка обновления данных товара");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-17 11:00:56 +04:00
|
|
|
|
[HttpPost]
|
|
|
|
|
public void Update(PurchaseBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-05-17 19:36:40 +04:00
|
|
|
|
for (int i = 0; i < model.PurchaseGoodsCounts.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
model.PurchaseGoods.Add(model.ListPurchaseGoods[i].Id, (model.ListPurchaseGoods[i] as IGoodModel, model.PurchaseGoodsCounts[i]));
|
|
|
|
|
}
|
|
|
|
|
model.PurchaseBuilds = _purchaseLogic.ReadElement(new() { Id = model.Id }).PurchaseBuilds;
|
|
|
|
|
model.Sum = Calc(model);
|
|
|
|
|
_purchaseLogic.Update(model);
|
2023-05-17 11:00:56 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка обновления данных товара");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-17 19:36:40 +04:00
|
|
|
|
private double Calc(PurchaseBindingModel purchase)
|
|
|
|
|
{
|
|
|
|
|
double price = 0;
|
|
|
|
|
foreach (var elem in purchase.PurchaseBuilds)
|
|
|
|
|
{
|
|
|
|
|
price += ((elem.Value.Item1?.Price ?? 0) * elem.Value.Item2);
|
|
|
|
|
}
|
|
|
|
|
foreach (var elem in purchase.PurchaseGoods)
|
|
|
|
|
{
|
|
|
|
|
price += ((elem.Value.Item1?.Price ?? 0) * elem.Value.Item2);
|
|
|
|
|
}
|
|
|
|
|
return Math.Round(price * 1.1, 2);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-16 17:54:45 +04:00
|
|
|
|
[HttpPost]
|
|
|
|
|
public void DeletePurchase(PurchaseBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_purchaseLogic.Delete(model);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка удаления товара");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2023-05-15 19:26:52 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-15 19:26:52 +04:00
|
|
|
|
}
|
2023-05-15 22:33:12 +04:00
|
|
|
|
|
|
|
|
|
|