Moeez
Moeez

Reputation: 478

Changing the response type of WEB API

I have created a WEB API using MySQL DB. The API is working well. The code is below

try
        {
            var before = dt.AddMinutes(-5);
            var after = dt.AddMinutes(5);

            var result = medEntitites.tj_xhqd
                         .Where(m =>
                         m.zdjh == msn &&
                         m.sjsj >= before &&
                         m.sjsj <= after).Select(m => new { MSN = m.zdjh, DateTime = m.sjsj, Signal_Strength = m.xhqd }).Distinct();


            return Request.CreateResponse(HttpStatusCode.OK, new { data = result });
        }
        catch (Exception ex)
        {
            return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
        }

and the response is below

{
"data": [
    {
        "MSN": "002999000076",
        "DateTime": "2017-10-11T10:16:48",
        "Signal_Strength": "15"
    },
    {
        "MSN": "002999000076",
        "DateTime": "2017-10-11T10:19:02",
        "Signal_Strength": "15"
    },
    {
        "MSN": "002999000076",
        "DateTime": "2017-10-11T10:20:58",
        "Signal_Strength": "16"
    },
    {
        "MSN": "002999000076",
        "DateTime": "2017-10-11T10:22:54",
        "Signal_Strength": "15"
    }
]}

How's it working?

I am passing a meter serial number and a date time. The API receives both of them. +- The minutes in time and display all the records withing that time.

Now I want is that, If at a particular time there exist any record then the API will send me YES in response and if no record is found then it will send me NO.

The sample response should be

{
    "data": [
        {
         "Response":   "YES"/"NO"
        }
    ]
}  

I have searched a solution for it but couldn't able to find one

Any help would be highly appreciated.

Upvotes: 0

Views: 296

Answers (2)

Richard Deeming
Richard Deeming

Reputation: 31198

Something like this should work:

var before = dt.AddMinutes(-5);
var after = dt.AddMinutes(5);

bool anyRecords = medEntitites.tj_xhqd.Any(m =>
     m.zdjh == msn &&
     m.sjsj >= before &&
     m.sjsj <= after);

return Request.CreateResponse(HttpStatusCode.OK, new 
{ 
    data = new[]
    {
        new { Response = anyRecords ? "YES" : "NO" }
    }
});

The response seems a little excessive to me. Why do you need a single-element array of objects just to return a single flag?

Upvotes: 0

Isham Mohamed
Isham Mohamed

Reputation: 2789

Create the below class

public class ResponseChecker
{
    public string Response{get; set;};

    public ResponseChecker(IEnumerable result)
    {
        if(result.ToList().Count > 0)
            Response = "YES";
        else
            Response = "NO";
    }
}

Modify your code as below

try
{
    var before = dt.AddMinutes(-5);
    var after = dt.AddMinutes(5);

    var result = medEntitites.tj_xhqd
                 .Where(m =>
                 m.zdjh == msn &&
                 m.sjsj >= before &&
                 m.sjsj <= after).Select(m => new { MSN = m.zdjh, DateTime = m.sjsj, Signal_Strength = m.xhqd }).Distinct();


    return Request.CreateResponse(HttpStatusCode.OK, new { data = new ResponseChecker(result) });
}
catch (Exception ex)
{
    return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
}

Upvotes: 1

Related Questions