From 5f5b983f82ecc5c5dcfa9198334244aa6722dabc Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Sun, 20 Jun 2021 14:25:45 +0300 Subject: [PATCH] Implement delete token and delete tokens in TokensController --- .../Controllers/TokensController.cs | 55 +++++++++++++++++-- .../Services/ITokensService.cs | 15 +++++ Retroactiune.WebAPI/Services/TokensService.cs | 14 +++++ 3 files changed, 80 insertions(+), 4 deletions(-) diff --git a/Retroactiune.WebAPI/Controllers/TokensController.cs b/Retroactiune.WebAPI/Controllers/TokensController.cs index 5c48b98..05a803f 100644 --- a/Retroactiune.WebAPI/Controllers/TokensController.cs +++ b/Retroactiune.WebAPI/Controllers/TokensController.cs @@ -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; @@ -22,7 +23,7 @@ namespace Retroactiune.Controllers _feedbackReceiverService = feedbackReceiverService; _tokensService = tokensService; } - + /// /// Creates a new batch of tokens, the tokens are tied to a FeedbackReceiver and are used by the client /// when leaving Feedback. @@ -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." }); } + + /// + /// Deletes tokens identified by ids. + /// + /// A list of token ids. + /// The request to delete the items has been submitted. + /// The request is invalid. + /// + [HttpDelete] + [ProducesResponseType(typeof(NoContentResult), StatusCodes.Status204NoContent)] + [ProducesResponseType(typeof(BasicResponse),StatusCodes.Status400BadRequest)] + public async Task DeleteTokens([Required] IEnumerable tokenIds) + { + // TODO: Unit test, integration test. + try + { + await _tokensService.DeleteTokens(tokenIds); + return NoContent(); + } + catch (GenericServiceException e) + { + return BadRequest(new BasicResponse + { + Message = e.Message + }); + } + } + + /// + /// Deletes a Token given it's guid. + /// + /// The guid of the item to be deleted. + /// A NoContent result. + /// The delete is submitted. + /// The request is invalid. + [HttpDelete("{guid}")] + [ProducesResponseType(typeof(NoContentResult), StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + public async Task 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(); + } } } \ No newline at end of file diff --git a/Retroactiune.WebAPI/Services/ITokensService.cs b/Retroactiune.WebAPI/Services/ITokensService.cs index 347b559..7163444 100644 --- a/Retroactiune.WebAPI/Services/ITokensService.cs +++ b/Retroactiune.WebAPI/Services/ITokensService.cs @@ -1,11 +1,26 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; namespace Retroactiune.Services { public interface ITokensService { + /// + /// Generates tokens. + /// + /// The number of tokens to generate. + /// The feedback receiver guid to reference the tokens. + /// Optional expiry time for the tokens. + /// The result of the generate operation. public Task GenerateTokensAsync(int numberOfTokens, string feedbackReceiverGuid, DateTime? expiryTime = null); + + /// + /// Deletes tokens. + /// + /// A list of tokens to delete. + /// The result of the delete operation. + public Task DeleteTokens(IEnumerable tokenIds); } } \ No newline at end of file diff --git a/Retroactiune.WebAPI/Services/TokensService.cs b/Retroactiune.WebAPI/Services/TokensService.cs index 6d0e7fd..2cc8a39 100644 --- a/Retroactiune.WebAPI/Services/TokensService.cs +++ b/Retroactiune.WebAPI/Services/TokensService.cs @@ -39,5 +39,19 @@ namespace Retroactiune.Services await _collection.InsertManyAsync(token); } + + public async Task DeleteTokens(IEnumerable tokenIds) + { + // TODO: Unit test. + try + { + var filter = new FilterDefinitionBuilder(); + await _collection.DeleteManyAsync(filter.In(i => i.Id, tokenIds)); + } + catch (Exception e) + { + throw new GenericServiceException($"Operation failed: {e.Message} {e.StackTrace}"); + } + } } } \ No newline at end of file