Reputation: 822
I need to return a success status and message when return data in case of success and error type error message if there is error or no data found .
This is controller code :
public HttpResponseMessage GetPatResult(int Patid,int branchid)
{
using (DBEntities1 entities = new DBEntities1())
{
var entity = entities.LAB_RESULTS_CLINIC_VIEW.Where(e => e.Patient_No == Patid && e.branchid==branchid).ToList();
if (!entity.Any())
{
return Request.CreateResponse(HttpStatusCode.NotFound, "No Results Found ");
}
else
{
return Request.CreateResponse(HttpStatusCode.OK, entity);
}
}
}
The output :
[{"order_number":2000000013,"Test_Id":46,"Test_Name":"Lymphocytes%","Result_Duration":24,"Culture":2,"Normal":"Yes","Report_Date":"2020-11-15T00:00:00","Result":"26","Notes":null,"Low_Range":"24","High_Range":"44","Panic":"","Text_Range":"","machine_name":"Sysmex XN-330","Customer_No":1,"Customer_Name":"Cash Price List","Patient_No":10,"Patient_Name":"ziad adnan","Clinic_File_No":"","Category":"Adult","AGE":25,"SEX":"Male ","Test_Note":"","UNIT":"%","DEPTID":1,"Collection_Date":"2020-11-15T00:00:00","Receiving_Date":"2020-11-15T00:00:00","Group_Name":"CBC WITH DIFFERENTIAL COUNT","SERIAL":12,"GROUPID":2,"packageid":0,"EXAMINED_BY":"ziad","APPROVED_BY":"ziad","UPDATED_BY":null,"UPDATED_DATE":null,"Comments":"","department_name":"Hematology Unit","branchid":2},
{"order_number":2000000013,"Test_Id":47,"Test_Name":"Neutrophils","Result_Duration":24,"Culture":2,"Normal":"Yes","Report_Date":"2020-11-15T00:00:00","Result":"50","Notes":null,"Low_Range":"35","High_Range":"66","Panic":"","Text_Range":"","machine_name":"Sysmex XN-330","Customer_No":1,"Customer_Name":"Cash Price List","Patient_No":10,"Patient_Name":"ziad adnan","Clinic_File_No":"","Category":"Adult","AGE":25,"SEX":"Male ","Test_Note":"","UNIT":"%","DEPTID":1,"Collection_Date":"2020-11-15T00:00:00","Receiving_Date":"2020-11-15T00:00:00","Group_Name":"CBC WITH DIFFERENTIAL COUNT","SERIAL":11,"GROUPID":2,"packageid":0,"EXAMINED_BY":"ziad","APPROVED_BY":"ziad","UPDATED_BY":null,"UPDATED_DATE":null,"Comments":"","department_name":"Hematology Unit","branchid":2}]
The output which I need as the following :
In case of success return "success": true, "data":[] "message": "Data Found" like this example:
{
"success": true,
"data": [
{
"order_number": 2000000013,
"Test_Id": 46,
"Test_Name": "Lymphocytes%",
"Result_Duration": 24,
"Culture": 2,
"Normal": "Yes",
"Report_Date": "2020-11-15T00:00:00",
"Result": "26",
"Notes": null,
"Low_Range": "24",
"High_Range": "44",
"Panic": "",
"Text_Range": "",
"machine_name": "Sysmex XN-330",
"Customer_No": 1,
"Customer_Name": "Cash Price List",
"Patient_No": 10,
"Patient_Name": "ziad adnan",
"Clinic_File_No": "",
"Category": "Adult",
"AGE": 25,
"SEX": "Male ",
"Test_Note": "",
"UNIT": "%",
"DEPTID": 1,
"Collection_Date": "2020-11-15T00:00:00",
"Receiving_Date": "2020-11-15T00:00:00",
"Group_Name": "CBC WITH DIFFERENTIAL COUNT",
"SERIAL": 12,
"GROUPID": 2,
"packageid": 0,
"EXAMINED_BY": "ziad",
"APPROVED_BY": "ziad",
"UPDATED_BY": null,
"UPDATED_DATE": null,
"Comments": "",
"department_name": "Hematology Unit",
"branchid": 2
},
{
"order_number": 2000000007,
"Test_Id": 1117,
"Test_Name": "ZINC",
"Result_Duration": 24,
"Culture": 2,
"Normal": "Yes",
"Report_Date": "2020-11-08T00:00:00",
"Result": "80",
"Notes": null,
"Low_Range": "60",
"High_Range": "130",
"Panic": "",
"Text_Range": "",
"machine_name": "Access 2",
"Customer_No": 1,
"Customer_Name": "Cash Price List",
"Patient_No": 10,
"Patient_Name": "ziad adnan",
"Clinic_File_No": "",
"Category": "Adult",
"AGE": 25,
"SEX": "Male ",
"Test_Note": "",
"UNIT": "ug/dl",
"DEPTID": 2,
"Collection_Date": "2020-11-07T00:00:00",
"Receiving_Date": "2020-11-07T00:00:00",
"Group_Name": null,
"SERIAL": 1,
"GROUPID": 0,
"packageid": 6329,
"EXAMINED_BY": "ziad",
"APPROVED_BY": "ziad",
"UPDATED_BY": null,
"UPDATED_DATE": null,
"Comments": "",
"department_name": "Clinical Chemistry Unit",
"branchid": 2
}
],
"message": "Data Found "
}
In case no data found or error return :
{
"success": False,
"error": [ "error code and type"],
"message": "error message"
}
How can I do that please and thank you .
Upvotes: 1
Views: 1785
Reputation: 1796
Define a result model like this:
public class PatResult
{
public bool Success { get; set; }
public string Message { get; set; }
public List<Entity> Entities { get; set; }
}
Then in the controller depending on the result f. e.
PatResult patResult = new PatResult
{
Success = true,
Message = "Data Found",
Entities = entities
};
return Request.CreateResponse(HttpStatusCode.OK, patResult);
Upvotes: 3