diff --git a/Retroactiune.WebAPI/Controllers/TokensController.cs b/Retroactiune.WebAPI/Controllers/TokensController.cs new file mode 100644 index 0000000..f15d1d7 --- /dev/null +++ b/Retroactiune.WebAPI/Controllers/TokensController.cs @@ -0,0 +1,58 @@ +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Retroactiune.DataTransferObjects; +using Retroactiune.Services; + +namespace Retroactiune.Controllers +{ + [ApiController] + [Route("api/v1/[controller]")] + public class TokensController : ControllerBase + { + // TODO: Add tokens, list tokens (unused, used), delete tokens. + + private readonly IFeedbackReceiverService _feedbackReceiverService; + private readonly ITokensService _tokensService; + + public TokensController(IFeedbackReceiverService feedbackReceiverService, ITokensService tokensService) + { + _feedbackReceiverService = feedbackReceiverService; + _tokensService = tokensService; + } + + // TODO: Test unit & integrations. + /// + /// Creates a new batch of tokens, the tokens are tied to a FeedbackReceiver and are used by the client + /// when leaving Feedback. + /// + /// The list of FeedbackReceivers + /// A BasicResponse indicating success. + /// Returns ok. + /// If the items is invalid + [HttpPost] + [ProducesResponseType(typeof(BasicResponse), StatusCodes.Status201Created)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + public async Task GenerateTokens([Required] GenerateTokensDto generateTokensDto) + { + var feedbackReceiverId = generateTokensDto.FeedbackReceiverId; + var result = await _feedbackReceiverService.FindAsync( + new[] {feedbackReceiverId}); + if (!result.Any()) + { + return BadRequest(new BasicResponse + { + Message = $"Invalid FeedbackReceiverId {generateTokensDto.FeedbackReceiverId}." + }); + } + + await _tokensService.GenerateTokensAsync(generateTokensDto.NumberOfTokens, feedbackReceiverId, generateTokensDto.ExpiryTime); + return Ok(new BasicResponse + { + Message = "Tokens generated." + }); + } + } +} \ No newline at end of file diff --git a/Retroactiune.WebAPI/Services/ITokensService.cs b/Retroactiune.WebAPI/Services/ITokensService.cs index 2f1603c..347b559 100644 --- a/Retroactiune.WebAPI/Services/ITokensService.cs +++ b/Retroactiune.WebAPI/Services/ITokensService.cs @@ -1,11 +1,11 @@ -using System.Collections.Generic; +using System; using System.Threading.Tasks; -using Retroactiune.Models; namespace Retroactiune.Services { public interface ITokensService { - public Task CreateManyAsync(IEnumerable items); + public Task GenerateTokensAsync(int numberOfTokens, string feedbackReceiverGuid, + DateTime? expiryTime = null); } } \ No newline at end of file diff --git a/Retroactiune.WebAPI/Services/TokensService.cs b/Retroactiune.WebAPI/Services/TokensService.cs index bae3138..a96fa54 100644 --- a/Retroactiune.WebAPI/Services/TokensService.cs +++ b/Retroactiune.WebAPI/Services/TokensService.cs @@ -1,10 +1,10 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Threading.Tasks; using MongoDB.Driver; using Retroactiune.Models; using Retroactiune.Settings; -// TODO: Test namespace Retroactiune.Services { public class TokensService : ITokensService @@ -17,9 +17,28 @@ namespace Retroactiune.Services _collection = database.GetCollection(settings.TokensCollectionName); } - public async Task CreateManyAsync(IEnumerable items) + public async Task GenerateTokensAsync(int numberOfTokens, string feedbackReceiverGuid, + DateTime? expiryTime = null) { - throw new System.NotImplementedException(); + // TODO: Test unit + if (numberOfTokens <= 0) + { + throw new ArgumentException("numberOfTokens must be positive"); + } + + var token = new List(); + for (var i = 0; i < numberOfTokens; i++) + { + token.Add(new Token + { + CreatedAt = DateTime.UtcNow, + FeedbackReceiverId = feedbackReceiverGuid, + ExpiryTime = null, + TimeUsed = null + }); + } + + await _collection.InsertManyAsync(token); } } } \ No newline at end of file