Implement delete token and delete tokens in TokensController

This commit is contained in:
Denis-Cosmin Nutiu 2021-06-20 14:25:45 +03:00
parent 2c53668f63
commit 5f5b983f82
3 changed files with 80 additions and 4 deletions

View file

@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
@ -12,7 +13,7 @@ namespace Retroactiune.Controllers
[Route("api/v1/[controller]")]
public class TokensController : ControllerBase
{
// TODO: Add tokens, list tokens (unused, used), delete tokens.
// TODO: list tokens (unused, used)
private readonly IFeedbackReceiverService _feedbackReceiverService;
private readonly ITokensService _tokensService;
@ -47,11 +48,57 @@ namespace Retroactiune.Controllers
});
}
await _tokensService.GenerateTokensAsync(generateTokensDto.NumberOfTokens, feedbackReceiverId, generateTokensDto.ExpiryTime);
await _tokensService.GenerateTokensAsync(generateTokensDto.NumberOfTokens, feedbackReceiverId,
generateTokensDto.ExpiryTime);
return Ok(new BasicResponse
{
Message = "Tokens generated."
});
}
/// <summary>
/// Deletes tokens identified by ids.
/// </summary>
/// <param name="tokenIds">A list of token ids.</param>
/// <response code="204">The request to delete the items has been submitted.</response>
/// <response code="404">The request is invalid.</response>
/// <returns></returns>
[HttpDelete]
[ProducesResponseType(typeof(NoContentResult), StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(BasicResponse),StatusCodes.Status400BadRequest)]
public async Task<IActionResult> DeleteTokens([Required] IEnumerable<string> tokenIds)
{
// TODO: Unit test, integration test.
try
{
await _tokensService.DeleteTokens(tokenIds);
return NoContent();
}
catch (GenericServiceException e)
{
return BadRequest(new BasicResponse
{
Message = e.Message
});
}
}
/// <summary>
/// Deletes a Token given it's guid.
/// </summary>
/// <param name="guid">The guid of the item to be deleted.</param>
/// <returns>A NoContent result.</returns>
/// <response code="204">The delete is submitted.</response>
/// <response code="400">The request is invalid.</response>
[HttpDelete("{guid}")]
[ProducesResponseType(typeof(NoContentResult), StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<NoContentResult> DeleteToken(
[StringLength(24, ErrorMessage = "invalid guid, must be 24 characters", MinimumLength = 24)] string guid)
{
// TODO: Unit test, integration test.
await _tokensService.DeleteTokens(new []{ guid });
return NoContent();
}
}
}

View file

@ -1,11 +1,26 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Retroactiune.Services
{
public interface ITokensService
{
/// <summary>
/// Generates tokens.
/// </summary>
/// <param name="numberOfTokens">The number of tokens to generate.</param>
/// <param name="feedbackReceiverGuid">The feedback receiver guid to reference the tokens.</param>
/// <param name="expiryTime">Optional expiry time for the tokens.</param>
/// <returns>The result of the generate operation.</returns>
public Task GenerateTokensAsync(int numberOfTokens, string feedbackReceiverGuid,
DateTime? expiryTime = null);
/// <summary>
/// Deletes tokens.
/// </summary>
/// <param name="tokenIds">A list of tokens to delete.</param>
/// <returns>The result of the delete operation.</returns>
public Task DeleteTokens(IEnumerable<string> tokenIds);
}
}

View file

@ -39,5 +39,19 @@ namespace Retroactiune.Services
await _collection.InsertManyAsync(token);
}
public async Task DeleteTokens(IEnumerable<string> tokenIds)
{
// TODO: Unit test.
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}");
}
}
}
}