Skip to content

Commit 5cf0efb

Browse files
Support delete item
Support delete item
1 parent 150a18d commit 5cf0efb

File tree

3 files changed

+143
-54
lines changed

3 files changed

+143
-54
lines changed

Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk.Tests/RepositoryTests/GraphSourceRepositoryTests.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,53 @@ public async Task DeleteContentAsync_ThrowsNotImplementedException()
416416
mockRestClient.VerifyAll();
417417
}
418418

419+
[TestMethod]
420+
public async Task DeleteContentItemsAsync_WhenThereAreOneItemToDelete_ShouldGenerateCorrectDeleteNdJson()
421+
{
422+
var expectedJsonString = @"{""delete"":{""_id"":""id1"",""language_routing"":""en""}}";
423+
424+
var response = new HttpResponseMessage(HttpStatusCode.OK);
425+
mockRestClient.Setup(c => c.HandleResponse(response));
426+
mockRestClient.Setup(c => c.SendAsync(It.IsAny<HttpRequestMessage>())).Callback<HttpRequestMessage>(req =>
427+
{
428+
// Read the content of the request
429+
var content = req.Content.ReadAsStringAsync().GetAwaiter().GetResult();
430+
431+
// Assert
432+
Assert.AreEqual(expectedJsonString, content);
433+
}).ReturnsAsync(response);
434+
435+
// Act
436+
await repository.DeleteContentItemsAsync("en", "id1");
437+
}
438+
439+
[TestMethod]
440+
public async Task DeleteContentItemsAsync_WhenThereAreSeveralItemToDelete_ShouldGenerateCorrectDeleteNdJson()
441+
{
442+
var expectedJsonStrings = new List<string>
443+
{
444+
@"{""delete"":{""_id"":""id1"",""language_routing"":""en""}}",
445+
@"{""delete"":{""_id"":""id2"",""language_routing"":""en""}}",
446+
@"{""delete"":{""_id"":""id3"",""language_routing"":""en""}}"
447+
};
448+
449+
var response = new HttpResponseMessage(HttpStatusCode.OK);
450+
mockRestClient.Setup(c => c.HandleResponse(response));
451+
mockRestClient.Setup(c => c.SendAsync(It.IsAny<HttpRequestMessage>())).Callback<HttpRequestMessage>(req =>
452+
{
453+
// Read the content of the request
454+
var content = req.Content.ReadAsStringAsync().GetAwaiter().GetResult();
455+
456+
// Assert
457+
var contentResultItems = content.Split(Environment.NewLine);
458+
Assert.AreEqual(expectedJsonStrings[0], contentResultItems[0]);
459+
Assert.AreEqual(expectedJsonStrings[1], contentResultItems[1]);
460+
Assert.AreEqual(expectedJsonStrings[2], contentResultItems[2]);
461+
}).ReturnsAsync(response);
462+
463+
// Act
464+
await repository.DeleteContentItemsAsync("en", "id1", "id2", "id3");
465+
}
419466

420467
#region Private
421468
private string BuildExpectedTypeJsonString()

Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk/Repositories/GraphSourceRepository.cs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using Optimizely.Graph.Source.Sdk.JsonConverters;
2-
using System.Text.Json;
3-
using System.Text;
42
using Optimizely.Graph.Source.Sdk.RestClientHelpers;
53
using Optimizely.Graph.Source.Sdk.SourceConfiguration;
64
using System.Linq.Expressions;
5+
using System.Text;
6+
using System.Text.Json;
7+
using static System.Runtime.InteropServices.JavaScript.JSType;
78

