using Dpz.Core.Public.ViewModel.Request;
using Dpz.Core.Public.ViewModel.Response;
namespace Dpz.Core.Service.RepositoryServiceImpl;
public class UserHistoryService(IRepository<UserHistory> repository) : IUserHistoryService
{
public async Task<IPagedList<UserHistoryResponse>> GetPagedListAsync(UserHistoryRequest request)
{
var filterEmpty = Builders<UserHistory>.Filter.Empty;
var filters = new List<FilterDefinition<UserHistory>>();
if (!string.IsNullOrEmpty(request.Account))
{
var accountFilter = Builders<UserHistory>.Filter.Regex(
x => x.Account,
new BsonRegularExpression(request.Account, "i")
);
filters.Add(accountFilter);
}
if (request.ChangeTimeStart.HasValue)
{
var startFilter = Builders<UserHistory>.Filter.Gte(
x => x.ChangeTime,
request.ChangeTimeStart.Value
);
filters.Add(startFilter);
}
if (request.ChangeTimeEnd.HasValue)
{
var endFilter = Builders<UserHistory>.Filter.Lte(
x => x.ChangeTime,
request.ChangeTimeEnd.Value
);
filters.Add(endFilter);
}
if (!string.IsNullOrEmpty(request.ChangeProperty))
{
var propertyFilter = Builders<UserHistory>.Filter.Eq(
x => x.Changes.Any(y => y.Field == request.ChangeProperty),
true
);
filters.Add(propertyFilter);
}
var filterResult =
filters.Count > 0 ? Builders<UserHistory>.Filter.And(filters) : filterEmpty;
return await repository
.SearchFor(filterResult)
.SortByDescending(x => x.Id)
.ToPagedListAsync<UserHistory, UserHistoryResponse>(
request.PageIndex,
request.PageSize
);
}
public async Task<List<string>> GetChangedPropertiesAsync()
{
return await repository
.MongodbQueryable.SelectMany(x => x.Changes)
.GroupBy(x => x.Field)
.Select(x => x.Key)
.ToListAsync();
}
}