2021-06-20 11:25:45 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2021-06-11 19:30:11 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-06-27 12:14:34 +00:00
|
|
|
|
using AutoMapper;
|
2021-06-11 19:30:11 +00:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2021-06-27 12:14:34 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Retroactiune.Core.Entities;
|
2021-06-21 14:46:44 +00:00
|
|
|
|
using Retroactiune.Core.Interfaces;
|
|
|
|
|
using Retroactiune.Core.Services;
|
2021-06-11 19:30:11 +00:00
|
|
|
|
using Retroactiune.DataTransferObjects;
|
|
|
|
|
|
|
|
|
|
namespace Retroactiune.Controllers
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/v1/[controller]")]
|
|
|
|
|
public class TokensController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly IFeedbackReceiverService _feedbackReceiverService;
|
|
|
|
|
private readonly ITokensService _tokensService;
|
2021-06-27 12:14:34 +00:00
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
|
private readonly ILogger _logger;
|
2021-06-11 19:30:11 +00:00
|
|
|
|
|
2021-06-27 12:14:34 +00:00
|
|
|
|
public TokensController(IFeedbackReceiverService feedbackReceiverService, ITokensService tokensService,
|
|
|
|
|
ILogger<TokensController> logger, IMapper mapper)
|
2021-06-11 19:30:11 +00:00
|
|
|
|
{
|
|
|
|
|
_feedbackReceiverService = feedbackReceiverService;
|
|
|
|
|
_tokensService = tokensService;
|
2021-06-27 12:14:34 +00:00
|
|
|
|
_mapper = mapper;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The list tokens controller retrieves a list of tokens.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filtersDto">Object that holds filters for listing tokens.</param>
|
|
|
|
|
/// <response code="200">A list of tokens.</response>
|
|
|
|
|
/// <response code="400">The request is invalid.</response>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<Token>), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
|
|
public async Task<IActionResult> ListTokens([FromQuery] ListTokensFiltersDto filtersDto)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var tokenFilters = _mapper.Map<TokenListFilters>(filtersDto);
|
|
|
|
|
var response = await _tokensService.ListTokens(tokenFilters);
|
|
|
|
|
return Ok(response);
|
|
|
|
|
}
|
|
|
|
|
catch (GenericServiceException e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("{Message}", e.Message);
|
|
|
|
|
return BadRequest(new BasicResponse()
|
|
|
|
|
{
|
|
|
|
|
Message = e.Message
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-06-11 19:30:11 +00:00
|
|
|
|
}
|
2021-06-20 11:25:45 +00:00
|
|
|
|
|
2021-06-11 19:30:11 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new batch of tokens, the tokens are tied to a FeedbackReceiver and are used by the client
|
|
|
|
|
/// when leaving Feedback.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="generateTokensDto">The list of FeedbackReceivers</param>
|
|
|
|
|
/// <returns>A BasicResponse indicating success.</returns>
|
|
|
|
|
/// <response code="200">Returns ok.</response>
|
|
|
|
|
/// <response code="400">If the items is invalid</response>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[ProducesResponseType(typeof(BasicResponse), StatusCodes.Status201Created)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
|
|
public async Task<IActionResult> 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}."
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-20 11:25:45 +00:00
|
|
|
|
await _tokensService.GenerateTokensAsync(generateTokensDto.NumberOfTokens, feedbackReceiverId,
|
|
|
|
|
generateTokensDto.ExpiryTime);
|
2021-06-11 19:30:11 +00:00
|
|
|
|
return Ok(new BasicResponse
|
|
|
|
|
{
|
|
|
|
|
Message = "Tokens generated."
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-06-20 11:25:45 +00:00
|
|
|
|
|
|
|
|
|
/// <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)]
|
2021-06-27 12:14:34 +00:00
|
|
|
|
[ProducesResponseType(typeof(BasicResponse), StatusCodes.Status400BadRequest)]
|
2021-06-20 11:25:45 +00:00
|
|
|
|
public async Task<IActionResult> DeleteTokens([Required] IEnumerable<string> tokenIds)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await _tokensService.DeleteTokens(tokenIds);
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
catch (GenericServiceException e)
|
|
|
|
|
{
|
2021-06-27 12:14:34 +00:00
|
|
|
|
_logger.LogError("{Message}", e.Message);
|
2021-06-20 11:25:45 +00:00
|
|
|
|
return BadRequest(new BasicResponse
|
|
|
|
|
{
|
|
|
|
|
Message = e.Message
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-27 12:14:34 +00:00
|
|
|
|
|
2021-06-20 11:25:45 +00:00
|
|
|
|
/// <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)]
|
2021-06-20 13:53:43 +00:00
|
|
|
|
public async Task<IActionResult> DeleteToken(
|
2021-06-27 12:14:34 +00:00
|
|
|
|
[StringLength(24, ErrorMessage = "invalid guid, must be 24 characters", MinimumLength = 24)]
|
|
|
|
|
string guid)
|
2021-06-20 11:25:45 +00:00
|
|
|
|
{
|
2021-06-20 13:53:43 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2021-06-27 12:14:34 +00:00
|
|
|
|
await _tokensService.DeleteTokens(new[] {guid});
|
2021-06-20 13:53:43 +00:00
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
catch (GenericServiceException e)
|
|
|
|
|
{
|
2021-06-27 12:14:34 +00:00
|
|
|
|
_logger.LogError("{Message}", e.Message);
|
2021-06-20 13:53:43 +00:00
|
|
|
|
return BadRequest(new BasicResponse
|
|
|
|
|
{
|
|
|
|
|
Message = e.Message
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-06-20 11:25:45 +00:00
|
|
|
|
}
|
2021-06-11 19:30:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|