Implement DELETE in FeedbackReceiversController

This commit is contained in:
Denis-Cosmin Nutiu 2021-06-01 16:20:31 +03:00
parent 035d6d177a
commit 733bc8eedd
5 changed files with 28 additions and 13 deletions

View file

@ -33,7 +33,7 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
[Fact] [Fact]
public async Task Test_CreateFeedbackReceiver_NoContent() 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")); new StringContent("[]", Encoding.UTF8, "application/json"));
Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode); Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode);
} }
@ -49,7 +49,7 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
var jsonContent = JsonSerializer.Serialize(new List<FeedbackReceiverDto> {item}); var jsonContent = JsonSerializer.Serialize(new List<FeedbackReceiverDto> {item});
// Test // Test
var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceiver/", var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceivers/",
new StringContent(jsonContent, Encoding.UTF8, "application/json")); new StringContent(jsonContent, Encoding.UTF8, "application/json"));
// Assert // Assert
@ -67,7 +67,7 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
var jsonContent = JsonSerializer.Serialize(new List<FeedbackReceiverDto> {item}); var jsonContent = JsonSerializer.Serialize(new List<FeedbackReceiverDto> {item});
// Test // Test
var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceiver/", var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceivers/",
new StringContent(jsonContent, Encoding.UTF8, "application/json")); new StringContent(jsonContent, Encoding.UTF8, "application/json"));
// Assert // Assert
@ -83,7 +83,7 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
var jsonContent = JsonSerializer.Serialize(feedbackReceiversDto); var jsonContent = JsonSerializer.Serialize(feedbackReceiversDto);
// Test // Test
var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceiver/", var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceivers/",
new StringContent(jsonContent, Encoding.UTF8, "application/json")); new StringContent(jsonContent, Encoding.UTF8, "application/json"));
// Assert // Assert

View file

@ -19,7 +19,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers
var mockService = new Mock<IFeedbackReceiverService>(); var mockService = new Mock<IFeedbackReceiverService>();
// Test // Test
var controller = new FeedbackReceiverController(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<FeedbackReceiverDto>());
// 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.
@ -34,7 +34,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers
var mockService = new Mock<IFeedbackReceiverService>(); var mockService = new Mock<IFeedbackReceiverService>();
// Test // Test
var controller = new FeedbackReceiverController(mockService.Object, mapper, null); var controller = new FeedbackReceiversController(mockService.Object, mapper, null);
var result = await controller.Post(items); var result = await controller.Post(items);
// Assert // Assert

View file

@ -14,13 +14,13 @@ namespace Retroactiune.Controllers
{ {
[ApiController] [ApiController]
[Route("api/v1/[controller]")] [Route("api/v1/[controller]")]
public class FeedbackReceiverController : ControllerBase public class FeedbackReceiversController : ControllerBase
{ {
private readonly IOptions<ApiBehaviorOptions> _apiBehaviorOptions; private readonly IOptions<ApiBehaviorOptions> _apiBehaviorOptions;
private readonly IFeedbackReceiverService _service; private readonly IFeedbackReceiverService _service;
private readonly IMapper _mapper; private readonly IMapper _mapper;
public FeedbackReceiverController(IFeedbackReceiverService service, IMapper mapper, public FeedbackReceiversController(IFeedbackReceiverService service, IMapper mapper,
IOptions<ApiBehaviorOptions> apiBehaviorOptions) IOptions<ApiBehaviorOptions> apiBehaviorOptions)
{ {
_service = service; _service = service;
@ -64,10 +64,12 @@ namespace Retroactiune.Controllers
}); });
} }
[HttpDelete("{id}")] [HttpDelete("{guid}")]
public NoContentResult Delete(long id) public async Task<NoContentResult> Delete(
[StringLength(24, ErrorMessage = "invalid guid, must be 24 characters", MinimumLength = 24)]
string guid)
{ {
// delete feedback item. await _service.DeleteOneAsync(guid);
return NoContent(); return NoContent();
} }

View file

@ -42,7 +42,19 @@ namespace Retroactiune.Services
{ {
throw new GenericServiceException($"Operation failed: {e.Message}"); throw new GenericServiceException($"Operation failed: {e.Message}");
} }
}
public async Task DeleteOneAsync(string guid)
{
try
{
var filter = new FilterDefinitionBuilder<FeedbackReceiver>();
await _collection.DeleteOneAsync(filter.Eq(i => i.Id, guid));
}
catch (Exception e)
{
throw new GenericServiceException($"Operation failed: {e.Message}");
}
} }
} }
} }

View file

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