diff --git a/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiver.cs b/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiver.cs index ba1d6f7..1aa0df8 100644 --- a/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiver.cs +++ b/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiver.cs @@ -195,5 +195,7 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers // Assert Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode); } + + // TODO test list } } \ No newline at end of file diff --git a/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiverController.cs b/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiverController.cs index 5117497..4d3109e 100644 --- a/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiverController.cs +++ b/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiverController.cs @@ -67,8 +67,8 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers // Arrange var mapper = TestUtils.GetMapper(); var mockService = new Mock(); - mockService.Setup(i => i.FindAsync(It.IsAny>())) - .ReturnsAsync(new FeedbackReceiver[] {new FeedbackReceiver()}); + mockService.Setup(i => i.FindAsync(It.IsAny>(), null, null)) + .ReturnsAsync(new [] {new FeedbackReceiver()}); // Test var controller = new FeedbackReceiversController(mockService.Object, mapper, null); @@ -76,7 +76,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers // Assert Assert.IsType(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); } @@ -93,8 +93,10 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers // Assert Assert.IsType(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); } + + // TODO: test list } } \ No newline at end of file diff --git a/Retroactiune.UnitTests/Retroactiune.WebAPI/Services/TestFeedbackReceiverService.cs b/Retroactiune.UnitTests/Retroactiune.WebAPI/Services/TestFeedbackReceiverService.cs index 07a7ceb..d0a8b12 100644 --- a/Retroactiune.UnitTests/Retroactiune.WebAPI/Services/TestFeedbackReceiverService.cs +++ b/Retroactiune.UnitTests/Retroactiune.WebAPI/Services/TestFeedbackReceiverService.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using System.Threading; using System.Threading.Tasks; using AutoFixture.Xunit2; @@ -35,7 +36,8 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Services // Test var service = new FeedbackReceiverService(mongoClientMock.Object, mongoSettingsMock.Object); - await service.CreateManyAsync(items); + var feedbackReceivers = items as FeedbackReceiver[] ?? items.ToArray(); + await service.CreateManyAsync(feedbackReceivers); // Assert mongoClientMock.Verify(i => i.GetDatabase("MyDB", null), Times.Once()); @@ -43,7 +45,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Services i => i.GetCollection("feedback_receiver", It.IsAny()), Times.Once()); mongoCollectionMock.Verify( - i => i.InsertManyAsync(items, It.IsAny(), It.IsAny()), + i => i.InsertManyAsync(feedbackReceivers, It.IsAny(), It.IsAny()), Times.Once()); } @@ -130,7 +132,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Services } [Fact] - public async Task Test_Delete_Ok() + public async Task Test_DeleteOneAsync_Ok() { // Arrange var mongoDatabaseMock = new Mock(); @@ -165,7 +167,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Services } [Fact] - public async Task Test_Get_Ok() + public async Task Test_FindAsync_Ok() { // Arrange var mongoDatabaseMock = new Mock(); @@ -193,7 +195,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Services // Test var service = new FeedbackReceiverService(mongoClientMock.Object, mongoSettingsMock.Object); var guids = new[] {"insert_guid_here"}; - await service.FindAsync(guids); + await service.FindAsync(guids, 0, 15); // Assert mongoClientMock.Verify(i => i.GetDatabase("MyDB", null), Times.Once()); diff --git a/Retroactiune.WebAPI/Controllers/FeedbackReceiversController.cs b/Retroactiune.WebAPI/Controllers/FeedbackReceiversController.cs index 0333fc7..65bf96a 100644 --- a/Retroactiune.WebAPI/Controllers/FeedbackReceiversController.cs +++ b/Retroactiune.WebAPI/Controllers/FeedbackReceiversController.cs @@ -111,16 +111,21 @@ namespace Retroactiune.Controllers return Ok(feedbackReceivers.First()); } + /// + /// Retrieves a FeedbackReceiver items from the database. + /// + /// If set, it will filter results for the given guids. + /// If set, it will skip the N items. + /// If set, it will limit the results to N items. + /// A Ok result with a list of . + /// The a list is returned. + /// The request is invalid. [HttpGet] - public IEnumerable List() + [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] + public async Task List([FromQuery] IEnumerable filter, [FromQuery] int offset, + [FromQuery] int limit) { - // list all feedback items. - return Enumerable.Range(1, 5).Select(i => - new BasicResponse() - { - Message = "hwlo" - } - ); + return Ok(await _service.FindAsync(filter, offset, limit)); } } } \ No newline at end of file diff --git a/Retroactiune.WebAPI/Retroactiune.WebAPI.csproj b/Retroactiune.WebAPI/Retroactiune.WebAPI.csproj index 3125d46..50f595c 100644 --- a/Retroactiune.WebAPI/Retroactiune.WebAPI.csproj +++ b/Retroactiune.WebAPI/Retroactiune.WebAPI.csproj @@ -3,6 +3,8 @@ netcoreapp3.1 Retroactiune + true + 1591 diff --git a/Retroactiune.WebAPI/Services/FeedbackReceiverService.cs b/Retroactiune.WebAPI/Services/FeedbackReceiverService.cs index 9280855..1eb452c 100644 --- a/Retroactiune.WebAPI/Services/FeedbackReceiverService.cs +++ b/Retroactiune.WebAPI/Services/FeedbackReceiverService.cs @@ -58,13 +58,36 @@ namespace Retroactiune.Services } } - public async Task> FindAsync(IEnumerable guids) + public async Task> FindAsync(IEnumerable guids, int? offset = null, + int? limit = null) { try { - var filter = new FilterDefinitionBuilder(); + var guidsArr = guids as string[] ?? guids.ToArray(); - var cursor = await _collection.FindAsync(filter.In(i => i.Id, guids)); + var filterBuilder = new FilterDefinitionBuilder(); + var filterOptions = new FindOptions(); + 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(); } catch (Exception e) diff --git a/Retroactiune.WebAPI/Services/IFeedbackReceiverService.cs b/Retroactiune.WebAPI/Services/IFeedbackReceiverService.cs index 8588020..4399d15 100644 --- a/Retroactiune.WebAPI/Services/IFeedbackReceiverService.cs +++ b/Retroactiune.WebAPI/Services/IFeedbackReceiverService.cs @@ -8,6 +8,6 @@ namespace Retroactiune.Services { public Task CreateManyAsync(IEnumerable items); public Task DeleteOneAsync(string guid); - Task> FindAsync(IEnumerable guid); + Task> FindAsync(IEnumerable guid, int? offset = null, int? limit = null); } } \ No newline at end of file diff --git a/Retroactiune.WebAPI/Startup.cs b/Retroactiune.WebAPI/Startup.cs index 20e394e..7cc13bf 100644 --- a/Retroactiune.WebAPI/Startup.cs +++ b/Retroactiune.WebAPI/Startup.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; @@ -32,7 +33,7 @@ namespace Retroactiune // AutoMapper services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); - + // Services services.AddSingleton(); services.AddSingleton(i => @@ -40,10 +41,14 @@ namespace Retroactiune var settings = i.GetService>(); return new MongoClient(settings.Value.ConnectionString); }); - + // WebAPI 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.