From b4021724f296834ba34e6ca15688e4c7167e1b3f Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Sat, 5 Jun 2021 16:03:12 +0300 Subject: [PATCH] Implement GET in FeedbackReceiversController.cs --- .../Controllers/TestFeedbackReceiver.cs | 10 +++--- .../TestFeedbackReceiverController.cs | 4 +-- .../FeedbackReceiversController.cs | 34 ++++++++++++++----- Retroactiune.WebAPI/MappingProfile.cs | 2 +- ...eceiverDto.cs => FeedbackReceiverInDto.cs} | 11 ++++-- .../Services/FeedbackReceiverService.cs | 15 ++++++++ .../Services/IFeedbackReceiverService.cs | 1 + 7 files changed, 59 insertions(+), 18 deletions(-) rename Retroactiune.WebAPI/Models/{FeedbackReceiverDto.cs => FeedbackReceiverInDto.cs} (52%) diff --git a/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiver.cs b/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiver.cs index 707cb5c..ffe07ef 100644 --- a/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiver.cs +++ b/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiver.cs @@ -45,10 +45,10 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers { // Arrange var fixture = new Fixture(); - var item = fixture.Create(); + var item = fixture.Create(); item.Name = null; - var jsonContent = JsonSerializer.Serialize(new List {item}); + var jsonContent = JsonSerializer.Serialize(new List {item}); // Test var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceivers/", @@ -63,10 +63,10 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers { // Arrange var fixture = new Fixture(); - var item = fixture.Create(); + var item = fixture.Create(); item.Description = null; - var jsonContent = JsonSerializer.Serialize(new List {item}); + var jsonContent = JsonSerializer.Serialize(new List {item}); // Test var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceivers/", @@ -77,7 +77,7 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers } [Theory, AutoData] - public async Task Test_CreateFeedbackReceiver_Ok(IEnumerable items) + public async Task Test_CreateFeedbackReceiver_Ok(IEnumerable items) { // Arrange await _mongoDb.DropAsync(); diff --git a/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiverController.cs b/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiverController.cs index 29e7adc..625bc45 100644 --- a/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiverController.cs +++ b/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiverController.cs @@ -21,7 +21,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers // Test var controller = new FeedbackReceiversController(mockService.Object, mapper, null); - var result = await controller.Post(new List()); + var result = await controller.Post(new List()); // Assert, null because we don't have the ApiBehaviourOptions set, which would generate the IActionResult for the invalid input. Assert.Null(result); @@ -29,7 +29,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers } [Theory, AutoData] - public async Task Post_Successful_Creation_Two_items(IEnumerable items) + public async Task Post_Successful_Creation_Two_items(IEnumerable items) { // Arrange var mapper = TestUtils.GetMapper(); diff --git a/Retroactiune.WebAPI/Controllers/FeedbackReceiversController.cs b/Retroactiune.WebAPI/Controllers/FeedbackReceiversController.cs index 9d6b0ba..751a0fb 100644 --- a/Retroactiune.WebAPI/Controllers/FeedbackReceiversController.cs +++ b/Retroactiune.WebAPI/Controllers/FeedbackReceiversController.cs @@ -39,12 +39,12 @@ namespace Retroactiune.Controllers [HttpPost] [ProducesResponseType(typeof(BasicResponse), StatusCodes.Status201Created)] [ProducesResponseType(StatusCodes.Status400BadRequest)] - public async Task Post([Required] IEnumerable items) + public async Task Post([Required] IEnumerable items) { var feedbackReceiversDto = items.ToList(); if (!feedbackReceiversDto.Any()) { - ModelState.AddModelError(nameof(IEnumerable), + ModelState.AddModelError(nameof(IEnumerable), "At least one FeedbackReceiver item is required."); return _apiBehaviorOptions?.Value.InvalidModelStateResponseFactory(ControllerContext); } @@ -82,14 +82,32 @@ namespace Retroactiune.Controllers return NoContent(); } - [HttpGet("{id}")] - public BasicResponse Get(long id) + /// + /// Retrieves a FeedbackReceiver item from the database. + /// + /// The guid of the item to be retrieved. + /// A Ok result with a . + /// The item returned successfully. + /// The request is invalid. + /// The item was not found. + [HttpGet("{guid}")] + [ProducesResponseType(typeof(FeedbackReceiverOutDto), StatusCodes.Status200OK)] + [ProducesResponseType(typeof(BasicResponse), StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + public async Task Get( + [StringLength(24, ErrorMessage = "invalid guid, must be 24 characters", MinimumLength = 24)] + string guid) { - // get feedback item from db - return new BasicResponse() + var result = await _service.FindAsync(new[] {guid}); + if (!result.Any()) { - Message = "hwlo" - }; + return NotFound(new BasicResponse() + { + Message = $"Item with guid {guid} was not found." + }); + } + + return Ok(result.First()); } [HttpGet] diff --git a/Retroactiune.WebAPI/MappingProfile.cs b/Retroactiune.WebAPI/MappingProfile.cs index 3263332..90f4f67 100644 --- a/Retroactiune.WebAPI/MappingProfile.cs +++ b/Retroactiune.WebAPI/MappingProfile.cs @@ -7,7 +7,7 @@ namespace Retroactiune { public MappingProfile() { - CreateMap().ReverseMap(); + CreateMap().ReverseMap(); } } } \ No newline at end of file diff --git a/Retroactiune.WebAPI/Models/FeedbackReceiverDto.cs b/Retroactiune.WebAPI/Models/FeedbackReceiverInDto.cs similarity index 52% rename from Retroactiune.WebAPI/Models/FeedbackReceiverDto.cs rename to Retroactiune.WebAPI/Models/FeedbackReceiverInDto.cs index 264e3a6..dad631d 100644 --- a/Retroactiune.WebAPI/Models/FeedbackReceiverDto.cs +++ b/Retroactiune.WebAPI/Models/FeedbackReceiverInDto.cs @@ -3,9 +3,9 @@ namespace Retroactiune.Models { /// - /// FeedbackReceiverDto is the DTO for + /// FeedbackReceiverInDto is the DTO for , used in incoming requests. /// - public class FeedbackReceiverDto + public class FeedbackReceiverInDto { [Required] public string Name { get; set; } @@ -13,4 +13,11 @@ namespace Retroactiune.Models [Required] public string Description { get; set; } } + + /// + /// FeedbackReceiverDto is the DTO for , used in outgoing requests. + /// + public class FeedbackReceiverOutDto : FeedbackReceiver + { + } } \ No newline at end of file diff --git a/Retroactiune.WebAPI/Services/FeedbackReceiverService.cs b/Retroactiune.WebAPI/Services/FeedbackReceiverService.cs index ce4b232..dad1e1e 100644 --- a/Retroactiune.WebAPI/Services/FeedbackReceiverService.cs +++ b/Retroactiune.WebAPI/Services/FeedbackReceiverService.cs @@ -56,5 +56,20 @@ namespace Retroactiune.Services throw new GenericServiceException($"Operation failed: {e.Message}"); } } + + public async Task> FindAsync(IEnumerable guids) + { + try + { + var filter = new FilterDefinitionBuilder(); + + var cursor = await _collection.FindAsync(filter.In(i => i.Id, guids)); + return cursor.ToList(); + } + catch (Exception e) + { + throw new GenericServiceException($"Operation failed: {e.Message}"); + } + } } } \ No newline at end of file diff --git a/Retroactiune.WebAPI/Services/IFeedbackReceiverService.cs b/Retroactiune.WebAPI/Services/IFeedbackReceiverService.cs index e3128b6..8588020 100644 --- a/Retroactiune.WebAPI/Services/IFeedbackReceiverService.cs +++ b/Retroactiune.WebAPI/Services/IFeedbackReceiverService.cs @@ -8,5 +8,6 @@ namespace Retroactiune.Services { public Task CreateManyAsync(IEnumerable items); public Task DeleteOneAsync(string guid); + Task> FindAsync(IEnumerable guid); } } \ No newline at end of file