diff --git a/Retroactiune.Core/Services/TokenListFilters.cs b/Retroactiune.Core/Services/TokenListFilters.cs index cc2e9a4..1c46eb9 100644 --- a/Retroactiune.Core/Services/TokenListFilters.cs +++ b/Retroactiune.Core/Services/TokenListFilters.cs @@ -11,7 +11,7 @@ namespace Retroactiune.Core.Services /// /// Id filters tokens by their ids. /// - public IEnumerable Id { get; set; } + public IEnumerable Ids { get; set; } /// /// FeedbackReceiverId filters tokens by their assigned FeedbackReceiverId. diff --git a/Retroactiune.Core/Services/TokensService.cs b/Retroactiune.Core/Services/TokensService.cs index 95076b0..005792e 100644 --- a/Retroactiune.Core/Services/TokensService.cs +++ b/Retroactiune.Core/Services/TokensService.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using MongoDB.Driver; using Retroactiune.Core.Entities; @@ -56,8 +57,55 @@ namespace Retroactiune.Core.Services public async Task> ListTokens(TokenListFilters filters) { // TODO Write unit tests. - // TODO: Implement - throw new NotImplementedException(); + var filterBuilder = new FilterDefinitionBuilder(); + var activeFilters = new List>(); + var tokensListFilter = FilterDefinition.Empty; + + // Filter by token ids. + if (filters.Ids.Any()) + { + activeFilters.Add(filterBuilder.In(i => i.Id, filters.Ids)); + } + + // Filter tokens by their assigned feedback receiver id. + if (filters.FeedbackReceiverId != null) + { + activeFilters.Add(filterBuilder.Eq(i => i.FeedbackReceiverId, filters.FeedbackReceiverId)); + } + + // Datetime after + if (filters.CreatedAfter != null) + { + activeFilters.Add(filterBuilder.Gte(i => i.CreatedAt, filters.CreatedAfter)); + } + + // Datetime before + if (filters.CreatedBefore != null) + { + activeFilters.Add(filterBuilder.Lte(i => i.CreatedAt, filters.CreatedBefore)); + } + + // Used time after + if (filters.UsedAfter != null) + { + activeFilters.Add(filterBuilder.Gte(i => i.TimeUsed, filters.UsedAfter)); + } + + // Used time before + if (filters.UsedBefore != null) + { + activeFilters.Add(filterBuilder.Lte(i => i.TimeUsed, filters.UsedBefore)); + } + + + // Construct the final filter. + if (activeFilters.Any()) + { + tokensListFilter = filterBuilder.And(activeFilters); + } + + var results = await _collection.FindAsync(tokensListFilter); + return await results.ToListAsync(); } } } \ No newline at end of file diff --git a/Retroactiune.WebAPI/DataTransferObjects/ListTokensFiltersDto.cs b/Retroactiune.WebAPI/DataTransferObjects/ListTokensFiltersDto.cs index 369674f..16dc580 100644 --- a/Retroactiune.WebAPI/DataTransferObjects/ListTokensFiltersDto.cs +++ b/Retroactiune.WebAPI/DataTransferObjects/ListTokensFiltersDto.cs @@ -10,7 +10,7 @@ namespace Retroactiune.DataTransferObjects /// public class ListTokensFiltersDto { - public IEnumerable Id { get; set; } + public IEnumerable Ids { get; set; } [StringLength(24, ErrorMessage = "invalid guid, must be 24 characters", MinimumLength = 24)] public string FeedbackReceiverId { get; set; } public DateTime? CreatedAfter { get; set; }