Implement DeleteMany in FeedbackReceiversController

This commit is contained in:
Denis-Cosmin Nutiu 2021-06-20 14:25:04 +03:00
parent 097381a75a
commit 2c53668f63
5 changed files with 62 additions and 14 deletions

View file

@ -59,7 +59,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers
// Assert // Assert
Assert.IsType<NoContentResult>(result); Assert.IsType<NoContentResult>(result);
mockService.Verify(s => s.DeleteOneAsync("bad_guid_but_unit_test_works_cause_validation_doesnt"), mockService.Verify(s => s.DeleteManyAsync(new[] {"bad_guid_but_unit_test_works_cause_validation_doesnt"}),
Times.Once); Times.Once);
} }
@ -70,7 +70,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers
var mapper = TestUtils.GetMapper(); var mapper = TestUtils.GetMapper();
var mockService = new Mock<IFeedbackReceiverService>(); var mockService = new Mock<IFeedbackReceiverService>();
mockService.Setup(i => i.FindAsync(It.IsAny<IEnumerable<string>>(), null, null)) mockService.Setup(i => i.FindAsync(It.IsAny<IEnumerable<string>>(), null, null))
.ReturnsAsync(new [] {new FeedbackReceiver()}); .ReturnsAsync(new[] {new FeedbackReceiver()});
// Test // Test
var controller = new FeedbackReceiversController(mockService.Object, mapper, null); var controller = new FeedbackReceiversController(mockService.Object, mapper, null);
@ -78,7 +78,8 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers
// Assert // Assert
Assert.IsType<OkObjectResult>(result); Assert.IsType<OkObjectResult>(result);
mockService.Verify(s => s.FindAsync(new[] {"bad_guid_but_unit_test_works_cause_validation_doesnt"}, null, null), mockService.Verify(
s => s.FindAsync(new[] {"bad_guid_but_unit_test_works_cause_validation_doesnt"}, null, null),
Times.Once); Times.Once);
} }
@ -95,7 +96,8 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers
// Assert // Assert
Assert.IsType<NotFoundObjectResult>(result); Assert.IsType<NotFoundObjectResult>(result);
mockService.Verify(s => s.FindAsync(new[] {"bad_guid_but_unit_test_works_cause_validation_doesnt"}, null, null), mockService.Verify(
s => s.FindAsync(new[] {"bad_guid_but_unit_test_works_cause_validation_doesnt"}, null, null),
Times.Once); Times.Once);
} }

View file

@ -132,7 +132,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Services
} }
[Fact] [Fact]
public async Task Test_DeleteOneAsync_Ok() public async Task Test_DeleteManyAsync_Ok()
{ {
// Arrange // Arrange
var mongoDatabaseMock = new Mock<IMongoDatabase>(); var mongoDatabaseMock = new Mock<IMongoDatabase>();
@ -153,7 +153,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Services
// Test // Test
var service = new FeedbackReceiverService(mongoClientMock.Object, mongoSettingsMock.Object); var service = new FeedbackReceiverService(mongoClientMock.Object, mongoSettingsMock.Object);
await service.DeleteOneAsync("insert_guid_here"); await service.DeleteManyAsync(new []{"insert_guid_here"});
// Assert // Assert
mongoClientMock.Verify(i => i.GetDatabase("MyDB", null), Times.Once()); mongoClientMock.Verify(i => i.GetDatabase("MyDB", null), Times.Once());
@ -162,7 +162,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Services
Times.Once()); Times.Once());
mongoCollectionMock.Verify( mongoCollectionMock.Verify(
i => i.DeleteOneAsync(It.IsAny<FilterDefinition<FeedbackReceiver>>(), It.IsAny<CancellationToken>()), i => i.DeleteManyAsync(It.IsAny<FilterDefinition<FeedbackReceiver>>(), It.IsAny<CancellationToken>()),
Times.Once()); Times.Once());
} }

View file

