пофиксил обновление цены при привязке сборки к покупке

This commit is contained in:
Николай 2023-05-17 11:00:56 +04:00
parent b84b593b88
commit d3b455117e
3 changed files with 71 additions and 16 deletions

View File

@ -18,12 +18,15 @@ namespace HardwareShopRestApi.Controllers
{
private readonly ILogger _logger;
private readonly IBuildLogic _buildLogic;
private readonly IPurchaseLogic _purchaseLogic;
public BuildController(IBuildLogic buildLogic, ILogger<UserController> logger)
public BuildController(IBuildLogic buildLogic, IPurchaseLogic purchaseLogic, ILogger<UserController> logger)
{
_logger = logger;
_buildLogic = buildLogic;
}
_purchaseLogic = purchaseLogic;
}
[HttpGet]
public List<BuildViewModel>? GetBuilds(int userId)
@ -112,7 +115,41 @@ namespace HardwareShopRestApi.Controllers
}
}
[HttpPost]
[HttpGet]
public bool LinkPurchase(int buildId, int purchaseId, int count)
{
try
{
var build = GetBuild(buildId);//APIClient.GetRequest<BuildViewModel>($"api/build/getBuild?buildId={buildId}");
var purchase = _purchaseLogic.ReadElement(new() { Id = purchaseId });
purchase.Sum = purchase.Sum + build.Price * count;
build.BuildPurchases.Add(purchaseId, (purchase as IPurchaseModel, count));
Update(new BuildBindingModel
{
Id = buildId,
Price = build.Price,
BuildName = build.BuildName,
BuildPurchases = build.BuildPurchases
});
_purchaseLogic.Update(new PurchaseBindingModel
{
Id = purchase.Id,
Sum = purchase.Sum,
PurchaseStatus = purchase.PurchaseStatus,
DatePurchase = purchase.DatePurchase,
PurchaseGoods = purchase.PurchaseGoods,
});
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления данных");
throw;
}
}
[HttpPost]
public void DeleteBuild(BuildBindingModel model)
{
try

View File

@ -40,11 +40,11 @@ namespace HardwareShopRestApi.Controllers
}
[HttpGet]
public PurchaseViewModel? GetPurchase(int id)
public PurchaseViewModel? GetPurchase(int purchaseId)
{
try
{
return _purchaseLogic.ReadElement(new() { Id = id });
return _purchaseLogic.ReadElement(new() { Id = purchaseId });
}
catch (Exception ex)
{
@ -89,6 +89,20 @@ namespace HardwareShopRestApi.Controllers
}
}
[HttpPost]
public void Update(PurchaseBindingModel model)
{
try
{
_purchaseLogic.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления данных товара");
throw;
}
}
[HttpPost]
public void DeletePurchase(PurchaseBindingModel model)
{

View File

@ -167,20 +167,24 @@ namespace HardwareShopWorkerApp.Controllers
{
throw new Exception($"Количество сборок в покупке не может быть ниже или равен 0");
}
var build = APIClient.GetRequest<BuildViewModel>($"api/build/getBuild?buildId={buildId}");
var purchase = APIClient.GetRequest<BuildViewModel>($"api/purchase/getpurchase?purchaseId={purchaseId}");
build.BuildPurchases.Add(purchaseId, (purchase as IPurchaseModel, count));
APIClient.PostRequest("api/build/update", new BuildBindingModel
{
Id = buildId,
Price = build.Price,
BuildName = build.BuildName,
BuildPurchases = build.BuildPurchases
});
//APIClient.PostRequest("api/build/linkPurchase", new { buildId, purchaseId, count });
APIClient.GetRequest<bool>($"api/build/linkPurchase?buildId={buildId}&purchaseId={purchaseId}&count={count}");
//var build = APIClient.GetRequest<BuildViewModel>($"api/build/getBuild?buildId={buildId}");
//var purchase = APIClient.GetRequest<PurchaseViewModel>($"api/purchase/getpurchase?purchaseId={purchaseId}");
//purchase.Sum = purchase.Sum + build.Price * count;
//build.BuildPurchases.Add(purchaseId, (purchase as IPurchaseModel, count));
//APIClient.PostRequest("api/build/update", new BuildBindingModel
//{
// Id = buildId,
// Price = build.Price,
// BuildName = build.BuildName,
// BuildPurchases = build.BuildPurchases
//});
//APIClient.PostRequest("api/purchase/update", purchase);
Response.Redirect("Builds");
}
[HttpPost]
public void DeleteBuild(int deleteBuildId)
{