diff --git a/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk.Tests/RepositoryTests/GraphSourceRepositoryTests.cs b/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk.Tests/RepositoryTests/GraphSourceRepositoryTests.cs index 8941f8a..2040f78 100644 --- a/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk.Tests/RepositoryTests/GraphSourceRepositoryTests.cs +++ b/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk.Tests/RepositoryTests/GraphSourceRepositoryTests.cs @@ -464,6 +464,53 @@ public async Task DeleteContentAsync_ThrowsNotImplementedException() mockRestClient.VerifyAll(); } + [TestMethod] + public async Task DeleteContentItemsAsync_WhenThereAreOneItemToDelete_ShouldGenerateCorrectDeleteNdJson() + { + var expectedJsonString = @"{""delete"":{""_id"":""id1"",""language_routing"":""en""}}"; + + var response = new HttpResponseMessage(HttpStatusCode.OK); + mockRestClient.Setup(c => c.HandleResponse(response)); + mockRestClient.Setup(c => c.SendAsync(It.IsAny())).Callback(req => + { + // Read the content of the request + var content = req.Content.ReadAsStringAsync().GetAwaiter().GetResult(); + + // Assert + Assert.AreEqual(expectedJsonString, content); + }).ReturnsAsync(response); + + // Act + await repository.DeleteContentItemsAsync("en", "id1"); + } + + [TestMethod] + public async Task DeleteContentItemsAsync_WhenThereAreSeveralItemToDelete_ShouldGenerateCorrectDeleteNdJson() + { + var expectedJsonStrings = new List + { + @"{""delete"":{""_id"":""id1"",""language_routing"":""en""}}", + @"{""delete"":{""_id"":""id2"",""language_routing"":""en""}}", + @"{""delete"":{""_id"":""id3"",""language_routing"":""en""}}" + }; + + var response = new HttpResponseMessage(HttpStatusCode.OK); + mockRestClient.Setup(c => c.HandleResponse(response)); + mockRestClient.Setup(c => c.SendAsync(It.IsAny())).Callback(req => + { + // Read the content of the request + var content = req.Content.ReadAsStringAsync().GetAwaiter().GetResult(); + + // Assert + var contentResultItems = content.Split(Environment.NewLine); + Assert.AreEqual(expectedJsonStrings[0], contentResultItems[0]); + Assert.AreEqual(expectedJsonStrings[1], contentResultItems[1]); + Assert.AreEqual(expectedJsonStrings[2], contentResultItems[2]); + }).ReturnsAsync(response); + + // Act + await repository.DeleteContentItemsAsync("en", "id1", "id2", "id3"); + } #region Private private string BuildExpectedTypeJsonString() diff --git a/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk/Repositories/GraphSourceRepository.cs b/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk/Repositories/GraphSourceRepository.cs index 180bea4..3e8ab16 100644 --- a/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk/Repositories/GraphSourceRepository.cs +++ b/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk/Repositories/GraphSourceRepository.cs @@ -1,9 +1,10 @@ using Optimizely.Graph.Source.Sdk.JsonConverters; -using System.Text.Json; -using System.Text; using Optimizely.Graph.Source.Sdk.RestClientHelpers; using Optimizely.Graph.Source.Sdk.SourceConfiguration; using System.Linq.Expressions; +using System.Text; +using System.Text.Json; +using static System.Runtime.InteropServices.JavaScript.JSType; namespace Optimizely.Graph.Source.Sdk.Repositories { @@ -134,6 +135,41 @@ public async Task DeleteContentAsync() public void ConfigureLink(string name, Expression> from, Expression> to) { SourceConfigurationModel.ConfigureLink(name, from, to); - } + } + + public async Task DeleteContentItemsAsync(string language, params string[] ids) + { + var serializeOptions = new JsonSerializerOptions + { + WriteIndented = false, + Converters = + { + new SourceSdkContentConverter() + } + }; + + var itemJson = string.Empty; + for(int i = 0;i - /// Adds language preference to SourceConfigurationModel. - /// - /// - void AddLanguage(string language); - - void ConfigureLink(string name, Expression> from, Expression> to); - - /// - /// Configures Content Types within the SourceConfigurationModel. - /// - /// Generic content type. - /// - SourceConfigurationModel ConfigureContentType() where T : class, new(); - - /// - /// Configures Content Property Types within the SourceConfigurationModel. - /// - /// Generic property type. - /// - SourceConfigurationModel ConfigurePropertyType() where T : class, new(); - - /// - /// Saves Content Types set in the SourceConfigurationModel to the Content Graph api. - /// - /// - Task SaveTypesAsync(); - - /// - /// Saves dynamic content sent in data array to the Content Graph api. - /// - /// - /// Id associated with content. - /// Dynamic data being saved to Content Graph. - /// - Task SaveContentAsync(Func generateId, string language, params T[] data) where T : class, new(); - - /// - /// Removes content previously stored by source. - /// - /// - Task DeleteContentAsync(); - } -} +using Optimizely.Graph.Source.Sdk.SourceConfiguration; +using System.Linq.Expressions; + +namespace Optimizely.Graph.Source.Sdk.Repositories +{ + public interface IGraphSourceRepository + { + /// + /// Adds language preference to SourceConfigurationModel. + /// + /// + void AddLanguage(string language); + + void ConfigureLink(string name, Expression> from, Expression> to); + + /// + /// Configures Content Types within the SourceConfigurationModel. + /// + /// Generic content type. + /// + SourceConfigurationModel ConfigureContentType() where T : class, new(); + + /// + /// Configures Content Property Types within the SourceConfigurationModel. + /// + /// Generic property type. + /// + SourceConfigurationModel ConfigurePropertyType() where T : class, new(); + + /// + /// Saves Content Types set in the SourceConfigurationModel to the Content Graph api. + /// + /// + Task SaveTypesAsync(); + + /// + /// Saves dynamic content sent in data array to the Content Graph api. + /// + /// + /// Id associated with content. + /// Dynamic data being saved to Content Graph. + /// + Task SaveContentAsync(Func generateId, string language, params T[] data) where T : class, new(); + + /// + /// Removes content previously stored by source. + /// + /// + Task DeleteContentAsync(); + + /// + /// Removes content previously stored by source. + /// + /// + Task DeleteContentItemsAsync(string language, params string[] ids); + } +} \ No newline at end of file