Kashin M.I. Lab work 5 hard #13

Closed
Sosees04ka wants to merge 18 commits from LabWork05_hard into LabWork04_hard
2 changed files with 96 additions and 1 deletions
Showing only changes of commit b31a7909ba - Show all commits

View File

@ -0,0 +1,92 @@
using Microsoft.AspNetCore.Mvc;
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.BusinessLogicsContracts;
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.ViewModels;
namespace PrecastConcretePlantRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ShopController : Controller
{
private readonly ILogger _logger;
private readonly IShopLogic _logic;
public ShopController(IShopLogic logic, ILogger<ShopController> logger)
{
_logger = logger;
_logic = logic;
}
[HttpGet]
public List<ShopViewModel>? GetShops()
{
try
{
return _logic.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка магазинов");
throw;
}
}
[HttpGet]
public Tuple<ShopViewModel, IEnumerable<ReinforcedViewModel>, IEnumerable<int>>? GetShopWithReinforcedies(int id)
{
try
{
var shop = _logic.ReadElement(new() { Id = id });
if (shop == null)
{
return null;
}
return Tuple.Create(shop,
shop.ShopReinforcedies.Select(x => new ReinforcedViewModel()
{
Id = x.Value.Item1.Id,
Price = x.Value.Item1.Price,
ReinforcedName = x.Value.Item1.ReinforcedName,
}),
shop.ShopReinforcedies.Select(x => x.Value.Item2));
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения магазина");
throw;
}
}
[HttpPost]
public void CRUDShop(Action action)
{
try
{
action.Invoke();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка операции CRUD - {operation} с магазином", action.Method.Name);
throw;
}
}
[HttpPost]
public void UpdateShop(ShopBindingModel model) => CRUDShop(() => _logic.Update(model));
[HttpPost]
public void CreateShop(ShopBindingModel model) => CRUDShop(() => _logic.Create(model));
[HttpPost]
public void DeleteShop(ShopBindingModel model) => CRUDShop(() => _logic.Delete(model));
[HttpPost]
public void AddReinforcedInShop(Tuple<ShopSearchModel, ReinforcedViewModel, int> countReinforcedForShop)
{
CRUDShop(() => _logic.AddReinforced(countReinforcedForShop.Item1, countReinforcedForShop.Item2, countReinforcedForShop.Item3));
}
}
}

View File

@ -12,10 +12,13 @@ builder.Logging.AddLog4Net("log4net.config");
builder.Services.AddTransient<IClientStorage, ClientStorage>();
builder.Services.AddTransient<IOrderStorage, OrderStorage>();
builder.Services.AddTransient<IReinforcedStorage, ReinforcedStorage>();
builder.Services.AddTransient<IOrderLogic, OrderLogic>();
builder.Services.AddTransient<IShopStorage, ShopStorage>();
builder.Services.AddTransient<IOrderLogic, OrderLogic>();
builder.Services.AddTransient<IClientLogic, ClientLogic>();
builder.Services.AddTransient<IReinforcedLogic, ReinforcedLogic>();
builder.Services.AddTransient<IShopLogic, ShopLogic>();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>