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]
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<FeedbackReceiverDto> {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<FeedbackReceiverDto> {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

View file

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

View file

@ -14,13 +14,13 @@ namespace Retroactiune.Controllers
{
[ApiController]
[Route("api/v1/[controller]")]
public class FeedbackReceiverController : ControllerBase
public class FeedbackReceiversController : ControllerBase
{
private readonly IOptions<ApiBehaviorOptions> _apiBehaviorOptions;
private readonly IFeedbackReceiverService _service;
private readonly IMapper _mapper;
public FeedbackReceiverController(IFeedbackReceiverService service, IMapper mapper,
public FeedbackReceiversController(IFeedbackReceiverService service, IMapper mapper,
IOptions<ApiBehaviorOptions> apiBehaviorOptions)
{
_service = service;
@ -64,10 +64,12 @@ namespace Retroactiune.Controllers
});
}
[HttpDelete("{id}")]
public NoContentResult Delete(long id)
[HttpDelete("{guid}")]
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();
}

View file

@ -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<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 Task CreateManyAsync(IEnumerable<FeedbackReceiver> items);
public Task DeleteOneAsync(string guid);
}
}