2023-05-15 22:33:12 +04:00
|
|
|
|
using HardwareShopBusinessLogic.BusinessLogics.Storekeeper;
|
|
|
|
|
using HardwareShopContracts.BindingModels;
|
2023-05-15 19:26:52 +04:00
|
|
|
|
using HardwareShopContracts.BusinessLogicsContracts;
|
|
|
|
|
using HardwareShopContracts.SearchModels;
|
|
|
|
|
using HardwareShopContracts.ViewModels;
|
2023-05-17 17:14:45 +04:00
|
|
|
|
using HardwareShopDatabaseImplement.Models.ManyToMany;
|
2023-05-16 23:01:02 +04:00
|
|
|
|
using HardwareShopDatabaseImplement.Models.Storekeeper;
|
2023-05-15 22:33:12 +04:00
|
|
|
|
using HardwareShopDatabaseImplement.Models.Worker;
|
2023-05-16 23:01:02 +04:00
|
|
|
|
using HardwareShopDataModels.Enums;
|
2023-05-15 22:33:12 +04:00
|
|
|
|
using HardwareShopDataModels.Models;
|
2023-05-15 19:26:52 +04:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-05-15 22:33:12 +04:00
|
|
|
|
using System.ComponentModel;
|
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 BuildController : Controller
|
2023-05-15 19:26:52 +04:00
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
2023-05-15 22:33:12 +04:00
|
|
|
|
private readonly IBuildLogic _buildLogic;
|
2023-05-17 11:00:56 +04:00
|
|
|
|
private readonly IPurchaseLogic _purchaseLogic;
|
2023-05-15 19:26:52 +04:00
|
|
|
|
|
2023-05-17 11:00:56 +04:00
|
|
|
|
public BuildController(IBuildLogic buildLogic, IPurchaseLogic purchaseLogic, ILogger<UserController> logger)
|
2023-05-15 19:26:52 +04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
2023-05-15 22:33:12 +04:00
|
|
|
|
_buildLogic = buildLogic;
|
2023-05-17 11:00:56 +04:00
|
|
|
|
_purchaseLogic = purchaseLogic;
|
|
|
|
|
|
|
|
|
|
}
|
2023-05-15 19:26:52 +04:00
|
|
|
|
|
2023-05-16 14:38:49 +04:00
|
|
|
|
[HttpGet]
|
2023-05-17 12:28:05 +04:00
|
|
|
|
public List<BuildViewModel>? GetBuilds(int userId = 0)
|
2023-05-15 19:26:52 +04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-05-17 12:28:05 +04:00
|
|
|
|
if (userId == 0)
|
|
|
|
|
return _buildLogic.ReadList(null);
|
2023-05-15 22:33:12 +04:00
|
|
|
|
return _buildLogic.ReadList(new BuildSearchModel
|
2023-05-15 19:26:52 +04:00
|
|
|
|
{
|
2023-05-15 22:33:12 +04:00
|
|
|
|
UserId = userId
|
2023-05-15 19:26:52 +04:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-05-16 14:38:49 +04:00
|
|
|
|
_logger.LogError(ex, "Ошибка получения списка сборок пользоватля");
|
2023-05-15 22:33:12 +04:00
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
2023-05-16 10:40:26 +04:00
|
|
|
|
public BuildViewModel? GetBuild(int buildId)
|
2023-05-15 22:33:12 +04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-05-16 10:40:26 +04:00
|
|
|
|
return _buildLogic.ReadElement(new() { Id = buildId });
|
2023-05-15 22:33:12 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка сборки");
|
2023-05-15 19:26:52 +04:00
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-16 23:01:02 +04:00
|
|
|
|
[HttpGet]
|
|
|
|
|
public List<Tuple<PurchaseViewModel, int>>? GetBuildPurchase(int buildId)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var result = _buildLogic.ReadElement(new() { Id = buildId });
|
|
|
|
|
List<Tuple<PurchaseViewModel, int>> listPurchase = new List<Tuple<PurchaseViewModel, int>>();
|
|
|
|
|
foreach (var item in result.BuildPurchases)
|
|
|
|
|
{
|
|
|
|
|
listPurchase.Add(Tuple.Create(new PurchaseViewModel
|
|
|
|
|
{
|
|
|
|
|
Id = item.Value.Item1.Id,
|
|
|
|
|
Sum = item.Value.Item1.Sum,
|
|
|
|
|
PurchaseStatus = item.Value.Item1.PurchaseStatus,
|
|
|
|
|
}, item.Value.Item2));
|
|
|
|
|
}
|
|
|
|
|
return listPurchase;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка сборки");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
2023-05-15 22:33:12 +04:00
|
|
|
|
public void Create(BuildBindingModel model)
|
2023-05-15 19:26:52 +04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-05-15 22:33:12 +04:00
|
|
|
|
_buildLogic.Create(model);
|
2023-05-15 19:26:52 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-05-15 22:33:12 +04:00
|
|
|
|
_logger.LogError(ex, "Ошибка сборки");
|
2023-05-15 19:26:52 +04:00
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-16 23:01:02 +04:00
|
|
|
|
|
|
|
|
|
[HttpPost]
|
2023-05-15 22:33:12 +04:00
|
|
|
|
public void Update(BuildBindingModel model)
|
2023-05-15 19:26:52 +04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-05-19 16:49:20 +04:00
|
|
|
|
var build = _buildLogic.ReadElement(new() { Id = model.Id });
|
|
|
|
|
model.Price = build.Price;
|
2023-05-15 22:33:12 +04:00
|
|
|
|
_buildLogic.Update(model);
|
2023-05-15 19:26:52 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка обновления данных");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-15 22:33:12 +04:00
|
|
|
|
|
2023-05-17 17:14:45 +04:00
|
|
|
|
private double Calc(int purchaseId)
|
|
|
|
|
{
|
|
|
|
|
var purchase = _purchaseLogic.ReadElement(new() { Id = purchaseId });
|
|
|
|
|
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-17 11:54:30 +04:00
|
|
|
|
|
|
|
|
|
[HttpGet]
|
2023-05-19 16:49:20 +04:00
|
|
|
|
public bool DeleteLinkPurchase(int deleteBuildId, int deletePurchaseId, int userId)
|
2023-05-17 11:54:30 +04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-05-19 16:49:20 +04:00
|
|
|
|
var build = _buildLogic.ReadElement(new() { Id = deleteBuildId });
|
2023-05-17 11:54:30 +04:00
|
|
|
|
var purchase = _purchaseLogic.ReadElement(new() { Id = deletePurchaseId });
|
2023-05-19 16:49:20 +04:00
|
|
|
|
if (purchase == null || build == null || build.UserId != userId || purchase.UserId != userId
|
|
|
|
|
|| purchase.PurchaseStatus == PurchaseStatus.Выдан) return false;
|
|
|
|
|
build.BuildPurchases.Remove(deletePurchaseId);
|
2023-05-17 11:54:30 +04:00
|
|
|
|
|
2023-05-19 16:49:20 +04:00
|
|
|
|
_buildLogic.Update(new BuildBindingModel
|
2023-05-17 11:54:30 +04:00
|
|
|
|
{
|
|
|
|
|
Id = deleteBuildId,
|
|
|
|
|
Price = build.Price,
|
|
|
|
|
BuildName = build.BuildName,
|
|
|
|
|
BuildPurchases = build.BuildPurchases
|
|
|
|
|
});
|
2023-05-17 17:14:45 +04:00
|
|
|
|
purchase.Sum = Calc(purchase.Id);
|
2023-05-17 11:00:56 +04:00
|
|
|
|
_purchaseLogic.Update(new PurchaseBindingModel
|
2023-05-17 11:54:30 +04:00
|
|
|
|
{
|
2023-05-17 11:00:56 +04:00
|
|
|
|
Id = purchase.Id,
|
|
|
|
|
Sum = purchase.Sum,
|
|
|
|
|
PurchaseStatus = purchase.PurchaseStatus,
|
|
|
|
|
DatePurchase = purchase.DatePurchase,
|
|
|
|
|
PurchaseGoods = purchase.PurchaseGoods,
|
|
|
|
|
});
|
2023-05-17 11:54:30 +04:00
|
|
|
|
return true;
|
2023-05-17 11:00:56 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка обновления данных");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-17 11:54:30 +04:00
|
|
|
|
|
2023-05-17 12:11:21 +04:00
|
|
|
|
[HttpGet]
|
2023-05-19 16:49:20 +04:00
|
|
|
|
public bool UpdateLinkPurchase(int buildId, int purchaseId, int count, int userId)
|
2023-05-17 12:11:21 +04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-05-19 16:49:20 +04:00
|
|
|
|
var build = _buildLogic.ReadElement(new() { Id = buildId });
|
|
|
|
|
var purchase = _purchaseLogic.ReadElement(new() { Id = purchaseId });
|
|
|
|
|
if (purchase == null || build == null || build.UserId != userId || purchase.UserId != userId
|
|
|
|
|
|| purchase.PurchaseStatus == PurchaseStatus.Выдан) return false;
|
|
|
|
|
if (build.BuildPurchases.ContainsKey(purchaseId))
|
2023-05-17 12:11:21 +04:00
|
|
|
|
{
|
2023-05-19 16:49:20 +04:00
|
|
|
|
build.BuildPurchases[purchaseId] = (build.BuildPurchases[purchaseId].Item1, count);
|
2023-05-17 12:11:21 +04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-05-19 16:49:20 +04:00
|
|
|
|
build.BuildPurchases.Add(purchaseId, (purchase, count));
|
2023-05-17 12:11:21 +04:00
|
|
|
|
}
|
2023-05-19 16:49:20 +04:00
|
|
|
|
_buildLogic.Update(new BuildBindingModel
|
2023-05-17 12:11:21 +04:00
|
|
|
|
{
|
2023-05-19 16:49:20 +04:00
|
|
|
|
Id = buildId,
|
2023-05-17 12:11:21 +04:00
|
|
|
|
Price = build.Price,
|
|
|
|
|
BuildName = build.BuildName,
|
|
|
|
|
BuildPurchases = build.BuildPurchases
|
|
|
|
|
});
|
2023-05-17 17:14:45 +04:00
|
|
|
|
purchase.Sum = Calc(purchase.Id);
|
|
|
|
|
_purchaseLogic.Update(new PurchaseBindingModel
|
|
|
|
|
{
|
|
|
|
|
Id = purchase.Id,
|
|
|
|
|
Sum = purchase.Sum,
|
|
|
|
|
PurchaseStatus = purchase.PurchaseStatus,
|
|
|
|
|
DatePurchase = purchase.DatePurchase,
|
|
|
|
|
PurchaseGoods = purchase.PurchaseGoods,
|
2023-05-19 22:07:03 +04:00
|
|
|
|
UserId = purchase.UserId
|
2023-05-17 17:14:45 +04:00
|
|
|
|
});
|
2023-05-17 12:11:21 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка обновления данных");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-17 11:00:56 +04:00
|
|
|
|
[HttpPost]
|
2023-05-16 12:48:47 +04:00
|
|
|
|
public void DeleteBuild(BuildBindingModel model)
|
2023-05-15 22:33:12 +04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-05-16 12:48:47 +04:00
|
|
|
|
_buildLogic.Delete(model);
|
2023-05-15 22:33:12 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка удаления сборки");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-15 19:26:52 +04:00
|
|
|
|
}
|
|
|
|
|
}
|