using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Retroactiune.Core.Entities;
using Retroactiune.Core.Services;
using Retroactiune.DataTransferObjects;
namespace Retroactiune.Controllers
{
///
/// Implementation FeedbackReceiversController and Feedbacks related functionality.
///
public partial class FeedbackReceiversController
{
///
/// Add Feedback to a FeedbackReceiver.
///
/// The feedback dto.
/// The feedback has been added.
/// The request is invalid.
///
[HttpPost("feedbacks")]
[ProducesResponseType(typeof(NoContentResult), StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(BasicResponse), StatusCodes.Status400BadRequest)]
public async Task AddFeedback([FromBody] FeedbackInDto feedbackInDto)
{
var tokenEnum = await _tokensService.FindAsync(new TokenListFilters
{
Ids = new[] {feedbackInDto.TokenId}
});
var tokens = (tokenEnum as Token[] ?? tokenEnum.ToArray());
if (tokens.Length == 0)
{
return BadRequest(new BasicResponse
{
Message = "Token not found."
});
}
var token = tokens[0];
var receivers = await _feedbackReceiversService.FindAsync(
new[] {token.FeedbackReceiverId}, limit: 1
);
var feedbackReceivers = receivers as FeedbackReceiver[] ?? receivers.ToArray();
if (!feedbackReceivers.Any())
{
return BadRequest(new BasicResponse
{
Message = $"FeedbackReceiver with id {token.FeedbackReceiverId} not found."
});
}
if (!token.IsValid(feedbackReceivers[0]))
{
return BadRequest(new BasicResponse
{
Message = "Token is invalid."
});
}
var feedback = _mapper.Map(feedbackInDto);
await Task.WhenAll(_tokensService.MarkTokenAsUsedAsync(token),
_feedbacksService.AddFeedbackAsync(feedback, feedbackReceivers[0]));
return Ok();
}
///
/// Returns the Feedbacks of a FeedbackReceiver. See and .
///
/// The guid of the FeedbackReceiver.
/// Query filters for filtering the response.
/// The feedback has been added.
/// The request is invalid.
///
[HttpGet("{guid}/feedbacks")]
[ProducesResponseType(typeof(NoContentResult), StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(BasicResponse), StatusCodes.Status400BadRequest)]
public async Task GetFeedbacks(string guid, [FromQuery] ListFeedbacksFiltersDto filters)
{
// TODO: Integration test.
var feedbacksListFilters = _mapper.Map(filters);
feedbacksListFilters.FeedbackReceiverId = guid;
var response = await _feedbacksService.GetFeedbacksAsync(feedbacksListFilters);
return Ok(response);
}
}
}