Complex array in ServiceStack request - c#

I am sending the following request parameters to my service; among which, is the filter parameter which is a multidimensional array:
filter[0][field]:homeCountry
filter[0][data][type]:string
filter[0][data][value]:united s
page:2
start:200
limit:200
sort:homeCountry
dir:ASC
The querystring is encoded like so:
paymentratetrip.json?filter%5B0%5D%5Bfield%5D=homeCountry&filter%5B0%5D%5Bdata%5D%5Btype%5D=string&filter%5B0%5D%5Bdata%5D%5Bvalue%5D=united%20s&page=2&start=200&limit=200&sort=homeCountry&dir=AS
Currently, my C# request object looks like this:
public class PaymentRateTripRequest
{
public int start { get; set; }
public int limit { get; set; }
public string sort { get; set; }
public string dir { get; set; }
}
How can I modify my request object to receive the filter parameter which could be a multidimensional array?
Note: I am using ServiceStack.
The only way I can think is to send the entire request object as a parameter to my method like so:
public object Get(PaymentRateTripRequest req)
{
return _repository.GetAllRates(req.start, req.limit, req.sort, req.dir, this.Request.OriginalRequest);
}
But, this doesn't seem like the best solution.
Edit: this.Request.QueryString
this.Request.QueryString
{filter%5b0%5d%5bfield%5d=homeCountry&filter%5b0%5d%5bdata%5d%5btype%5d=string&filter%5b0%5d%5bdata%5d%5bvalue%5d=united+s&page=2&start=200&limit=200&sort=homeCountry&dir=ASC}
[System.Web.HttpValueCollection]: {filter%5b0%5d%5bfield%5d=homeCountry&filter%5b0%5d%5bdata%5d%5btype%5d=string&filter%5b0%5d%5bdata%5d%5bvalue%5d=united+s&page=2&start=200&limit=200&sort=homeCountry&dir=ASC}
base {System.Collections.Specialized.NameObjectCollectionBase}: {filter%5b0%5d%5bfield%5d=homeCountry&filter%5b0%5d%5bdata%5d%5btype%5d=string&filter%5b0%5d%5bdata%5d%5bvalue%5d=united+s&page=2&start=200&limit=200&sort=homeCountry&dir=ASC}
_all: null
_allKeys: {string[8]}
AllKeys: {string[8]}
Edit: filter is still empty.

