using Microsoft.AspNetCore.Mvc; using SushiBarContracts.BindingModels; using SushiBarContracts.BusinessLogicsContracts; using SushiBarContracts.SearchModels; using SushiBarContracts.ViewModels; namespace SushiBarRestApi.Controllers; [Route("api/[controller]/[action]")] [ApiController] public class StoreController : Controller { private readonly ILogger _logger; private readonly IStoreLogic _store; public StoreController(ILogger logger, IStoreLogic store) { _logger = logger; _store = store; } [HttpGet] public List? GetStores() { try { return _store.ReadList(null); } catch (Exception ex) { _logger.LogError(ex, "Error getting store list"); throw; } } [HttpGet] public Tuple>>? GetStore(int storeId) { try { var elem = _store.ReadElement(new StoreSearchModel { Id = storeId }); return elem == null ? null : Tuple.Create(elem, elem.Sushis .Select(x => Tuple.Create(x.Value.Item1.SushiName, x.Value.Item2)) .ToList()); } catch (Exception ex) { _logger.LogError(ex, "Error getting store with id={Id}", storeId); throw; } } [HttpPost] public void CreateStore(StoreBindingModel model) { try { _store.Create(model); } catch (Exception ex) { _logger.LogError(ex, "Error create store"); throw; } } [HttpPost] public void UpdateData(StoreBindingModel model) { try { model.Sushis = null!; _store.Update(model); } catch (Exception ex) { _logger.LogError(ex, "Data update error"); throw; } } [HttpPost] public void DeleteStore(StoreBindingModel model) { try { _store.Delete(model); } catch (Exception ex) { _logger.LogError(ex, "Store deletion error"); throw; } } [HttpPost] public void Supply(Tuple model) { try { _store.SupplySushi(model.Item1, model.Item2, model.Item3); } catch (Exception ex) { _logger.LogError(ex, "Error adding product to store"); throw; } } }