From 733bc8eeddda51603f2544373bf30da426326576 Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Tue, 1 Jun 2021 16:20:31 +0300 Subject: [PATCH] Implement DELETE in FeedbackReceiversController --- .../Controllers/TestFeedbackReceiver.cs | 8 ++++---- .../TestFeedbackReceiverController.cs | 4 ++-- ...troller.cs => FeedbackReceiversController.cs} | 12 +++++++----- .../Services/FeedbackReceiverService.cs | 16 ++++++++++++++-- .../Services/IFeedbackReceiverService.cs | 1 + 5 files changed, 28 insertions(+), 13 deletions(-) rename Retroactiune.WebAPI/Controllers/{FeedbackReceiverController.cs => FeedbackReceiversController.cs} (87%) diff --git a/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiver.cs b/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiver.cs index f014cc9..e9e8867 100644 --- a/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiver.cs +++ b/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiver.cs @@ -33,7 +33,7 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers [Fact] public async Task Test_CreateFeedbackReceiver_NoContent() { - var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceiver/", + var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceivers/", new StringContent("[]", Encoding.UTF8, "application/json")); Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode); } @@ -49,7 +49,7 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers var jsonContent = JsonSerializer.Serialize(new List {item}); // Test - var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceiver/", + var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceivers/", new StringContent(jsonContent, Encoding.UTF8, "application/json")); // Assert @@ -67,7 +67,7 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers var jsonContent = JsonSerializer.Serialize(new List {item}); // Test - var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceiver/", + var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceivers/", new StringContent(jsonContent, Encoding.UTF8, "application/json")); // Assert @@ -83,7 +83,7 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers var jsonContent = JsonSerializer.Serialize(feedbackReceiversDto); // Test - var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceiver/", + var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceivers/", new StringContent(jsonContent, Encoding.UTF8, "application/json")); // Assert diff --git a/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiverController.cs b/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiverController.cs index 57141a8..c50d4d3 100644 --- a/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiverController.cs +++ b/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiverController.cs @@ -19,7 +19,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers var mockService = new Mock(); // Test - var controller = new FeedbackReceiverController(mockService.Object, mapper, null); + var controller = new FeedbackReceiversController(mockService.Object, mapper, null); 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. @@ -34,7 +34,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers var mockService = new Mock(); // Test - var controller = new FeedbackReceiverController(mockService.Object, mapper, null); + var controller = new FeedbackReceiversController(mockService.Object, mapper, null); var result = await controller.Post(items); // Assert diff --git a/Retroactiune.WebAPI/Controllers/FeedbackReceiverController.cs b/Retroactiune.WebAPI/Controllers/FeedbackReceiversController.cs similarity index 87% rename from Retroactiune.WebAPI/Controllers/FeedbackReceiverController.cs rename to Retroactiune.WebAPI/Controllers/FeedbackReceiversController.cs index e8395cb..1f571d5 100644 --- a/Retroactiune.WebAPI/Controllers/FeedbackReceiverController.cs +++ b/Retroactiune.WebAPI/Controllers/FeedbackReceiversController.cs @@ -14,13 +14,13 @@ namespace Retroactiune.Controllers { [ApiController] [Route("api/v1/[controller]")] - public class FeedbackReceiverController : ControllerBase + public class FeedbackReceiversController : ControllerBase { private readonly IOptions _apiBehaviorOptions; private readonly IFeedbackReceiverService _service; private readonly IMapper _mapper; - public FeedbackReceiverController(IFeedbackReceiverService service, IMapper mapper, + public FeedbackReceiversController(IFeedbackReceiverService service, IMapper mapper, IOptions apiBehaviorOptions) { _service = service; @@ -64,10 +64,12 @@ namespace Retroactiune.Controllers }); } - [HttpDelete("{id}")] - public NoContentResult Delete(long id) + [HttpDelete("{guid}")] + public async Task Delete( + [StringLength(24, ErrorMessage = "invalid guid, must be 24 characters", MinimumLength = 24)] + string guid) { - // delete feedback item. + await _service.DeleteOneAsync(guid); return NoContent(); } diff --git a/Retroactiune.WebAPI/Services/FeedbackReceiverService.cs b/Retroactiune.WebAPI/Services/FeedbackReceiverService.cs index 3337c0f..ce4b232 100644 --- a/Retroactiune.WebAPI/Services/FeedbackReceiverService.cs +++ b/Retroactiune.WebAPI/Services/FeedbackReceiverService.cs @@ -33,7 +33,7 @@ namespace Retroactiune.Services { throw new GenericServiceException("items must contain at least one element"); } - + try { await _collection.InsertManyAsync(items); @@ -42,7 +42,19 @@ namespace Retroactiune.Services { throw new GenericServiceException($"Operation failed: {e.Message}"); } - + } + + public async Task DeleteOneAsync(string guid) + { + try + { + var filter = new FilterDefinitionBuilder(); + await _collection.DeleteOneAsync(filter.Eq(i => i.Id, guid)); + } + 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 468f418..e3128b6 100644 --- a/Retroactiune.WebAPI/Services/IFeedbackReceiverService.cs +++ b/Retroactiune.WebAPI/Services/IFeedbackReceiverService.cs @@ -7,5 +7,6 @@ namespace Retroactiune.Services public interface IFeedbackReceiverService { public Task CreateManyAsync(IEnumerable items); + public Task DeleteOneAsync(string guid); } } \ No newline at end of file