This commit is contained in:
71
BimAI.API/Controllers/ProductsController.cs
Normal file
71
BimAI.API/Controllers/ProductsController.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using BimAI.Application.DTOModels;
|
||||
using BimAI.Application.DTOModels.Common;
|
||||
using BimAI.Infrastructure.Data;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace BimAI.API.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class ProductsController(BimAIDbContext context) : ControllerBase
|
||||
{
|
||||
private readonly BimAIDbContext _context = context;
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<IEnumerable<ProductDto>>> GetProducts([FromQuery] ProductFilterRequest request)
|
||||
{
|
||||
var query = _context.Products.AsQueryable();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.Search))
|
||||
{
|
||||
var searchTerm = request.Search.ToLower();
|
||||
query = query.Where(x =>
|
||||
x.Name.ToLower().Contains(searchTerm) ||
|
||||
(x.Code != null && x.Code.ToLower().Contains(searchTerm)) ||
|
||||
(x.Ean != null && x.Ean.ToLower().Contains(searchTerm))
|
||||
);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.Name))
|
||||
{
|
||||
query = query.Where(x => x.Name.ToLower().Contains(request.Name.ToLower()));
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.Code))
|
||||
{
|
||||
query = query.Where(x => x.Code != null && x.Code.ToLower().Contains(request.Code.ToLower()));
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.Ean))
|
||||
{
|
||||
query = query.Where(x => x.Ean != null && x.Ean.ToLower().Contains(request.Ean.ToLower()));
|
||||
}
|
||||
|
||||
var totalCount = await query.CountAsync();
|
||||
|
||||
var items = await query
|
||||
.OrderBy(x => x.Name)
|
||||
.Skip((request.Page -1) * request.PageSize)
|
||||
.Take(request.PageSize)
|
||||
.Select(x => new ProductDto
|
||||
{
|
||||
Id = x.Id,
|
||||
Name = x.Name,
|
||||
Code = x.Code ?? string.Empty,
|
||||
Ean = x.Ean ?? string.Empty,
|
||||
StockAddresses = x.StockAddresses ?? string.Empty,
|
||||
CreatedAt = x.CreatedAt,
|
||||
UpdatedAt = x.UpdatedAt
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
return Ok(new PagedResult<ProductDto>
|
||||
{
|
||||
Items = items,
|
||||
TotalCount = totalCount,
|
||||
Page = request.Page,
|
||||
PageSize = request.PageSize,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user