Skip to content

Commit 03911e1

Browse files
authored
Merge pull request #26 from contentstack/feat/CS-43224-query-in-parameter-collection
feat: ✨ adds AddQuery function to ParameterCollection
2 parents 8e10119 + 6d5bc74 commit 03911e1

File tree

6 files changed

+58
-2
lines changed

6 files changed

+58
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [v0.1.4](https://github.com/contentstack/contentstack-management-dotnet/tree/v0.1.4) (2024-01-22)
4+
5+
## [v0.1.3](https://github.com/contentstack/contentstack-management-dotnet/tree/v0.1.3) (2023-04-04)
6+
37
## [v0.1.2](https://github.com/contentstack/contentstack-management-dotnet/tree/v0.1.2) (2023-03-07)
48

59
## [v0.1.1](https://github.com/contentstack/contentstack-management-dotnet/tree/v0.1.1) (2022-12-16)

Contentstack.Management.Core.Unit.Tests/Queryable/ParameterCollectionTest.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using Contentstack.Management.Core.Queryable;
44
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using Newtonsoft.Json.Linq;
56

67
namespace Contentstack.Management.Core.Unit.Tests.Queryable
78
{
@@ -84,5 +85,18 @@ public void Should_Add_Double_List_Value_Parameter_Collection()
8485
Assert.IsInstanceOfType(collection["param1"], typeof(DoubleListParameterValue));
8586
Assert.AreEqual(vs, (collection["param1"] as DoubleListParameterValue).Value);
8687
}
88+
89+
[TestMethod]
90+
public void Should_Add_Query_JObject_In_Parameter_Collection()
91+
{
92+
ParameterCollection collection = new ParameterCollection();
93+
JObject queryObject = JObject.Parse("{ \"price_in_usd\": { \"$lt\": 600 } }");
94+
95+
collection.AddQuery(queryObject);
96+
97+
Assert.IsTrue(collection.ContainsKey("query"));
98+
Assert.IsInstanceOfType(collection["query"], typeof(StringParameterValue));
99+
Assert.AreEqual(Uri.EscapeDataString(queryObject.ToString()), (collection["query"] as StringParameterValue).Value);
100+
}
87101
}
88102
}

Contentstack.Management.Core.Unit.Tests/Utils/ContentstackUtilitiesTest.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Contentstack.Management.Core.Unit.Tests.Mokes;
44
using Contentstack.Management.Core.Utils;
55
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
using Newtonsoft.Json.Linq;
67

78
namespace Contentstack.Management.Core.Unit.Tests.Utils
89
{
@@ -89,9 +90,12 @@ public void Return_Query_Parameters_On_ParameterCollection()
8990
param.Add("limit", 10);
9091
param.Add("include", "type");
9192

93+
JObject q_obj = JObject.Parse("{ \"price_in_usd\": { \"$lt\": 600 } }");
94+
param.AddQuery(q_obj);
95+
9296
var result = ContentstackUtilities.GetQueryParameter(param);
9397

94-
Assert.AreEqual("include=type&limit=10", result);
98+
Assert.AreEqual("include=type&limit=10&query=%7B%0D%0A%20%20%22price_in_usd%22%3A%20%7B%0D%0A%20%20%20%20%22%24lt%22%3A%20600%0D%0A%20%20%7D%0D%0A%7D", result);
9599
}
96100

97101
[TestMethod]

Contentstack.Management.Core/Queryable/ParameterCollection.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Globalization;
44
using System.Linq;
55
using Contentstack.Management.Core.Exceptions;
6+
using Newtonsoft.Json.Linq;
67

78
namespace Contentstack.Management.Core.Queryable
89
{
@@ -60,6 +61,15 @@ public void Add(string key, List<double> values)
6061
Add(key, new DoubleListParameterValue(values));
6162
}
6263

64+
/// <summary>
65+
/// Adds a parameter with a queryObj value.
66+
/// </summary>
67+
/// <param name="queryObj"></param>
68+
public void AddQuery(JObject queryObj)
69+
{
70+
Add("query", new StringParameterValue(Uri.EscapeDataString(queryObj.ToString())));
71+
}
72+
6373
/// <summary>
6474
/// Converts the current parameters into a list of key-value pairs.
6575
/// </summary>
@@ -87,6 +97,9 @@ private IEnumerable<KeyValuePair<string, string>> GetParametersEnumerable()
8797
case StringParameterValue stringParameterValue:
8898
yield return new KeyValuePair<string, string>(name, stringParameterValue.Value);
8999
break;
100+
case JObjectParameterValue jObjectParameterValue:
101+
yield return new KeyValuePair<string, string>(name, jObjectParameterValue.Value.ToString());
102+
break;
90103
case StringListParameterValue stringListParameterValue:
91104
var sortedStringListParameterValue = stringListParameterValue.Value;
92105
sortedStringListParameterValue.Sort(StringComparer.Ordinal);

Contentstack.Management.Core/Queryable/QueryParamValue.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using Newtonsoft.Json.Linq;
34

45
namespace Contentstack.Management.Core.Queryable
56
{
@@ -109,4 +110,24 @@ public DoubleListParameterValue(List<double> values)
109110
Value = values;
110111
}
111112
}
113+
114+
/// <summary>
115+
/// Double list parameter value.
116+
/// </summary>
117+
public class JObjectParameterValue : QueryParamValue
118+
{
119+
/// <summary>
120+
/// List of doubles value of the parameter.
121+
/// </summary>
122+
public JObject Value { get; set; }
123+
124+
/// <summary>
125+
/// Constructs ParameterValue for a list of doubles.
126+
/// </summary>
127+
/// <param name="values"></param>
128+
public JObjectParameterValue(JObject values)
129+
{
130+
Value = values;
131+
}
132+
}
112133
}

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2012-2023 Contentstack
3+
Copyright (c) 2012-2024 Contentstack
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

0 commit comments

Comments
 (0)