Implement GET in FeedbackReceiversController.cs

This commit is contained in:
Denis-Cosmin Nutiu 2021-06-05 16:03:12 +03:00
parent 2eb29a4a25
commit b4021724f2
7 changed files with 59 additions and 18 deletions

View file

@ -45,10 +45,10 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
{ {
// Arrange // Arrange
var fixture = new Fixture(); var fixture = new Fixture();
var item = fixture.Create<FeedbackReceiverDto>(); var item = fixture.Create<FeedbackReceiverInDto>();
item.Name = null; item.Name = null;
var jsonContent = JsonSerializer.Serialize(new List<FeedbackReceiverDto> {item}); var jsonContent = JsonSerializer.Serialize(new List<FeedbackReceiverInDto> {item});
// Test // Test
var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceivers/", var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceivers/",
@ -63,10 +63,10 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
{ {
// Arrange // Arrange
var fixture = new Fixture(); var fixture = new Fixture();
var item = fixture.Create<FeedbackReceiverDto>(); var item = fixture.Create<FeedbackReceiverInDto>();
item.Description = null; item.Description = null;
var jsonContent = JsonSerializer.Serialize(new List<FeedbackReceiverDto> {item}); var jsonContent = JsonSerializer.Serialize(new List<FeedbackReceiverInDto> {item});
// Test // Test
var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceivers/", var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceivers/",
@ -77,7 +77,7 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
} }
[Theory, AutoData] [Theory, AutoData]
public async Task Test_CreateFeedbackReceiver_Ok(IEnumerable<FeedbackReceiverDto> items) public async Task Test_CreateFeedbackReceiver_Ok(IEnumerable<FeedbackReceiverInDto> items)
{ {
// Arrange // Arrange
await _mongoDb.DropAsync(); await _mongoDb.DropAsync();

View file

@ -21,7 +21,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers
// Test // Test
var controller = new FeedbackReceiversController(mockService.Object, mapper, null); var controller = new FeedbackReceiversController(mockService.Object, mapper, null);
var result = await controller.Post(new List<FeedbackReceiverDto>()); var result = await controller.Post(new List<FeedbackReceiverInDto>());
// Assert, null because we don't have the ApiBehaviourOptions set, which would generate the IActionResult for the invalid input. // Assert, null because we don't have the ApiBehaviourOptions set, which would generate the IActionResult for the invalid input.
Assert.Null(result); Assert.Null(result);
@ -29,7 +29,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers
} }
[Theory, AutoData] [Theory, AutoData]
public async Task Post_Successful_Creation_Two_items(IEnumerable<FeedbackReceiverDto> items) public async Task Post_Successful_Creation_Two_items(IEnumerable<FeedbackReceiverInDto> items)
{ {
// Arrange // Arrange
var mapper = TestUtils.GetMapper(); var mapper = TestUtils.GetMapper();

View file

@ -39,12 +39,12 @@ namespace Retroactiune.Controllers
[HttpPost] [HttpPost]
[ProducesResponseType(typeof(BasicResponse), StatusCodes.Status201Created)] [ProducesResponseType(typeof(BasicResponse), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> Post([Required] IEnumerable<FeedbackReceiverDto> items) public async Task<IActionResult> Post([Required] IEnumerable<FeedbackReceiverInDto> items)
{ {
var feedbackReceiversDto = items.ToList(); var feedbackReceiversDto = items.ToList();
if (!feedbackReceiversDto.Any()) if (!feedbackReceiversDto.Any())
{ {
ModelState.AddModelError(nameof(IEnumerable<FeedbackReceiverDto>), ModelState.AddModelError(nameof(IEnumerable<FeedbackReceiverInDto>),
"At least one FeedbackReceiver item is required."); "At least one FeedbackReceiver item is required.");
return _apiBehaviorOptions?.Value.InvalidModelStateResponseFactory(ControllerContext); return _apiBehaviorOptions?.Value.InvalidModelStateResponseFactory(ControllerContext);
} }
@ -82,14 +82,32 @@ namespace Retroactiune.Controllers
return NoContent(); return NoContent();
} }
[HttpGet("{id}")] /// <summary>
public BasicResponse Get(long id) /// Retrieves a FeedbackReceiver item from the database.
/// </summary>
/// <param name="guid">The guid of the item to be retrieved.</param>
/// <returns>A Ok result with a <see cref="FeedbackReceiverOutDto"/>.</returns>
/// <response code="200">The item returned successfully.</response>
/// <response code="400">The request is invalid.</response>
/// <response code="404">The item was not found.</response>
[HttpGet("{guid}")]
[ProducesResponseType(typeof(FeedbackReceiverOutDto), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(BasicResponse), StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> Get(
[StringLength(24, ErrorMessage = "invalid guid, must be 24 characters", MinimumLength = 24)]
string guid)
{ {
// get feedback item from db var result = await _service.FindAsync(new[] {guid});
return new BasicResponse() if (!result.Any())
{ {
Message = "hwlo" return NotFound(new BasicResponse()
}; {
Message = $"Item with guid {guid} was not found."
});
}
return Ok(result.First());
} }
[HttpGet] [HttpGet]

View file

@ -7,7 +7,7 @@ namespace Retroactiune
{ {
public MappingProfile() public MappingProfile()
{ {
CreateMap<FeedbackReceiver, FeedbackReceiverDto>().ReverseMap(); CreateMap<FeedbackReceiver, FeedbackReceiverInDto>().ReverseMap();
} }
} }
} }

View file

@ -3,9 +3,9 @@
namespace Retroactiune.Models namespace Retroactiune.Models
{ {
/// <summary> /// <summary>
/// FeedbackReceiverDto is the DTO for <see cref="FeedbackReceiver"/> /// FeedbackReceiverInDto is the DTO for <see cref="FeedbackReceiver"/>, used in incoming requests.
/// </summary> /// </summary>
public class FeedbackReceiverDto public class FeedbackReceiverInDto
{ {
[Required] [Required]
public string Name { get; set; } public string Name { get; set; }
@ -13,4 +13,11 @@ namespace Retroactiune.Models
[Required] [Required]
public string Description { get; set; } public string Description { get; set; }
} }
/// <summary>
/// FeedbackReceiverDto is the DTO for <see cref="FeedbackReceiver"/>, used in outgoing requests.
/// </summary>
public class FeedbackReceiverOutDto : FeedbackReceiver
{
}
} }

View file

@ -56,5 +56,20 @@ namespace Retroactiune.Services
throw new GenericServiceException($"Operation failed: {e.Message}"); throw new GenericServiceException($"Operation failed: {e.Message}");
} }
} }
public async Task<IEnumerable<FeedbackReceiver>> FindAsync(IEnumerable<string> guids)
{
try
{
var filter = new FilterDefinitionBuilder<FeedbackReceiver>();
var cursor = await _collection.FindAsync(filter.In(i => i.Id, guids));
return cursor.ToList();
}
catch (Exception e)
{
throw new GenericServiceException($"Operation failed: {e.Message}");
}
}
} }
} }

View file

@ -8,5 +8,6 @@ namespace Retroactiune.Services
{ {
public Task CreateManyAsync(IEnumerable<FeedbackReceiver> items); public Task CreateManyAsync(IEnumerable<FeedbackReceiver> items);
public Task DeleteOneAsync(string guid); public Task DeleteOneAsync(string guid);
Task<IEnumerable<FeedbackReceiver>> FindAsync(IEnumerable<string> guid);
} }
} }