132 lines
3.7 KiB
C#

using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System;
using LawFirmContracts.BusinessLogicContracts;
using LawFirmContracts.ViewModels;
using LawFirmContracts.SearchModels;
using LawFirmContracts.BindingModels;
using LawFirmDataModels.Models;
namespace LawFirmRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ShopController : Controller
{
private readonly ILogger _logger;
private readonly IShopLogic _shop;
public ShopController(ILogger<ShopController> logger, IShopLogic shopLogic)
{
_logger = logger;
_shop = shopLogic;
}
[HttpGet]
public List<ShopViewModel>? GetShopList()
{
try
{
return _shop.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка магазинов");
throw;
}
}
[HttpGet]
public ShopViewModel? GetShop(int shopId)
{
try
{
var shop = _shop.ReadElement(new ShopSearchModel
{
Id = shopId
});
if (shop != null)
{
List<Tuple<string, int>> docCount = new List<Tuple<string, int>>();
foreach (var doc in shop.ShopDocuments)
{
docCount.Add(new Tuple<string, int>(doc.Value.Item1.DocumentName, doc.Value.Item2));
}
shop = new()
{
Id = shop.Id,
Name = shop.Name,
Adress = shop.Adress,
OpeningDate = shop.OpeningDate,
MaxCountDocuments = shop.MaxCountDocuments,
DocumentCount = docCount
};
}
return shop;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения магазина по id={Id}",shopId);
throw;
}
}
[HttpPost]
public void CreateShop(ShopBindingModel model)
{
try
{
_shop.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания магазина");
throw;
}
}
[HttpPost]
public void UpdateShop(ShopBindingModel model)
{
try
{
_shop.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления магазина");
throw;
}
}
[HttpPost]
public void DeleteShop(ShopBindingModel model)
{
try
{
_shop.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления магазина");
throw;
}
}
[HttpPost]
public void SupplyDocumentsToShop(Tuple<ShopSearchModel,DocumentBindingModel,int> supplyRequest)
{
try
{
_shop.SupplyDocuments(supplyRequest.Item1, supplyRequest.Item2, supplyRequest.Item3);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка пополнения магазина");
throw;
}
}
}
}