using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Dpz.Core.Infrastructure.Entity;
using MongoDB.Bson;
using MongoDB.Driver;
namespace Dpz.Core.Infrastructure.ExpressTreeQuery;
public class BuildFilter<TEntity,TRequest>(TRequest request)
where TEntity : IBaseEntity
{
private readonly FilterDefinition<TEntity> _empty = Builders<TEntity>.Filter.Empty;
// private readonly T _request = request;
public List<FilterDefinition<TEntity>> Build()
{
var properties = GetProperties();
foreach (var item in properties) { }
// todo
return [_empty];
}
private List<PropertyInfo> GetProperties()
{
return request?
.GetType()
.GetProperties()
.Where(x => x.GetCustomAttribute<IgnorePropertyAttribute>() == null)
.ToList() ?? [];
}
private FilterDefinition<TEntity> BuildItemFilter(PropertyInfo property)
{
var queryFilters = property.GetCustomAttributes<QueryFilterAttribute>().ToList();
if (queryFilters.Count > 0)
{
// todo
return _empty;
}
var value = property.GetValue(request);
if (value == null)
{
return _empty;
}
var entityProperty = typeof(TEntity).GetProperty(property.Name);
if (entityProperty == null || entityProperty.PropertyType != property.PropertyType)
{
return _empty;
}
if (value is string strValue)
{
if (string.IsNullOrEmpty(strValue))
{
return _empty;
}
Builders<TEntity>.Filter.Regex(property.Name, new BsonRegularExpression(strValue, "i"));
}
if (value is DateTime dateTime)
{
}
return _empty;
}
}