using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; using Teso_API.Models; namespace Teso_API.Controllers { [AllowAnonymous, Route("products")] [ApiController] public class ProductsController : ControllerBase { private readonly TESOContext _context; public ProductsController(TESOContext context) { _context = context; } [Route("byshop"),HttpPost] public async Task>> GetProducts([FromBody] string businessID) { return await _context.Products.AsQueryable().Where(b => b.BusinessId == businessID).ToListAsync(); } [Authorize] [Route("desire-product"), HttpPost] public async Task>> GetDesire([FromBody] string desire) { return await _context.Desires.FromSqlRaw("Select productID,pname as productName,productImage,(Cast(unitPrice as float)) as cost,category,'true' as enlisted from Products").AsQueryable() .Where(d => d.productName.Contains(desire)).OrderBy(p => p.productName).Take(50).ToListAsync(); } // design the correct algorithm [Authorize] [Route("trending"), HttpGet] public async Task>> GetTrending() { return await _context.Products.FromSqlRaw("exec [dbo].[usp_trending_products_get]").ToListAsync(); } // design the correct algorithm [Authorize] [Route("newarrivals"), HttpGet] public async Task>> GetLatest() { return await _context.Products.FromSqlRaw("exec [dbo].[usp_latest_products_get]").ToListAsync(); } [Authorize] [Route("product_image_list"), HttpPost] public async Task>> GetProdutImages([FromBody] string productID) { List parms = new List { new SqlParameter { ParameterName = "@productID", Value = productID }, }; return await _context.ProductImaging.FromSqlRaw("exec [dbo].[usp_productImages_list_get] @productID",parms.ToArray()).ToListAsync(); } [Authorize] [Route("product_by_id"), HttpPost] public async Task> GetProdutById([FromBody] string productID) { List parms = new List { new SqlParameter { ParameterName = "@productID", Value = productID }, }; return await _context.Products.FromSqlRaw("exec [dbo].[usp_product_get_by_productID] @productID", parms.ToArray()).AsAsyncEnumerable().FirstOrDefaultAsync(); } } }