Skip to content

Commit 0d1021d

Browse files
authored
Merge pull request #15 from episerver/feature/add-delete-items-support
Support delete item
2 parents f751c98 + 1327294 commit 0d1021d

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
@@ -464,6 +464,53 @@ public async Task DeleteContentAsync_ThrowsNotImplementedException()
464464
mockRestClient.VerifyAll();
465465
}
466466

467+
[TestMethod]
468+
public async Task DeleteContentItemsAsync_WhenThereAreOneItemToDelete_ShouldGenerateCorrectDeleteNdJson()
469+
{
470+
var expectedJsonString = @"{""delete"":{""_id"":""id1"",""language_routing"":""en""}}";
471+
472+
var response = new HttpResponseMessage(HttpStatusCode.OK);
473+
mockRestClient.Setup(c => c.HandleResponse(response));
474+
mockRestClient.Setup(c => c.SendAsync(It.IsAny<HttpRequestMessage>())).Callback<HttpRequestMessage>(req =>
475+
{
476+
// Read the content of the request
477+
var content = req.Content.ReadAsStringAsync().GetAwaiter().GetResult();
478+
479+
// Assert
480+
Assert.AreEqual(expectedJsonString, content);
481+
}).ReturnsAsync(response);
482+
483+
// Act
484+
await repository.DeleteContentItemsAsync("en", "id1");
485+
}
486+
487+
[TestMethod]
488+
public async Task DeleteContentItemsAsync_WhenThereAreSeveralItemToDelete_ShouldGenerateCorrectDeleteNdJson()
489+
{
490+
var expectedJsonStrings = new List<string>
491+
{
492+
@"{""delete"":{""_id"":""id1"",""language_routing"":""en""}}",
493+
@"{""delete"":{""_id"":""id2"",""language_routing"":""en""}}",
494+
@"{""delete"":{""_id"":""id3"",""language_routing"":""en""}}"
495+
};
496+
497+
var response = new HttpResponseMessage(HttpStatusCode.OK);
498+
mockRestClient.Setup(c => c.HandleResponse(response));
499+
mockRestClient.Setup(c => c.SendAsync(It.IsAny<HttpRequestMessage>())).Callback<HttpRequestMessage>(req =>
500+
{
501+
// Read the content of the request
502+
var content = req.Content.ReadAsStringAsync().GetAwaiter().GetResult();
503+
504+
// Assert
505+
var contentResultItems = content.Split(Environment.NewLine);
506+
Assert.AreEqual(expectedJsonStrings[0], contentResultItems[0]);
507+
Assert.AreEqual(expectedJsonStrings[1], contentResultItems[1]);
508+
Assert.AreEqual(expectedJsonStrings[2], contentResultItems[2]);
509+
}).ReturnsAsync(response);
510+
511+
// Act
512+
await repository.DeleteContentItemsAsync("en", "id1", "id2", "id3");
513+
}
467514

468515
#region Private
469516
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
{
@@ -134,6 +135,41 @@ public async Task<string> DeleteContentAsync()
134135
public void ConfigureLink<T, U>(string name, Expression<Func<T, object>> from, Expression<Func<U, object>> to)
135136
{
136137
SourceConfigurationModel.ConfigureLink<T, U>(name, from, to);
137-
}
138+
}
139+
140+
public async Task<string> DeleteContentItemsAsync(string language, params string[] ids)
141+
{
142+
var serializeOptions = new JsonSerializerOptions
143+
{
144+
WriteIndented = false,
145+
Converters =
146+
{
147+
new SourceSdkContentConverter()
148+
}
149+
};
150+
151+
var itemJson = string.Empty;
152+
for(int i = 0;i<ids.Length;i++)
153+
{
154+
itemJson += $"{{\"delete\":{{\"_id\":\"{ids[i]}\",\"language_routing\":\"{language}\"}}}}";
155+
156+
if (i < ids.Length - 1)
157+
{
158+
itemJson += Environment.NewLine;
159+
}
160+
}
161+
162+
var content = new StringContent(itemJson, Encoding.UTF8, "application/json");
163+
164+
using (var requestMessage = new HttpRequestMessage(HttpMethod.Post, $"{DataUrl}?id={source}"))
165+
{
166+
requestMessage.Content = content;
167+
using (var responseMessage = await client.SendAsync(requestMessage))
168+
{
169+
await client.HandleResponse(responseMessage);
170+
}
171+
}
172+
return string.Empty;
173+
}
138174
}
139175
}
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, string language, 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, string language, 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)