SHA256
GuestLog - GuestDocumentList - Added filter for GuestDocuments.
This commit is contained in:
@@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Somnia.GuestLog.Infrastructure;
|
||||
using Somnia.GuestLog.Models.DTOs;
|
||||
using Somnia.GuestLog.Models.Entities;
|
||||
using Somnia.GuestLog.Utils;
|
||||
|
||||
namespace Somnia.GuestLog.Services;
|
||||
|
||||
@@ -127,6 +128,40 @@ public class GuestDocumentService(
|
||||
return dbList.Select(x => GetDtoFromDb(x));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get GuestDocumentDTO from DB with the given filters.
|
||||
/// Result count can be limited for perfomance reasons.
|
||||
/// </summary>
|
||||
/// <param name="filter">Filters to use</param>
|
||||
/// <param name="limit">Result count. Set to 0 for unlimited results</param>
|
||||
/// <returns>Filtered GuestDocumentDTO result list</returns>
|
||||
public async Task<IEnumerable<GuestDocumentDTO>> GetDtoListByFilterAsync(GuestDocFilterDTO filter, int limit = 0)
|
||||
{
|
||||
using var dbContext = contextFactory.CreateDbContext();
|
||||
|
||||
var query = dbContext.GuestDocuments.AsQueryable();
|
||||
|
||||
query = query.WhereIf(!string.IsNullOrEmpty(filter.DocumentType),
|
||||
e => e.DocumentType == filter.DocumentType);
|
||||
query = query.WhereIf(!string.IsNullOrEmpty(filter.DocumentNumber),
|
||||
e => e.DocumentNum == filter.DocumentNumber);
|
||||
|
||||
query = query.WhereIf(!string.IsNullOrEmpty(filter.Name),
|
||||
e => EF.Functions.Like(e.Name, filter.Name));
|
||||
query = query.WhereIf(!string.IsNullOrEmpty(filter.Surname),
|
||||
e => EF.Functions.Like(e.Surname, filter.Surname));
|
||||
|
||||
query = query.OrderByDescending(e => e.UpdateTime);
|
||||
|
||||
if (limit > 0)
|
||||
{
|
||||
query = query.Take(limit);
|
||||
}
|
||||
|
||||
var dbList = await query.ToListAsync();
|
||||
|
||||
return dbList.Select(x => GetDtoFromDb(x));
|
||||
}
|
||||
|
||||
public GuestDocumentDTO GetDtoFromDb(GuestDocument dbItem)
|
||||
{
|
||||
@@ -154,5 +189,4 @@ public class GuestDocumentService(
|
||||
UpdateTime = dbItem.UpdateTime,
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,6 +10,7 @@ public interface IGuestDocumentService
|
||||
public Task<bool> DeleteDocumentAsync(string docType, string docNum);
|
||||
|
||||
public Task<GuestDocumentDTO?> GetDtoByIdAsync(string docType, string docNum);
|
||||
public Task<IEnumerable<GuestDocumentDTO>> GetDtoListByFilterAsync(GuestDocFilterDTO filter, int limit = 0);
|
||||
public Task<IEnumerable<GuestDocumentDTO>> GetDtoListAsync();
|
||||
|
||||
//TODO: Add Filter
|
||||
|
||||
Reference in New Issue
Block a user