@ -70,8 +70,8 @@ namespace Retroactiune.Controllers
/// </summary> /// </summary>
/// <param name="guid">The guid of the item to be deleted.</param> /// <param name="guid">The guid of the item to be deleted.</param>
/// <returns>A NoContent result.</returns> /// <returns>A NoContent result.</returns>
/// <response code="204">The delete is successful.</response> /// <response code="204">The delete is submitted.</response>
/// <response code="400">The delete is unsuccessful.</response> /// <response code="400">The request is invalid.</response>
[HttpDelete("{guid}")] [HttpDelete("{guid}")]
[ProducesResponseType(typeof(NoContentResult), StatusCodes.Status204NoContent)] [ProducesResponseType(typeof(NoContentResult), StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status400BadRequest)]
@ -79,7 +79,7 @@ namespace Retroactiune.Controllers
[StringLength(24, ErrorMessage = "invalid guid, must be 24 characters", MinimumLength = 24)] [StringLength(24, ErrorMessage = "invalid guid, must be 24 characters", MinimumLength = 24)]
string guid) string guid)
{ {
await _service.DeleteOneAsync(guid); await _service.DeleteManyAsync(new [] {guid});
return NoContent(); return NoContent();
} }
@ -132,5 +132,32 @@ namespace Retroactiune.Controllers
{ {
return Ok(await _service.FindAsync(filter, offset, limit)); return Ok(await _service.FindAsync(filter, offset, limit));
} }
/// <summary>
/// Deletes FeedbackReceiver identified by ids.
/// </summary>
/// <param name="ids">A list of FeedbackReceiver ids.</param>
/// <response code="204">The request to delete the items has been submitted.</response>
/// <response code="404">The request is invalid.</response>
/// <returns></returns>
[HttpDelete]
[ProducesResponseType(typeof(NoContentResult), StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(BasicResponse),StatusCodes.Status400BadRequest)]
public async Task<IActionResult> DeleteTokens([Required] IEnumerable<string> ids)
{
// TODO: Unit test, integration test.
try
{
await _service.DeleteManyAsync(ids);
return NoContent();
}
catch (GenericServiceException e)
{
return BadRequest(new BasicResponse
{
Message = e.Message
});
}
}
} }
} }

View file

@ -45,12 +45,12 @@ namespace Retroactiune.Services
} }
} }
public async Task DeleteOneAsync(string guid) public async Task DeleteManyAsync(IEnumerable<string> guids)
{ {
try try
{ {
var filter = new FilterDefinitionBuilder<FeedbackReceiver>(); var filter = new FilterDefinitionBuilder<FeedbackReceiver>();
await _collection.DeleteOneAsync(filter.Eq(i => i.Id, guid)); await _collection.DeleteManyAsync(filter.In(i => i.Id, guids));
} }
catch (Exception e) catch (Exception e)
{ {

View file

@ -6,8 +6,27 @@ namespace Retroactiune.Services
{ {
public interface IFeedbackReceiverService public interface IFeedbackReceiverService
{ {
/// <summary>
/// Creates feedback receivers.
/// </summary>
/// <param name="items">A collection of feedback receivers.</param>
/// <returns>Nothing.</returns>
public Task CreateManyAsync(IEnumerable<FeedbackReceiver> items); public Task CreateManyAsync(IEnumerable<FeedbackReceiver> items);
public Task DeleteOneAsync(string guid);
/// <summary>
/// Deletes one FeedbackReceiver.
/// </summary>
/// <param name="guids">A list of FeedbackReceiver guids to delete.</param>
/// <returns>Nothing.</returns>
public Task DeleteManyAsync(IEnumerable<string> guids);
/// <summary>
/// Finds FeedbackReceivers.
/// </summary>
/// <param name="guid">A list of guids to filter the FeedbackReceivers. </param>
/// <param name="offset">An offset, it skips the specified FeedbackReceivers.</param>
/// <param name="limit">A limit for the returned results.</param>
/// <returns>A collection of FeedbackReceivers.</returns>
Task<IEnumerable<FeedbackReceiver>> FindAsync(IEnumerable<string> guid, int? offset = null, int? limit = null); Task<IEnumerable<FeedbackReceiver>> FindAsync(IEnumerable<string> guid, int? offset = null, int? limit = null);
} }
} }