Home > Back-end >  return without brackets when sending a get request
return without brackets when sending a get request

Time:10-11

My api returns this following json:

{
    "requestInfo": [
        {
            "requestId": 118170,
            "firstRead": "1905-06-24T00:00:00",
            "requestAnswerSent": "2021-09-09T00:00:00",
            "currentMilage": "13131",
            "damageOccured": "1753-01-01T00:00:00",
            "milageDamageOccured": 0,
            "latestService": "1753-01-01T00:00:00",
            "latestServiceKm": 0,
            "contractID": 00000,
            "requestRecieved": "2021-10-08T16:18:58.237",
            "requestStatus": "NEW_REQUEST",
            "claiminfo": [
                {
                    "claimId": 387498,
                    "name": "Claim",
                    "answerSent": "2021-09-09T00:00:00"
                },
                {
                    "claimId": 387499,
                    "name": "Rent Replacement Car",
                    "answerSent": "2021-09-09T00:00:00"
                },
                {
                    "claimId": 387500,
                    "name": "Other Work",
                    "answerSent": "2021-09-09T00:00:00"
                }
            ]
        }
    ]
}

And i would like to return this without it being wrapped in brackets. I can't use any of the FOR JSON WITHOUT WRAPPER since i return a list as you can see in "claiminfo" but I would still like to return without it being wrapped in { }. And again due to a list being returned i can't use anything like SingleOrDefault either. Here's the code (method, models and controller)

public class RequestInfo
    {
        //public int claimId { get; set; } = 0;
        public int requestId { get; set; } = 0;
        public DateTime firstRead { get; set; } = DateTime.Parse("1753-01-01");
        public DateTime requestAnswerSent { get; set; } = DateTime.Parse("2021-09-09");
        public string currentMilage { get; set; } = "";
        public DateTime damageOccured { get; set; } = DateTime.Parse("1753-01-01");
        public int milageDamageOccured { get; set; } = 0;
        public DateTime latestService { get; set; } = DateTime.Parse("1753-01-01");
        public int latestServiceKm { get; set; } = 0;
        public int contractID { get; set; } = 0;
        public DateTime requestRecieved { get; set; } = DateTime.Parse("1753-01-01");
        public string requestStatus { get; set; } = "";
        public IEnumerable<ClaimInfo> claiminfo { get; set; }
    }

    public class ClaimInfo
    {
        public int claimId { get; set; } = 0;
        public string name { get; set; } = "";
        public DateTime answerSent { get; set; } = DateTime.Parse("2021-09-09");
    }

    public class RequestInformation
    {
        public IEnumerable<RequestInfo> requestInfo { get; set; }
    }

Here's the controller:

[HttpGet]
        public async Task<IActionResult> getRequestInfo([FromQuery] RequestInfo model)
        {

        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
        try
        {
            var list = await _request.getRequestInfo(model);
            return Ok(list);
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
        }
        return Ok();
    }

And finally the method:

// request info card 
public async Task<RequestInformation> getRequestInfo(RequestInfo model)
{
    var parameters = new DynamicParameters();
    parameters.Add("@requestId", model.requestId);
    var getAllRequest = await _sqlconnection.QueryAsync<RequestInfo>($@"SELECT TOP 1
    Request.ID as requestId, 
    FirstRead as firstRead,
    CurrentKilometerreading as currentMilage, 
    DamageDate as damageOccured, DamageKilometerreading as milageDamageOccured,
    LatestServiceDate as latestService, LatestServiceKilometer as latestServiceKm,
    ClaimHandlingStatus.Name as requestStatus, Request.CreatedDate as requestRecieved, 
    Contract.ID as contractID
    FROM Request 
    INNER JOIN RequestCrossClaim ON Request.ID = RequestCrossClaim.RequestID
    INNER JOIN Claim ON RequestCrossClaim.ClamID = Claim.ID
    INNER JOIN Contract ON Request.ContractID = Contract.ID
    INNER JOIN ClaimHandlingStatus ON Request.ClaimHandlingStatusID = 
    ClaimHandlingStatus.ID
    WHERE Request.ID = @requestId", parameters);


    var getClaimsForRequet = await _sqlconnection.QueryAsync<ClaimInfo>($@"
    SELECT RequestType.Name, AnswerSent, Claim.ID as claimId
    FROM Request
    INNER JOIN RequestCrossClaim ON Request.ID = RequestCrossClaim.RequestID
    INNER JOIN Claim ON RequestCrossClaim.ClamID = Claim.ID
    INNER JOIN RequestType ON RequestTypeID = RequestType.ID
    WHERE Request.ID = @requestId", parameters);

    var claims = getClaimsForRequet;
    var request = new RequestInformation()
    {
        requestInfo = getAllRequest
    };

    foreach (var r in request.requestInfo)
    {
        r.claiminfo = claims.OrderBy(s => s.claimId);
    }

    return request;
}

Thankful for any help.

CodePudding user response:

In getRequestInfo try to return OK(list.requestInfo) instead of OK(list). Meaning, extract the object requestInfo from the result (list).

  • Related