This is an alternative solution that requires no changes to your client and therefore will accept the query string in the format you have currently:
paymentratetrip.json?filter%5B0%5D%5Bfield%5D=homeCountry&filter%5B0%5D%5Bdata%5D%5Btype%5D=string&filter%5B0%5D%5Bdata%5D%5Bvalue%5D=united%20s&page=2&start=200&limit=200&sort=homeCountry&dir=AS
The disadvantage of this method is that it's more code to maintain. The JSV method is simpler.
Request attribute to populate the filter from the querystring:
We can use a ServiceStack filter to intercept the query string before it reaches the action method. It can then parse the custom filter format and populate the filter object of the DTO.
public class FilterAttribute : Attribute, IHasRequestFilter
{
IHasRequestFilter IHasRequestFilter.Copy()
{
return this;
}
public int Priority { get { return int.MinValue; } }
FilterField CreateOrUpdateField(ref Dictionary<string, FilterField> filter, string id)
{
if(filter.ContainsKey(id))
return filter[id];
var field = new FilterField { Data = new Dictionary<string, object>() };
filter.Add(id, field);
return field;
}
public void RequestFilter(IRequest req, IResponse res, object requestDto)
{
var filteredDto = requestDto as IFilter;
if(filteredDto == null)
return;
const string fieldPattern = #"filter\[([A-Za-z0-9]+)\]\[field\]";
const string dataPattern = #"filter\[([A-Za-z0-9]+)\]\[data\]\[([A-Za-z0-9]+)\]";
Dictionary<string, FilterField> filter = new Dictionary<string, FilterField>();
foreach(var property in req.QueryString.AllKeys)
{
Match match = Regex.Match(property, fieldPattern, RegexOptions.IgnoreCase);
if(match.Success)
{
// Field
var id = match.Groups[1].Value;
var field = CreateOrUpdateField(ref filter, id);
field.Field = req.QueryString[property];
} else {
match = Regex.Match(property, dataPattern, RegexOptions.IgnoreCase);
if(match.Success)
{
// Data value
var id = match.Groups[1].Value;
var keyName = match.Groups[2].Value;
var field = CreateOrUpdateField(ref filter, id);
if(!field.Data.ContainsKey(keyName))
field.Data.Add(keyName, req.QueryString[property]);
}
}
}
filteredDto.Filter = filter.Values.ToArray();
}
}
You will also need to add this interface and FilterField class:
public class FilterField
{
public string Field { get; set; }
public Dictionary<string,object> Data { get; set; }
}
public interface IFilter
{
FilterField[] filter { get; set; }
}
Then you simply need to update your DTO so it looks like this:
[Route("/paymentratetrip", "GET"]
[Filter]
public class PaymentRateTripRequest : IFilter
{
public int page { get; set; }
public int start { get; set; }
public int limit { get; set; }
public string sort { get; set; }
public string dir { get; set; }
public FilterField[] filter { get; set; }
}

You should add a property with the filter to your DTO, such as below:
public class PaymentRateTripRequest
{
public int page { get; set; }
public int start { get; set; }
public int limit { get; set; }
public string sort { get; set; }
public string dir { get; set; }
public FilterField[] filter { get; set; }
}
public class FilterField
{
public string field { get; set; }
public Dictionary<string,object> data { get; set; }
}
This will allow you to add any number of fields to filter by, and by making the data property of the FilterField a Dictionary<string, object> you can add as many data properties as needed.
Then you can populate the filter parameter in your PaymentRateTripRequest using JSV format. You can learn about JSV format here. JSV Format (i.e. JSON-like Separated Values) is a JSON inspired format that uses CSV-style escaping for the least overhead and optimal performance.
paymentratetrip.json?filter=[{field:homeCountry,data:{type:string,value:"united s"}},{field:other,data:{type:int,value:34,special:true}}]&page=2&start=200&limit=200&sort=homeCountry&dir=ASC
Then you can access the filter as a regular property on your request.
Hope this helps.

Related

Map jQuery datatable search param to property of class in c#

I am using .net core clean architecture along with jQuery datatable. Server-side search is enabled, but I cannot map that search param search[value] from datable to a model property in c#. I have tried the Newtonsoft JsonPropertyName attribute to map it but it fails. Below is my model code:
public class GetVotesByMeetingIdQuery : IRequest<PaginatedList<VoteCastDTO>>
{
public int PageNumber { get; set; } = 1;
public int PageSize { get; set; } = 10;
public Search Search { get; set; }
}
public class Search
{
[JsonProperty(PropertyName = "value")]
public string Value { set; get; }
[JsonProperty(PropertyName = "regex")]
public bool Regex { set; get; }
}
I am able to capture the param from the request in my controller.
[HttpGet("GetVotesByMeetingId")]
public async Task<ActionResult<PaginatedList<VoteCastDTO>>> GetVotesByMeetingId([FromQuery] GetVotesByMeetingIdQuery query)
{
var exist = Request.Query.TryGetValue("search[value]", out Microsoft.Extensions.Primitives.StringValues val);
query.Search = exist ? val.ToString() : string.Empty;
return await Mediator.Send(query);
}
but I don't want to do this as I want to keep my controller clean. Is there anyway to sort out this issue?
You can use [FromQuery] attribute on the property. It will map the parameter to the property accordingly. Also, you need to change the property type to string as you are getting the param value in string. Below is the example:
public class GetVotesByMeetingIdQuery : IRequest<PaginatedList<VoteCastDTO>>
{
public int PageNumber { get; set; } = 1;
public int PageSize { get; set; } = 10;
[FromQuery(Name = "search[value]")]
public string Search { get; set; }
}

Serializing Response Inside A Class As A List

I'm trying to fetch Symptoms from an API. I can fetch them already and I'm serializing it inside a class succesfully. The result looks like in image that i share at below:
There are just IDs and Names inside them. The second table from API is like that:
So here my Entity Class;
using System.Collections.Generic;
public class SymptomousInBodySublocations
{
public int ID { get; set; }
public string Name { get; set; }
public bool HasRedFlag { get; set; }
public ICollection<BodyLocations> HealthSymptomLocationIDs { get; set; }
public string ProfName { get; set; }
public List<string> Synonyms { get; set; }
}
And my Serialize Method:
public static List<SymptomousInBodySublocations> SymptomsInBodySublocations()
{
var client = new RestClient("https://priaid-symptom-checker-v1.p.rapidapi.com/symptoms/31/man?format=json&language=en-gb");
var request = new RestRequest(Method.GET);
request.AddHeader("x-rapidapi-host", "priaid-symptom-checker-v1.p.rapidapi.com");
request.AddHeader("x-rapidapi-key", "<api-key>");
List<SymptomousInBodySublocations> SymptomsInBodySublocationsList = new List<SymptomousInBodySublocations>();
var response = client.Execute<List<SymptomousInBodySublocations>>(request);
foreach(SymptomousInBodySublocations variables in response.Data)
{
SymptomsInBodySublocationsList.Add(variables);
}
return SymptomsInBodySublocationsList;
}
And my BodyLocations Class:
public class BodyLocations
{
public int ID { get; set; }
public string Name { get; set; }
}
In this point when i tried to fetch my data inside my List<BodyLocations>() the response.Data is empty. What should i do?
HealthSymptomLocationIDs isn't an object, looking at the response it is an array of integers.
Changing the field to match the response should populate the field with the integer values from the API
public List<int> HealthSymptomLocationIDs { get; set; }

Consume Data From this endpoint

I am getting tdata from a certain endpoint and the problem id on serialization to my classes. I want to cast the bellow data to my class but cant get how the class should be structured. Check out the data .....
{
"-LYG_AI_oGYjNBrzMlKF": {
"chatDispayText": "",
"chatId": "-LYG_AI_oGYjNBrzMlKF",
"chatName": "",
"chattype": "single",
"imageUrl": "https://wallpaper.wiki/wp-content/uploads/2017/04/wallpaper.wiki-Amazing-celebrities-hd-wallpaper-PIC-WPD004734.jpg",
"lastMessageSent": "aiye",
"lastMessageSentTime": 1549704416263,
"synched": false,
"users": {
"-LYG_AIZ5MvTbjR7DACe": "Uicpm3L15TX0c15pKCI6KUEARyB3",
"-LYG_AI_oGYjNBrzMlKE": "Xsr0z9lsqNOEytX61lJvaGz1A8F2"
}
}
}
If the data you get out the endpoint has a dynamic structure, you can make use of a key-vale pair collection or a dictionary. For instance:
JObject jObject = JObject.Parse(Data); // This would already give you a key-value pair collection
Dictionary<String,Object> collection = new Dictionary<String, Object>();
foreach(var obj in jObject){
collection.Add(obj.Key, obj.Value);
}
However, this isn't a strongly typed approach which means that it is not effective in the majority of scenarios. A better solution when dealing with endpoints would be to define a class with fixed schema, actually something you need in your code, and then map the class to the object yielded by the endpoint using a metadata struct. For example:
public class ChatInfoModel
{
[JsonProperty(Metadata.ChatId)]
public long ChatId { get; set; }
[JsonProperty(Metadata.ChatId, Required = Required.AllowNull)]
public String Message { get; set; }
}
public struct Metadata
{
public const String ChatId = "userChatId";
public const String Message = "messageTxt";
}
And then
var deserializedObject = JsonConvert.DeserializeObject<ChatInfoModel>(data);
However, if your class has the exact same naming convention (but should not necessarily follow the camelCase naming convention) for its properties as in the serialized data, the JsonProperty attribute would not be needed.
You can also deserialize the object without using JsonProperty attribute manually using the first approach, and it is actually advantageous in certain scenarios where your schema comes from a configuration file rather than a struct.
Take inspiration from the Structure below:
public class Rootobject
{
public LYG_AI_Ogyjnbrzmlkf LYG_AI_oGYjNBrzMlKF { get; set; }
}
public class LYG_AI_Ogyjnbrzmlkf
{
public string chatDispayText { get; set; }
public string chatId { get; set; }
public string chatName { get; set; }
public string chattype { get; set; }
public string imageUrl { get; set; }
public string lastMessageSent { get; set; }
public long lastMessageSentTime { get; set; }
public bool synched { get; set; }
public Users users { get; set; }
}
public class Users
{
public string LYG_AIZ5MvTbjR7DACe { get; set; }
public string LYG_AI_oGYjNBrzMlKE { get; set; }
}

How to convert Generic.List values into extension method

I am using Linq to Entity , Linq query returning a ToList() and storing multiple rows in DataSourcesModel class. Later i am using foreach to iterate items and store values in DataSourcesModelDTO, till here working fine.
Now question is DataSourcesModelDTO dto storing only one row value.
I am expecting it should store multiple row value. How to achieve this in extension method?
** var datasoruceModeltDTO = DataSourcesDTOTransformers.ToDTO(result);**
public static class DataSourcesDTOTransformers
{
public static DataSourcesModelDTO ToDTO(this List<DataSourcesModel> model)
{
if (model == null) { return null; }
var dto = new DataSourcesModelDTO();
ToDTO(model, dto);
return dto;
}
public static void ToDTO(List<DataSourcesModel> model1, DataSourcesModelDTO dto)
{
foreach (var model in model1)
{
dto.DataSourceConfigID = model.DataSourceConfigID.ToString();
dto.DataSourceID = model.DataSourceID.ToString();
dto.Name = model.Name;
dto.server = model.server;
dto.LastModified = model.LastModified;
dto.Instance = model.Instance;
}
}
}
[DataContract]
public class DataSourcesModelDTO
{
[DataMember]
public string DataSourceID { get; set; }
[DataMember]
public string DataSourceConfigID { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string server { get; set; }
[DataMember]
public string Instance { get; set; }
[DataMember]
public bool Deleted { get; set; }
[DataMember]
public DateTime LastModified { get; set; }
[DataMember]
public DateTime LastSync { get; set; }
}
foreach (var model in model1)
{
dto.DataSourceConfigID = model.DataSourceConfigID.ToString();
You are setting the properties of the same dto object every time in the loop. I would rearrange your toDTO methiod to either:
take in a single DataSourcesModel object and return a single DataSourcesModelDTO object (calling the method from within a loop or Linq query), or
take in an IEnumerable<DataSourcesModel> and return an IEnumerable<DataSourcesModelDTO>
How to achieve this in extension method?
Not sure why you need an extension method, but the appropriate signature would be:
public static IEnumerable<DataSourcesModelDTO> ToDTOCollection (this IEnumerable<DataSourcesModel> model)
I'll let you work out what the implementation would be. Hint: You could create a List<DataSourcesModelDTO> within the method and populate it with new objects the same way you are now.

JSON Response Parsing in WP7

I have a JSON Response from web api as this
{"payload":{"items":{"11204":{"title":"The Ugliest Girl?","item_id":"11204","thumb_url":"http:google.11204.jpg","teaser":"We live in the internet generationher purpose in life to her through this adversity.","language_id":"en","media_id":1,"views":"5","shares":"0"},"11228":{"title":"Depressed","item_id":"11228","thumb_url":"http:google.11228.jpg","teaser":"We all get discouraged at times, especially when things go wrong or other people hurt us. Sometimes we can seem to go through a string of disappointments that compound our sadness till we wonder.","language_id":"en","media_id":5,"views":"35","shares":"2"}}
and many more objects in similar manner
How can i parse this to Dictionary or in any other way? The response varies depending on the request.
u can parse your json into an object like :
var parsed = JObject.Parse(Json);
and to get a specific value :
var value = parsed[key];
Using a service like json2csharp.com you can convert your json to C#. Given the json that you have, you will need to modify the classes slightly. Here are consumable classes
public class Item
{
public string title { get; set; }
public string item_id { get; set; }
public string thumb_url { get; set; }
public string teaser { get; set; }
public string language_id { get; set; }
public int media_id { get; set; }
public string views { get; set; }
public string shares { get; set; }
}
public class Payload
{
public ICollection<Item> Items { get; set; }
}
From there you can use a library like Json.Net to convert from the json to these objects. Normally you would be able to convert directly to your classes, but because of the indexes in the names, this is not possible. So you will have to do some conversion yourself.
public Payload ConvertJson(string json)
{
var payload = new Payload();
var container = JToken.Parse(json) as JContainer;
if(container == null) return payload;
payload.Items = new List<Item>(container.Count);
foreach (var child in container)
{
var childJson = child.FirstOrDefault();
if (childJson == null) continue;
var item = childJson.ToObject<Item>();
if (item.item_id == 0)
{
item.item_id = Convert.ToInt32(((JProperty)child).Name);
}
payload.Items.Add(item);
}
return payload;
}

Categories