Implement LIST in FeedbackReceiversController.cs.

This commit is contained in:
Denis-Cosmin Nutiu 2021-06-05 19:39:47 +03:00
parent 30ea3ec92c
commit 4066c53dc4
8 changed files with 65 additions and 24 deletions

View file

@ -195,5 +195,7 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
// Assert // Assert
Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode); Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode);
} }
// TODO test list
} }
} }

View file

@ -67,8 +67,8 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers
// Arrange // Arrange
var mapper = TestUtils.GetMapper(); var mapper = TestUtils.GetMapper();
var mockService = new Mock<IFeedbackReceiverService>(); var mockService = new Mock<IFeedbackReceiverService>();
mockService.Setup(i => i.FindAsync(It.IsAny<IEnumerable<string>>())) mockService.Setup(i => i.FindAsync(It.IsAny<IEnumerable<string>>(), null, null))
.ReturnsAsync(new FeedbackReceiver[] {new FeedbackReceiver()}); .ReturnsAsync(new [] {new FeedbackReceiver()});
// Test // Test
var controller = new FeedbackReceiversController(mockService.Object, mapper, null); var controller = new FeedbackReceiversController(mockService.Object, mapper, null);
@ -76,7 +76,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers
// Assert // Assert
Assert.IsType<OkObjectResult>(result); Assert.IsType<OkObjectResult>(result);
mockService.Verify(s => s.FindAsync(new[] {"bad_guid_but_unit_test_works_cause_validation_doesnt"}), mockService.Verify(s => s.FindAsync(new[] {"bad_guid_but_unit_test_works_cause_validation_doesnt"}, null, null),
Times.Once); Times.Once);
} }
@ -93,8 +93,10 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers
// Assert // Assert
Assert.IsType<NotFoundObjectResult>(result); Assert.IsType<NotFoundObjectResult>(result);
mockService.Verify(s => s.FindAsync(new[] {"bad_guid_but_unit_test_works_cause_validation_doesnt"}), mockService.Verify(s => s.FindAsync(new[] {"bad_guid_but_unit_test_works_cause_validation_doesnt"}, null, null),
Times.Once); Times.Once);
} }
// TODO: test list
} }
} }

