2
\$\begingroup\$

I have an HTTP endpoint for returning data directly from Azure Cosmos DB. The endpoint is basically a database access point (this seems like a common case). Since I'm returning the data unmodified, there's no need to deserialize it, even though I have an object for Project on the server that is used elsewhere. I'm wondering if there are any ways to make this code more efficient, or tighter.

[Route("")]
public async Task<IHttpActionResult> GetProjects()
{
    await ValidateUser();
    var cosmosDb = await Storage.GetCosmosDb();
    var queryDefinition = new QueryDefinition($"SELECT * FROM projects project WHERE project.organization = '{CurrentUser.Organization}'");
    var projectsQuery = cosmosDb.Containers[typeof(Project)]
        .GetItemQueryStreamIterator(queryDefinition);
    var projects = new JArray();
    while (projectsQuery.HasMoreResults)
    {
        var responseMessage = await projectsQuery.ReadNextAsync();
        if (!responseMessage.IsSuccessStatusCode)
        {
            return InternalServerError(new Exception(
                $"Query item from stream failed. Status code: {responseMessage.StatusCode} Message: {responseMessage.ErrorMessage}"));
        }

        using (var streamReader = new StreamReader(responseMessage.Content))
        using (var jsonTextReader = new JsonTextReader(streamReader))
        {
            var project = JObject.Load(jsonTextReader);
            ((JArray)project["Documents"]).ForEach(projects.Add);
        }
    }

    return Ok(projects);
}
\$\endgroup\$

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.