Computer_Hardware_Store/HardwareShop/HardwareShopRestApi/Controllers/BuildController.cs

245 lines
7.0 KiB
C#
Raw Normal View History

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-16 23:01:02 +04:00
using HardwareShopDatabaseImplement.Models.Storekeeper;
using HardwareShopDatabaseImplement.Models.Worker;
2023-05-16 23:01:02 +04:00
using HardwareShopDataModels.Enums;
using HardwareShopDataModels.Models;
2023-05-15 19:26:52 +04:00
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel;
2023-05-15 19:26:52 +04:00
namespace HardwareShopRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class BuildController : Controller
2023-05-15 19:26:52 +04:00
{
private readonly ILogger _logger;
private readonly IBuildLogic _buildLogic;
private readonly IPurchaseLogic _purchaseLogic;
2023-05-15 19:26:52 +04:00
public BuildController(IBuildLogic buildLogic, IPurchaseLogic purchaseLogic, ILogger<UserController> logger)
2023-05-15 19:26:52 +04:00
{
_logger = logger;
_buildLogic = buildLogic;
_purchaseLogic = purchaseLogic;
}
2023-05-15 19:26:52 +04:00
[HttpGet]
public List<BuildViewModel>? GetBuilds(int userId)
2023-05-15 19:26:52 +04:00
{
try
{
return _buildLogic.ReadList(new BuildSearchModel
2023-05-15 19:26:52 +04:00
{
UserId = userId
2023-05-15 19:26:52 +04:00
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка сборок пользоватля");
throw;
}
}
[HttpGet]
public BuildViewModel? GetBuild(int buildId)
{
try
{
return _buildLogic.ReadElement(new() { Id = buildId });
}
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]
public void Create(BuildBindingModel model)
2023-05-15 19:26:52 +04:00
{
try
{
_buildLogic.Create(model);
2023-05-15 19:26:52 +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
[HttpPost]
public void Update(BuildBindingModel model)
2023-05-15 19:26:52 +04:00
{
try
{
_buildLogic.Update(model);
2023-05-15 19:26:52 +04:00
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления данных");
throw;
}
}
[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 });
if (purchase.PurchaseStatus == PurchaseStatus.Выдан) return false;
purchase.Sum = purchase.Sum + build.Price * count;
if (build.BuildPurchases.ContainsKey(purchaseId))
{
build.BuildPurchases[purchaseId] = (build.BuildPurchases[purchaseId].Item1, build.BuildPurchases[purchaseId].Item2 + count);
}
else
{
build.BuildPurchases.Add(purchaseId, (purchase as IPurchaseModel, count));
}
Update(new BuildBindingModel
{
Id = buildId,
Price = build.Price,
BuildName = build.BuildName,
BuildPurchases = build.BuildPurchases
});
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления данных");
throw;
}
}
[HttpGet]
public bool DeleteLinkPurchase(int deleteBuildId, int deletePurchaseId)
{
try
{
var build = GetBuild(deleteBuildId);//APIClient.GetRequest<BuildViewModel>($"api/build/getBuild?buildId={buildId}");
var purchase = _purchaseLogic.ReadElement(new() { Id = deletePurchaseId });
if (purchase.PurchaseStatus == PurchaseStatus.Выдан) return false;
purchase.Sum = purchase.Sum - build.Price * build.BuildPurchases[deletePurchaseId].Item2;
build.BuildPurchases.Remove(deletePurchaseId);//Add(purchaseId, (purchase as IPurchaseModel, count));
Update(new BuildBindingModel
{
Id = deleteBuildId,
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;
}
}
[HttpGet]
public bool UpdateLinkPurchase(int updateBuildId, int updatePurchaseId, int count)
{
try
{
var build = GetBuild(updateBuildId);//APIClient.GetRequest<BuildViewModel>($"api/build/getBuild?buildId={buildId}");
var purchase = _purchaseLogic.ReadElement(new() { Id = updatePurchaseId });
if (purchase.PurchaseStatus == PurchaseStatus.Выдан) return false;
purchase.Sum = purchase.Sum + build.Price * count;
if (build.BuildPurchases.ContainsKey(updatePurchaseId))
{
if (count > build.BuildPurchases[updatePurchaseId].Item2)
{
purchase.Sum = purchase.Sum + (count - build.BuildPurchases[updatePurchaseId].Item2) * build.Price;
}
else
{
purchase.Sum = purchase.Sum - (build.BuildPurchases[updatePurchaseId].Item2 - count) * build.Price;
}
build.BuildPurchases[updatePurchaseId] = (build.BuildPurchases[updatePurchaseId].Item1, count);
}
else
{
return false;
}
Update(new BuildBindingModel
{
Id = updateBuildId,
Price = build.Price,
BuildName = build.BuildName,
BuildPurchases = build.BuildPurchases
});
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления данных");
throw;
}
}
[HttpPost]
2023-05-16 12:48:47 +04:00
public void DeleteBuild(BuildBindingModel model)
{
try
{
2023-05-16 12:48:47 +04:00
_buildLogic.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления сборки");
throw;
}
}
2023-05-15 19:26:52 +04:00
}
}