using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Threading.Tasks; using Dapper; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.JsonPatch; using Microsoft.AspNetCore.Mvc; using MisIngredientes.Core.Models; using MisIngredientesVue.Core.Models; using MisIngredientesVue.Core.Services; using MisIngredientesVue.Core.Extensions; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using MisIngredientesVue.Hubs; using Microsoft.AspNetCore.SignalR; namespace MisIngredientesVue.Controllers { [Route("api/[controller]")] [ApiController] public class SuppliersController : Controller { private readonly ISuppliersService _suppliersService; public SuppliersController(ISuppliersService suppliersService) { _suppliersService = suppliersService; } [HttpGet] public async Task Get(int skip, int take, string sortBy, string sortOrder, string q) { var findAll = await _suppliersService.GetSuppliers(User.Identity.GetTenantId(), skip, take, sortBy, sortOrder, q); return Ok(new PagedResults(findAll.Results, findAll.Total, skip, take)); } [Route("{id}")] [HttpGet] public async Task Get(int id) { return Ok(await _suppliersService.GetSupplierById(User.Identity.GetTenantId(), id)); } [Route("all")] [HttpGet] public async Task GetAllSuppliers() { return Ok(await _suppliersService.GetAllSuppliers(User.Identity.GetTenantId())); } [Route("search")] [HttpGet] public async Task SearchVerifiedSuppliers(string q) { return Ok(await _suppliersService.SearchVerifiedSuppliers(User.Identity.GetTenantId(), q)); } [Route("searchlocal")] [HttpGet] public async Task SearchLocalSuppliers(string q) { return Ok(await _suppliersService.SearchLocalSuppliers(User.Identity.GetTenantId(), q)); } [Route("searchall")] [HttpGet] public async Task SearchAllSuppliers(string q) { return Ok(await _suppliersService.SearchAllSuppliers(User.Identity.GetTenantId(), q)); } [Route("link/{supplierId}")] [HttpPost] public async Task AddLinkToSupplier(int supplierId) { await _suppliersService.AddLinkToSupplier(User.Identity.GetTenantId(), supplierId); return Ok(); } [Route("{id}")] [HttpPut] public async Task Put(int id, Supplier supplier) { if (id != supplier.Id) return BadRequest(); await _suppliersService.UpdateSupplier(User.Identity.GetTenantId(), User.Identity.GetUserId(), supplier); return Ok(); } [HttpPost] public async Task Post(Supplier supplier) { return Ok(await _suppliersService.InsertSupplier(User.Identity.GetTenantId(), User.Identity.GetUserId(), supplier)); } } }