2021-06-11 19:30:11 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2021-06-09 21:03:07 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using MongoDB.Driver;
|
2021-06-21 14:46:44 +00:00
|
|
|
|
using Retroactiune.Core.Entities;
|
|
|
|
|
using Retroactiune.Core.Interfaces;
|
2021-06-09 21:03:07 +00:00
|
|
|
|
|
2021-06-21 14:46:44 +00:00
|
|
|
|
namespace Retroactiune.Core.Services
|
2021-06-09 21:03:07 +00:00
|
|
|
|
{
|
|
|
|
|
public class TokensService : ITokensService
|
|
|
|
|
{
|
|
|
|
|
private readonly IMongoCollection<Token> _collection;
|
|
|
|
|
|
2021-06-11 20:17:03 +00:00
|
|
|
|
public TokensService(IMongoClient client, IDatabaseSettings settings)
|
2021-06-09 21:03:07 +00:00
|
|
|
|
{
|
|
|
|
|
var database = client.GetDatabase(settings.DatabaseName);
|
|
|
|
|
_collection = database.GetCollection<Token>(settings.TokensCollectionName);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-11 19:30:11 +00:00
|
|
|
|
public async Task GenerateTokensAsync(int numberOfTokens, string feedbackReceiverGuid,
|
|
|
|
|
DateTime? expiryTime = null)
|
2021-06-09 21:03:07 +00:00
|
|
|
|
{
|
2021-06-11 19:30:11 +00:00
|
|
|
|
if (numberOfTokens <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("numberOfTokens must be positive");
|
|
|
|
|
}
|
2021-06-11 19:49:12 +00:00
|
|
|
|
|
2021-06-11 19:30:11 +00:00
|
|
|
|
var token = new List<Token>();
|
|
|
|
|
for (var i = 0; i < numberOfTokens; i++)
|
|
|
|
|
{
|
|
|
|
|
token.Add(new Token
|
|
|
|
|
{
|
|
|
|
|
CreatedAt = DateTime.UtcNow,
|
|
|
|
|
FeedbackReceiverId = feedbackReceiverGuid,
|
2021-06-15 20:08:15 +00:00
|
|
|
|
ExpiryTime = expiryTime,
|
2021-06-11 19:30:11 +00:00
|
|
|
|
TimeUsed = null
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await _collection.InsertManyAsync(token);
|
2021-06-09 21:03:07 +00:00
|
|
|
|
}
|
2021-06-20 11:25:45 +00:00
|
|
|
|
|
|
|
|
|
public async Task DeleteTokens(IEnumerable<string> tokenIds)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var filter = new FilterDefinitionBuilder<Token>();
|
|
|
|
|
await _collection.DeleteManyAsync(filter.In(i => i.Id, tokenIds));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
throw new GenericServiceException($"Operation failed: {e.Message} {e.StackTrace}");
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-27 12:14:34 +00:00
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<Token>> ListTokens(TokenListFilters filters)
|
|
|
|
|
{
|
|
|
|
|
// TODO Write unit tests.
|
|
|
|
|
// TODO: Implement
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
2021-06-09 21:03:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|