Backend for the Teso project written in 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
3.0 KiB

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<ActionResult<IEnumerable<Product>>> GetProducts([FromBody] string businessID)
{
return await _context.Products.AsQueryable().Where(b => b.BusinessId == businessID).ToListAsync();
}
[Authorize]
[Route("desire-product"), HttpPost]
public async Task<ActionResult<IEnumerable<DesiredItem>>> 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<ActionResult<IEnumerable<Product>>> GetTrending()
{
return await _context.Products.FromSqlRaw("exec [dbo].[usp_trending_products_get]").ToListAsync();
}
// design the correct algorithm
[Authorize]
[Route("newarrivals"), HttpGet]
public async Task<ActionResult<IEnumerable<Product>>> GetLatest()
{
return await _context.Products.FromSqlRaw("exec [dbo].[usp_latest_products_get]").ToListAsync();
}
[Authorize]
[Route("product_image_list"), HttpPost]
public async Task<ActionResult<IEnumerable<ProductImages>>> GetProdutImages([FromBody] string productID)
{
List<SqlParameter> parms = new List<SqlParameter>
{
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<ActionResult<Product>> GetProdutById([FromBody] string productID)
{
List<SqlParameter> parms = new List<SqlParameter>
{
new SqlParameter { ParameterName = "@productID", Value = productID },
};
return await _context.Products.FromSqlRaw("exec [dbo].[usp_product_get_by_productID] @productID", parms.ToArray()).AsAsyncEnumerable().FirstOrDefaultAsync();
}
}
}