2
\$\begingroup\$

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?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.