Looping JSON to get matching values from Database

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
I have a need to take in a payload that looks like this:
"Registry":[
"Token":"@!!!@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","ProcessId":"1",
"Token":"@!!!@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","ProcessId":"2",
"Token":"@!!!@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","ProcessId":"3"
]
Read each record and remove the "@!!!@" and then search the database for the value (represented by xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx). This is web api 2.0. The return payload should look like this:
"Registry": [
"ProcessId": "1",
"Code": 200,
"Value": "Test",
"Token": "@!!!@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
,
"ProcessId": "2",
"Code": 404,
"Message": "Could not resolve token.",
"Token": "@!!!@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
,
"ProcessId": "3",
"Code": 200,
"Message": "Test2",
"Token": "@!!!@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
]
If a "process id" is provided return that same number with the return record if not provided return a unique process id "counter".
I have a service that is handling this on an exposed API controller and it seems to work rather quickly but I feel as if it is written poorly. This is what I have so far:
[Route("get")]
[HttpPost]
public async Task<HttpResponseMessage> GetPIIRegistry([FromBody] JObject registryArry)
ReturnCodes.UsedCodes = new List<int>();
JArray registryArray = null;
registryArray = (JArray)registryArry["Registry"];
var payloadTokens = registryArray.Values("Token");
int processIdCounter = 1;
HttpResponseMessage response;
List<string> values = new List<string>();
foreach (var item in payloadTokens)
values.Add(item.ToString().Contains("@!!!@") ? item.ToString().Replace("@!!!@", "") : item.ToString());
List<REGISTRY> returnVals = new List<REGISTRY>();
using (var dataContext = new registryEntries())
dataContext.Configuration.AutoDetectChangesEnabled = false;
var query =
from registries in dataContext.REGISTRies
where values.Contains(registries.Token)
select registries;
returnVals = query.ToList();
JArray returnObjects = new JArray();
foreach (JObject registryObj in registryArray)
var returnObj = new JObject();
var resultSet = returnVals.Where("Token.Equals(@0)", registryObj["Token"].ToString().Replace("@!!!@",""));
if (resultSet.Count() > 0)
foreach (var item in resultSet)
returnObj = new JObject();
if(registryObj["ProcessId"] != null)
if (string.IsNullOrEmpty(registryObj["ProcessId"].ToString()))
returnObj["ProcessId"] = processIdCounter.ToString();
else
returnObj["ProcessId"] = registryObj["ProcessId"];
else
returnObj["ProcessId"] = processIdCounter.ToString();
if (item.Remote == 0)
returnObj["Code"] = ReturnCodes.OK;
returnObj["Value"] = item.Value;
returnObj["Token"] = "@!!!@" + item.Token;
returnObj["Remote"] = item.Remote;
if(!ReturnCodes.UsedCodes.Contains(ReturnCodes.OK)) ReturnCodes.UsedCodes.Add(ReturnCodes.OK);
else
//remote get not enabled
returnObj["Code"] = ReturnCodes.NotFound;
returnObj["Message"] = "Remote service not implemented. No matching values found.";
returnObj["Token"] = "@!!!@"+item.Token;
if (!ReturnCodes.UsedCodes.Contains(ReturnCodes.NotFound)) ReturnCodes.UsedCodes.Add(ReturnCodes.NotFound);
returnObjects.Add(returnObj);
else
if (registryObj["ProcessId"] != null)
if (string.IsNullOrEmpty(registryObj["ProcessId"].ToString()))
returnObj["ProcessId"] = processIdCounter.ToString();
else
returnObj["ProcessId"] = registryObj["ProcessId"];
else
returnObj["ProcessId"] = processIdCounter.ToString();
returnObj["Code"] = ReturnCodes.NotFound;
returnObj["Message"] = "Provided token produced no matches.";
returnObj["Token"] = "@!!!@"+registryObj["Token"].ToString().Replace("@!!!@","");
returnObjects.Add(returnObj);
if (!ReturnCodes.UsedCodes.Contains(ReturnCodes.NotFound)) ReturnCodes.UsedCodes.Add(ReturnCodes.NotFound);
processIdCounter++;
var returns = returnObjects.ToString();
returns = "Registry:" + returns + "";
var json = JObject.Parse(returns);
if (ReturnCodes.UsedCodes.Count() > 1)
response = Request.CreateResponse((HttpStatusCode)ReturnCodes.MultiStatus, json);
else
try
response = Request.CreateResponse((HttpStatusCode)200, json);
catch
response = Request.CreateResponse((HttpStatusCode)ReturnCodes.Error, json);
return response;
Any thoughts on how to clean this up?
json asp.net-web-api json.net
add a comment |Â
up vote
2
down vote
favorite
I have a need to take in a payload that looks like this:
"Registry":[
"Token":"@!!!@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","ProcessId":"1",
"Token":"@!!!@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","ProcessId":"2",
"Token":"@!!!@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","ProcessId":"3"
]
Read each record and remove the "@!!!@" and then search the database for the value (represented by xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx). This is web api 2.0. The return payload should look like this:
"Registry": [
"ProcessId": "1",
"Code": 200,
"Value": "Test",
"Token": "@!!!@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
,
"ProcessId": "2",
"Code": 404,
"Message": "Could not resolve token.",
"Token": "@!!!@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
,
"ProcessId": "3",
"Code": 200,
"Message": "Test2",
"Token": "@!!!@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
]
If a "process id" is provided return that same number with the return record if not provided return a unique process id "counter".
I have a service that is handling this on an exposed API controller and it seems to work rather quickly but I feel as if it is written poorly. This is what I have so far:
[Route("get")]
[HttpPost]
public async Task<HttpResponseMessage> GetPIIRegistry([FromBody] JObject registryArry)
ReturnCodes.UsedCodes = new List<int>();
JArray registryArray = null;
registryArray = (JArray)registryArry["Registry"];
var payloadTokens = registryArray.Values("Token");
int processIdCounter = 1;
HttpResponseMessage response;
List<string> values = new List<string>();
foreach (var item in payloadTokens)
values.Add(item.ToString().Contains("@!!!@") ? item.ToString().Replace("@!!!@", "") : item.ToString());
List<REGISTRY> returnVals = new List<REGISTRY>();
using (var dataContext = new registryEntries())
dataContext.Configuration.AutoDetectChangesEnabled = false;
var query =
from registries in dataContext.REGISTRies
where values.Contains(registries.Token)
select registries;
returnVals = query.ToList();
JArray returnObjects = new JArray();
foreach (JObject registryObj in registryArray)
var returnObj = new JObject();
var resultSet = returnVals.Where("Token.Equals(@0)", registryObj["Token"].ToString().Replace("@!!!@",""));
if (resultSet.Count() > 0)
foreach (var item in resultSet)
returnObj = new JObject();
if(registryObj["ProcessId"] != null)
if (string.IsNullOrEmpty(registryObj["ProcessId"].ToString()))
returnObj["ProcessId"] = processIdCounter.ToString();
else
returnObj["ProcessId"] = registryObj["ProcessId"];
else
returnObj["ProcessId"] = processIdCounter.ToString();
if (item.Remote == 0)
returnObj["Code"] = ReturnCodes.OK;
returnObj["Value"] = item.Value;
returnObj["Token"] = "@!!!@" + item.Token;
returnObj["Remote"] = item.Remote;
if(!ReturnCodes.UsedCodes.Contains(ReturnCodes.OK)) ReturnCodes.UsedCodes.Add(ReturnCodes.OK);
else
//remote get not enabled
returnObj["Code"] = ReturnCodes.NotFound;
returnObj["Message"] = "Remote service not implemented. No matching values found.";
returnObj["Token"] = "@!!!@"+item.Token;
if (!ReturnCodes.UsedCodes.Contains(ReturnCodes.NotFound)) ReturnCodes.UsedCodes.Add(ReturnCodes.NotFound);
returnObjects.Add(returnObj);
else
if (registryObj["ProcessId"] != null)
if (string.IsNullOrEmpty(registryObj["ProcessId"].ToString()))
returnObj["ProcessId"] = processIdCounter.ToString();
else
returnObj["ProcessId"] = registryObj["ProcessId"];
else
returnObj["ProcessId"] = processIdCounter.ToString();
returnObj["Code"] = ReturnCodes.NotFound;
returnObj["Message"] = "Provided token produced no matches.";
returnObj["Token"] = "@!!!@"+registryObj["Token"].ToString().Replace("@!!!@","");
returnObjects.Add(returnObj);
if (!ReturnCodes.UsedCodes.Contains(ReturnCodes.NotFound)) ReturnCodes.UsedCodes.Add(ReturnCodes.NotFound);
processIdCounter++;
var returns = returnObjects.ToString();
returns = "Registry:" + returns + "";
var json = JObject.Parse(returns);
if (ReturnCodes.UsedCodes.Count() > 1)
response = Request.CreateResponse((HttpStatusCode)ReturnCodes.MultiStatus, json);
else
try
response = Request.CreateResponse((HttpStatusCode)200, json);
catch
response = Request.CreateResponse((HttpStatusCode)ReturnCodes.Error, json);
return response;
Any thoughts on how to clean this up?
json asp.net-web-api json.net
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a need to take in a payload that looks like this:
"Registry":[
"Token":"@!!!@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","ProcessId":"1",
"Token":"@!!!@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","ProcessId":"2",
"Token":"@!!!@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","ProcessId":"3"
]
Read each record and remove the "@!!!@" and then search the database for the value (represented by xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx). This is web api 2.0. The return payload should look like this:
"Registry": [
"ProcessId": "1",
"Code": 200,
"Value": "Test",
"Token": "@!!!@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
,
"ProcessId": "2",
"Code": 404,
"Message": "Could not resolve token.",
"Token": "@!!!@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
,
"ProcessId": "3",
"Code": 200,
"Message": "Test2",
"Token": "@!!!@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
]
If a "process id" is provided return that same number with the return record if not provided return a unique process id "counter".
I have a service that is handling this on an exposed API controller and it seems to work rather quickly but I feel as if it is written poorly. This is what I have so far:
[Route("get")]
[HttpPost]
public async Task<HttpResponseMessage> GetPIIRegistry([FromBody] JObject registryArry)
ReturnCodes.UsedCodes = new List<int>();
JArray registryArray = null;
registryArray = (JArray)registryArry["Registry"];
var payloadTokens = registryArray.Values("Token");
int processIdCounter = 1;
HttpResponseMessage response;
List<string> values = new List<string>();
foreach (var item in payloadTokens)
values.Add(item.ToString().Contains("@!!!@") ? item.ToString().Replace("@!!!@", "") : item.ToString());
List<REGISTRY> returnVals = new List<REGISTRY>();
using (var dataContext = new registryEntries())
dataContext.Configuration.AutoDetectChangesEnabled = false;
var query =
from registries in dataContext.REGISTRies
where values.Contains(registries.Token)
select registries;
returnVals = query.ToList();
JArray returnObjects = new JArray();
foreach (JObject registryObj in registryArray)
var returnObj = new JObject();
var resultSet = returnVals.Where("Token.Equals(@0)", registryObj["Token"].ToString().Replace("@!!!@",""));
if (resultSet.Count() > 0)
foreach (var item in resultSet)
returnObj = new JObject();
if(registryObj["ProcessId"] != null)
if (string.IsNullOrEmpty(registryObj["ProcessId"].ToString()))
returnObj["ProcessId"] = processIdCounter.ToString();
else
returnObj["ProcessId"] = registryObj["ProcessId"];
else
returnObj["ProcessId"] = processIdCounter.ToString();
if (item.Remote == 0)
returnObj["Code"] = ReturnCodes.OK;
returnObj["Value"] = item.Value;
returnObj["Token"] = "@!!!@" + item.Token;
returnObj["Remote"] = item.Remote;
if(!ReturnCodes.UsedCodes.Contains(ReturnCodes.OK)) ReturnCodes.UsedCodes.Add(ReturnCodes.OK);
else
//remote get not enabled
returnObj["Code"] = ReturnCodes.NotFound;
returnObj["Message"] = "Remote service not implemented. No matching values found.";
returnObj["Token"] = "@!!!@"+item.Token;
if (!ReturnCodes.UsedCodes.Contains(ReturnCodes.NotFound)) ReturnCodes.UsedCodes.Add(ReturnCodes.NotFound);
returnObjects.Add(returnObj);
else
if (registryObj["ProcessId"] != null)
if (string.IsNullOrEmpty(registryObj["ProcessId"].ToString()))
returnObj["ProcessId"] = processIdCounter.ToString();
else
returnObj["ProcessId"] = registryObj["ProcessId"];
else
returnObj["ProcessId"] = processIdCounter.ToString();
returnObj["Code"] = ReturnCodes.NotFound;
returnObj["Message"] = "Provided token produced no matches.";
returnObj["Token"] = "@!!!@"+registryObj["Token"].ToString().Replace("@!!!@","");
returnObjects.Add(returnObj);
if (!ReturnCodes.UsedCodes.Contains(ReturnCodes.NotFound)) ReturnCodes.UsedCodes.Add(ReturnCodes.NotFound);
processIdCounter++;
var returns = returnObjects.ToString();
returns = "Registry:" + returns + "";
var json = JObject.Parse(returns);
if (ReturnCodes.UsedCodes.Count() > 1)
response = Request.CreateResponse((HttpStatusCode)ReturnCodes.MultiStatus, json);
else
try
response = Request.CreateResponse((HttpStatusCode)200, json);
catch
response = Request.CreateResponse((HttpStatusCode)ReturnCodes.Error, json);
return response;
Any thoughts on how to clean this up?
json asp.net-web-api json.net
I have a need to take in a payload that looks like this:
"Registry":[
"Token":"@!!!@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","ProcessId":"1",
"Token":"@!!!@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","ProcessId":"2",
"Token":"@!!!@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","ProcessId":"3"
]
Read each record and remove the "@!!!@" and then search the database for the value (represented by xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx). This is web api 2.0. The return payload should look like this:
"Registry": [
"ProcessId": "1",
"Code": 200,
"Value": "Test",
"Token": "@!!!@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
,
"ProcessId": "2",
"Code": 404,
"Message": "Could not resolve token.",
"Token": "@!!!@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
,
"ProcessId": "3",
"Code": 200,
"Message": "Test2",
"Token": "@!!!@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
]
If a "process id" is provided return that same number with the return record if not provided return a unique process id "counter".
I have a service that is handling this on an exposed API controller and it seems to work rather quickly but I feel as if it is written poorly. This is what I have so far:
[Route("get")]
[HttpPost]
public async Task<HttpResponseMessage> GetPIIRegistry([FromBody] JObject registryArry)
ReturnCodes.UsedCodes = new List<int>();
JArray registryArray = null;
registryArray = (JArray)registryArry["Registry"];
var payloadTokens = registryArray.Values("Token");
int processIdCounter = 1;
HttpResponseMessage response;
List<string> values = new List<string>();
foreach (var item in payloadTokens)
values.Add(item.ToString().Contains("@!!!@") ? item.ToString().Replace("@!!!@", "") : item.ToString());
List<REGISTRY> returnVals = new List<REGISTRY>();
using (var dataContext = new registryEntries())
dataContext.Configuration.AutoDetectChangesEnabled = false;
var query =
from registries in dataContext.REGISTRies
where values.Contains(registries.Token)
select registries;
returnVals = query.ToList();
JArray returnObjects = new JArray();
foreach (JObject registryObj in registryArray)
var returnObj = new JObject();
var resultSet = returnVals.Where("Token.Equals(@0)", registryObj["Token"].ToString().Replace("@!!!@",""));
if (resultSet.Count() > 0)
foreach (var item in resultSet)
returnObj = new JObject();
if(registryObj["ProcessId"] != null)
if (string.IsNullOrEmpty(registryObj["ProcessId"].ToString()))
returnObj["ProcessId"] = processIdCounter.ToString();
else
returnObj["ProcessId"] = registryObj["ProcessId"];
else
returnObj["ProcessId"] = processIdCounter.ToString();
if (item.Remote == 0)
returnObj["Code"] = ReturnCodes.OK;
returnObj["Value"] = item.Value;
returnObj["Token"] = "@!!!@" + item.Token;
returnObj["Remote"] = item.Remote;
if(!ReturnCodes.UsedCodes.Contains(ReturnCodes.OK)) ReturnCodes.UsedCodes.Add(ReturnCodes.OK);
else
//remote get not enabled
returnObj["Code"] = ReturnCodes.NotFound;
returnObj["Message"] = "Remote service not implemented. No matching values found.";
returnObj["Token"] = "@!!!@"+item.Token;
if (!ReturnCodes.UsedCodes.Contains(ReturnCodes.NotFound)) ReturnCodes.UsedCodes.Add(ReturnCodes.NotFound);
returnObjects.Add(returnObj);
else
if (registryObj["ProcessId"] != null)
if (string.IsNullOrEmpty(registryObj["ProcessId"].ToString()))
returnObj["ProcessId"] = processIdCounter.ToString();
else
returnObj["ProcessId"] = registryObj["ProcessId"];
else
returnObj["ProcessId"] = processIdCounter.ToString();
returnObj["Code"] = ReturnCodes.NotFound;
returnObj["Message"] = "Provided token produced no matches.";
returnObj["Token"] = "@!!!@"+registryObj["Token"].ToString().Replace("@!!!@","");
returnObjects.Add(returnObj);
if (!ReturnCodes.UsedCodes.Contains(ReturnCodes.NotFound)) ReturnCodes.UsedCodes.Add(ReturnCodes.NotFound);
processIdCounter++;
var returns = returnObjects.ToString();
returns = "Registry:" + returns + "";
var json = JObject.Parse(returns);
if (ReturnCodes.UsedCodes.Count() > 1)
response = Request.CreateResponse((HttpStatusCode)ReturnCodes.MultiStatus, json);
else
try
response = Request.CreateResponse((HttpStatusCode)200, json);
catch
response = Request.CreateResponse((HttpStatusCode)ReturnCodes.Error, json);
return response;
Any thoughts on how to clean this up?
json asp.net-web-api json.net
asked Aug 2 at 19:13
VinnyGuitara
12911
12911
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f200851%2flooping-json-to-get-matching-values-from-database%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password