View file

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using AutoFixture.Xunit2; using AutoFixture.Xunit2;
@ -35,7 +36,8 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Services
// Test // Test
var service = new FeedbackReceiverService(mongoClientMock.Object, mongoSettingsMock.Object); var service = new FeedbackReceiverService(mongoClientMock.Object, mongoSettingsMock.Object);
await service.CreateManyAsync(items); var feedbackReceivers = items as FeedbackReceiver[] ?? items.ToArray();
await service.CreateManyAsync(feedbackReceivers);
// Assert // Assert
mongoClientMock.Verify(i => i.GetDatabase("MyDB", null), Times.Once()); mongoClientMock.Verify(i => i.GetDatabase("MyDB", null), Times.Once());
@ -43,7 +45,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Services
i => i.GetCollection<FeedbackReceiver>("feedback_receiver", It.IsAny<MongoCollectionSettings>()), i => i.GetCollection<FeedbackReceiver>("feedback_receiver", It.IsAny<MongoCollectionSettings>()),
Times.Once()); Times.Once());
mongoCollectionMock.Verify( mongoCollectionMock.Verify(
i => i.InsertManyAsync(items, It.IsAny<InsertManyOptions>(), It.IsAny<CancellationToken>()), i => i.InsertManyAsync(feedbackReceivers, It.IsAny<InsertManyOptions>(), It.IsAny<CancellationToken>()),
Times.Once()); Times.Once());
} }
@ -130,7 +132,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Services
} }
[Fact] [Fact]
public async Task Test_Delete_Ok() public async Task Test_DeleteOneAsync_Ok()
{ {
// Arrange // Arrange
var mongoDatabaseMock = new Mock<IMongoDatabase>(); var mongoDatabaseMock = new Mock<IMongoDatabase>();
@ -165,7 +167,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Services
} }
[Fact] [Fact]
public async Task Test_Get_Ok() public async Task Test_FindAsync_Ok()
{ {
// Arrange // Arrange
var mongoDatabaseMock = new Mock<IMongoDatabase>(); var mongoDatabaseMock = new Mock<IMongoDatabase>();
@ -193,7 +195,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Services
// Test // Test
var service = new FeedbackReceiverService(mongoClientMock.Object, mongoSettingsMock.Object); var service = new FeedbackReceiverService(mongoClientMock.Object, mongoSettingsMock.Object);
var guids = new[] {"insert_guid_here"}; var guids = new[] {"insert_guid_here"};
await service.FindAsync(guids); await service.FindAsync(guids, 0, 15);
// Assert // Assert
mongoClientMock.Verify(i => i.GetDatabase("MyDB", null), Times.Once()); mongoClientMock.Verify(i => i.GetDatabase("MyDB", null), Times.Once());

View file

@ -111,16 +111,21 @@ namespace Retroactiune.Controllers
return Ok(feedbackReceivers.First()); return Ok(feedbackReceivers.First());
} }
/// <summary>
/// Retrieves a FeedbackReceiver items from the database.
/// </summary>
/// <param name="filter">If set, it will filter results for the given guids.</param>
/// <param name="offset">If set, it will skip the N items.</param>
/// <param name="limit">If set, it will limit the results to N items.</param>
/// <returns>A Ok result with a list of <see cref="FeedbackReceiverOutDto"/>.</returns>
/// <response code="200">The a list is returned.</response>
/// <response code="400">The request is invalid.</response>
[HttpGet] [HttpGet]
public IEnumerable<BasicResponse> List() [ProducesResponseType(typeof(IEnumerable<FeedbackReceiverOutDto>), StatusCodes.Status200OK)]
public async Task<IActionResult> List([FromQuery] IEnumerable<string> filter, [FromQuery] int offset,
[FromQuery] int limit)
{ {
// list all feedback items. return Ok(await _service.FindAsync(filter, offset, limit));
return Enumerable.Range(1, 5).Select(i =>
new BasicResponse()
{
Message = "hwlo"
}
);
} }
} }
} }

View file

@ -3,6 +3,8 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>Retroactiune</RootNamespace> <RootNamespace>Retroactiune</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>1591</NoWarn>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View file

@ -58,13 +58,36 @@ namespace Retroactiune.Services
} }
} }
public async Task<IEnumerable<FeedbackReceiver>> FindAsync(IEnumerable<string> guids) public async Task<IEnumerable<FeedbackReceiver>> FindAsync(IEnumerable<string> guids, int? offset = null,
int? limit = null)
{ {
try try
{ {
var filter = new FilterDefinitionBuilder<FeedbackReceiver>(); var guidsArr = guids as string[] ?? guids.ToArray();
var cursor = await _collection.FindAsync(filter.In(i => i.Id, guids)); var filterBuilder = new FilterDefinitionBuilder<FeedbackReceiver>();
var filterOptions = new FindOptions<FeedbackReceiver, FeedbackReceiver>();
var filter = filterBuilder.Empty;
// Filter for guids
if (guidsArr.Any())
{
filter = filterBuilder.In(i => i.Id, guidsArr);
}
// Set skip
if (offset != null)
{
filterOptions.Skip = offset;
}
// Set limit
if (limit != null)
{
filterOptions.Limit = limit;
}
var cursor = await _collection.FindAsync(filter, filterOptions);
return cursor.ToList(); return cursor.ToList();
} }
catch (Exception e) catch (Exception e)

View file

@ -8,6 +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); Task<IEnumerable<FeedbackReceiver>> FindAsync(IEnumerable<string> guid, int? offset = null, int? limit = null);
} }
} }

View file

@ -1,4 +1,5 @@
using System; using System;
using System.IO;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
@ -43,7 +44,11 @@ namespace Retroactiune
// WebAPI // WebAPI
services.AddControllers(); services.AddControllers();
services.AddSwaggerGen(); services.AddSwaggerGen(c =>
{
var filePath = Path.Combine(AppContext.BaseDirectory, "Retroactiune.WebAPI.xml");
c.IncludeXmlComments(filePath);
});
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.