89
namespace Optimizely.Graph.Source.Sdk.Repositories
910
{
@@ -139,6 +140,41 @@ public async Task<string> DeleteContentAsync()
139140
public void ConfigureLink<T, U>(string name, Expression<Func<T, object>> from, Expression<Func<U, object>> to)
140141
{
141142
SourceConfigurationModel.ConfigureLink<T, U>(name, from, to);
142-
}
143+
}
144+
145+
public async Task<string> DeleteContentItemsAsync(string language, params string[] ids)
146+
{
147+
var serializeOptions = new JsonSerializerOptions
148+
{
149+
WriteIndented = false,
150+
Converters =
151+
{
152+
new SourceSdkContentConverter()
153+
}
154+
};
155+
156+
var itemJson = string.Empty;
157+
for(int i = 0;i<ids.Length;i++)
158+
{
159+
itemJson += $"{{\"delete\":{{\"_id\":\"{ids[i]}\",\"language_routing\":\"{language}\"}}}}";
160+
161+
if (i < ids.Length - 1)
162+
{
163+
itemJson += Environment.NewLine;
164+
}
165+
}
166+
167+
var content = new StringContent(itemJson, Encoding.UTF8, "application/json");
168+
169+
using (var requestMessage = new HttpRequestMessage(HttpMethod.Post, $"{DataUrl}?id={source}"))
170+
{
171+
requestMessage.Content = content;
172+
using (var responseMessage = await client.SendAsync(requestMessage))
173+
{
174+
await client.HandleResponse(responseMessage);
175+
}
176+
}
177+
return string.Empty;
178+
}
143179
}
144180
}
Lines changed: 57 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,57 @@
1-
using Optimizely.Graph.Source.Sdk.SourceConfiguration;
2-
using System.Linq.Expressions;
3-
4-
namespace Optimizely.Graph.Source.Sdk.Repositories
5-
{
6-
public interface IGraphSourceRepository
7-
{
8-
/// <summary>
9-
/// Adds language preference to SourceConfigurationModel.
10-
/// </summary>
11-
/// <param name="language"></param>
12-
void AddLanguage(string language);
13-
14-
void ConfigureLink<T, U>(string name, Expression<Func<T, object>> from, Expression<Func<U, object>> to);
15-
16-
/// <summary>
17-
/// Configures Content Types within the SourceConfigurationModel.
18-
/// </summary>
19-
/// <typeparam name="T">Generic content type.</typeparam>
20-
/// <returns></returns>
21-
SourceConfigurationModel<T> ConfigureContentType<T>() where T : class, new();
22-
23-
/// <summary>
24-
/// Configures Content Property Types within the SourceConfigurationModel.
25-
/// </summary>
26-
/// <typeparam name="T">Generic property type.</typeparam>
27-
/// <returns></returns>
28-
SourceConfigurationModel<T> ConfigurePropertyType<T>() where T : class, new();
29-
30-
/// <summary>
31-
/// Saves Content Types set in the SourceConfigurationModel to the Content Graph api.
32-
/// </summary>
33-
/// <returns></returns>
34-
Task<string> SaveTypesAsync();
35-
36-
/// <summary>
37-
/// Saves dynamic content sent in data array to the Content Graph api.
38-
/// </summary>
39-
/// <typeparam name="T"></typeparam>
40-
/// <param name="generateId">Id associated with content.</param>
41-
/// <param name="data">Dynamic data being saved to Content Graph.</param>
42-
/// <returns></returns>
43-
Task<string> SaveContentAsync<T>(Func<T, string> generateId, params T[] data) where T : class, new();
44-
45-
/// <summary>
46-
/// Removes content previously stored by source.
47-
/// </summary>
48-
/// <returns></returns>
49-
Task<string> DeleteContentAsync();
50-
}
51-
}
1+
using Optimizely.Graph.Source.Sdk.SourceConfiguration;
2+
using System.Linq.Expressions;
3+
4+
namespace Optimizely.Graph.Source.Sdk.Repositories
5+
{
6+
public interface IGraphSourceRepository
7+
{
8+
/// <summary>
9+
/// Adds language preference to SourceConfigurationModel.
10+
/// </summary>
11+
/// <param name="language"></param>
12+
void AddLanguage(string language);
13+
14+
void ConfigureLink<T, U>(string name, Expression<Func<T, object>> from, Expression<Func<U, object>> to);
15+
16+
/// <summary>
17+
/// Configures Content Types within the SourceConfigurationModel.
18+
/// </summary>
19+
/// <typeparam name="T">Generic content type.</typeparam>
20+
/// <returns></returns>
21+
SourceConfigurationModel<T> ConfigureContentType<T>() where T : class, new();
22+
23+
/// <summary>
24+
/// Configures Content Property Types within the SourceConfigurationModel.
25+
/// </summary>
26+
/// <typeparam name="T">Generic property type.</typeparam>
27+
/// <returns></returns>
28+
SourceConfigurationModel<T> ConfigurePropertyType<T>() where T : class, new();
29+
30+
/// <summary>
31+
/// Saves Content Types set in the SourceConfigurationModel to the Content Graph api.
32+
/// </summary>
33+
/// <returns></returns>
34+
Task<string> SaveTypesAsync();
35+
36+
/// <summary>
37+
/// Saves dynamic content sent in data array to the Content Graph api.
38+
/// </summary>
39+
/// <typeparam name="T"></typeparam>
40+
/// <param name="generateId">Id associated with content.</param>
41+
/// <param name="data">Dynamic data being saved to Content Graph.</param>
42+
/// <returns></returns>
43+
Task<string> SaveContentAsync<T>(Func<T, string> generateId, params T[] data) where T : class, new();
44+
45+
/// <summary>
46+
/// Removes content previously stored by source.
47+
/// </summary>
48+
/// <returns></returns>
49+
Task<string> DeleteContentAsync();
50+
51+
/// <summary>
52+
/// Removes content previously stored by source.
53+
/// </summary>
54+
/// <returns></returns>
55+
Task<string> DeleteContentItemsAsync(string language, params string[] ids);
56+
}
57+
}

0 commit comments

Comments
 (0)