Implement GET Feedbacks operation.

This commit is contained in:
Denis-Cosmin Nutiu 2021-07-17 20:14:37 +03:00
parent 9a4aeced64
commit 85fcc29b61
7 changed files with 114 additions and 9 deletions

View file

@ -1,5 +1,7 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
using Retroactiune.Core.Entities;
using Retroactiune.Core.Services;
namespace Retroactiune.Core.Interfaces
{
@ -13,5 +15,12 @@ namespace Retroactiune.Core.Interfaces
/// <param name="receiver">The feedback receiver.</param>
/// <returns></returns>
public Task AddFeedbackAsync(Feedback feedback, FeedbackReceiver receiver);
/// <summary>
/// Gets Feedbacks.
/// </summary>
/// <param name="filters">Filters for filtering the response.</param>
/// <returns>An enumerable of feedbacks.</returns>
Task<IEnumerable<Feedback>> GetFeedbacksAsync(FeedbacksListFilters filters);
}
}

View file

@ -0,0 +1,31 @@
using System;
namespace Retroactiune.Core.Services
{
/// <summary>
/// FeedbacksListFilters is a data class for filtering Feedbacks.
/// </summary>
public class FeedbacksListFilters
{
/// <summary>
/// FeedbackReceiverId the ID of the FeedbackReceiver.
/// </summary>
public string FeedbackReceiverId { get; set; }
/// <summary>
/// CreatedAfter filters items that have been created after the given date.
/// </summary>
public DateTime? CreatedAfter { get; set; }
/// <summary>
/// CreatedBefore filters items that have been created before the given date.
/// </summary>
public DateTime? CreatedBefore { get; set; }
/// <summary>
/// Rating filters for the rating.
/// </summary>
public uint Rating { get; set; }
}
}

View file

@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
using Ardalis.GuardClauses;
using MongoDB.Driver;
using Retroactiune.Core.Entities;
@ -24,5 +25,34 @@ namespace Retroactiune.Core.Services
feedback.FeedbackReceiverId = receiver.Id;
await _collection.InsertOneAsync(feedback);
}
public async Task<IEnumerable<Feedback>> GetFeedbacksAsync(FeedbacksListFilters filters)
{
// TODO: Unit test.
Guard.Against.Null(filters, nameof(filters));
Guard.Against.Null(filters.FeedbackReceiverId, nameof(filters.FeedbackReceiverId));
var filterBuilder = new FilterDefinitionBuilder<Feedback>();
var activeFilters = new List<FilterDefinition<Feedback>>
{
// Filter tokens by their assigned feedback receiver id.
filterBuilder.Eq(i => i.FeedbackReceiverId, filters.FeedbackReceiverId)
};
// Datetime after
if (filters.CreatedAfter != null)
{
activeFilters.Add(filterBuilder.Gte(i => i.CreatedAt, filters.CreatedAfter));
}
// Datetime before
if (filters.CreatedBefore != null)
{
activeFilters.Add(filterBuilder.Lte(i => i.CreatedAt, filters.CreatedBefore));
}
var results = await _collection.FindAsync(filterBuilder.And(activeFilters));
return await results.ToListAsync();
}
}
}

View file

@ -4,7 +4,7 @@ using System.Collections.Generic;
namespace Retroactiune.Core.Services
{
/// <summary>
/// TokenListFilters is a data class representing the filters used to list tokens.
/// TokenListFilters is a data class for filtering Tokens.
/// </summary>
public class TokenListFilters
{

View file

@ -129,6 +129,7 @@ namespace Retroactiune.Controllers
/// <response code="400">The request is invalid.</response>
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<FeedbackReceiverOutDto>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(BasicResponse), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> List([FromQuery] IEnumerable<string> filter,
[RangeAttribute(1, int.MaxValue, ErrorMessage = "offset is out of range, allowed ranges [1-IntMax]"),
FromQuery]
@ -144,7 +145,7 @@ namespace Retroactiune.Controllers
/// </summary>
/// <param name="ids">A list of FeedbackReceiver ids.</param>
/// <response code="204">The request to delete the items has been submitted.</response>
/// <response code="404">The request is invalid.</response>
/// <response code="400">The request is invalid.</response>
/// <returns></returns>
[HttpDelete]
[ProducesResponseType(typeof(NoContentResult), StatusCodes.Status204NoContent)]
@ -174,13 +175,14 @@ namespace Retroactiune.Controllers
/// <param name="guid">The guid of the FeedbackReceiver to add feedback.</param>
/// <param name="feedbackInDto">The feedback dto.</param>
/// <response code="200">The feedback has been added.</response>
/// <response code="404">The request is invalid.</response>
/// <response code="400">The request is invalid.</response>
/// <returns></returns>
[HttpPost("{guid}/feedbacks")]
[ProducesResponseType(typeof(NoContentResult), StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(BasicResponse), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> AddFeedback(string guid, [FromBody] FeedbackInDto feedbackInDto)
{
// TODO: Remove guid from request and infer it from the given token.
var receivers = await _feedbackReceiversService.FindAsync(new[] {guid}, limit: 1);
var tokenEnum = await _tokensService.FindAsync(new TokenListFilters
{
@ -219,6 +221,24 @@ namespace Retroactiune.Controllers
return Ok();
}
// TODO: Implement get for feedbacks.
/// <summary>
/// Returns the Feedbacks of a FeedbackReceiver. See <see cref="Feedback"/> and <see cref="FeedbackReceiver"/>.
/// </summary>
/// <param name="guid">The guid of the FeedbackReceiver.</param>
/// <param name="filters">Query filters for filtering the response.</param>
/// <response code="200">The feedback has been added.</response>
/// <response code="400">The request is invalid.</response>
/// <returns></returns>
[HttpGet("{guid}/feedbacks")]
[ProducesResponseType(typeof(NoContentResult), StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(BasicResponse), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> GetFeedbacks(string guid, [FromQuery] ListFeedbacksFiltersDto filters)
{
// TODO: Unit & Integration test.
var feedbacksListFilters = _mapper.Map<FeedbacksListFilters>(filters);
feedbacksListFilters.FeedbackReceiverId = guid;
var response = await _feedbacksService.GetFeedbacksAsync(feedbacksListFilters);
return Ok(response);
}
}
}

View file

@ -0,0 +1,14 @@
using System;
namespace Retroactiune.DataTransferObjects
{
/// <summary>
/// DTO with filters for listing Feedbacks.
/// </summary>
public class ListFeedbacksFiltersDto
{
public uint Rating { get; set; }
public DateTime? CreatedAfter { get; set; }
public DateTime? CreatedBefore { get; set; }
}
}

View file

@ -10,6 +10,7 @@ namespace Retroactiune
public MappingProfile()
{
CreateMap<ListTokensFiltersDto, TokenListFilters>();
CreateMap<ListFeedbacksFiltersDto, FeedbacksListFilters>();
CreateMap<FeedbackReceiver, FeedbackReceiverInDto>().ReverseMap();
CreateMap<FeedbackReceiver, FeedbackReceiverOutDto>();
CreateMap<FeedbackInDto, Feedback>();