Implement ListTokens in TokensService.cs

This commit is contained in:
Denis-Cosmin Nutiu 2021-06-27 16:51:12 +03:00
parent b03979b8b2
commit 910343207c
3 changed files with 52 additions and 4 deletions

View file

@ -11,7 +11,7 @@ namespace Retroactiune.Core.Services
/// <summary> /// <summary>
/// Id filters tokens by their ids. /// Id filters tokens by their ids.
/// </summary> /// </summary>
public IEnumerable<string> Id { get; set; } public IEnumerable<string> Ids { get; set; }
/// <summary> /// <summary>
/// FeedbackReceiverId filters tokens by their assigned FeedbackReceiverId. /// FeedbackReceiverId filters tokens by their assigned FeedbackReceiverId.

View file

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using MongoDB.Driver; using MongoDB.Driver;
using Retroactiune.Core.Entities; using Retroactiune.Core.Entities;
@ -56,8 +57,55 @@ namespace Retroactiune.Core.Services
public async Task<IEnumerable<Token>> ListTokens(TokenListFilters filters) public async Task<IEnumerable<Token>> ListTokens(TokenListFilters filters)
{ {
// TODO Write unit tests. // TODO Write unit tests.
// TODO: Implement var filterBuilder = new FilterDefinitionBuilder<Token>();
throw new NotImplementedException(); var activeFilters = new List<FilterDefinition<Token>>();
var tokensListFilter = FilterDefinition<Token>.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();
} }
} }
} }

View file

@ -10,7 +10,7 @@ namespace Retroactiune.DataTransferObjects
/// </summary> /// </summary>
public class ListTokensFiltersDto public class ListTokensFiltersDto
{ {
public IEnumerable<string> Id { get; set; } public IEnumerable<string> Ids { get; set; }
[StringLength(24, ErrorMessage = "invalid guid, must be 24 characters", MinimumLength = 24)] [StringLength(24, ErrorMessage = "invalid guid, must be 24 characters", MinimumLength = 24)]
public string FeedbackReceiverId { get; set; } public string FeedbackReceiverId { get; set; }
public DateTime? CreatedAfter { get; set; } public DateTime? CreatedAfter { get; set; }