Skip to content

Commit

Permalink
Added WriteJson extention method to write raw SQL queries to HttpContext
Browse files Browse the repository at this point in the history
  • Loading branch information
kwestground authored and oskardudycz committed Nov 22, 2023
1 parent 38f7226 commit 0e5facc
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Marten.AspNetCore/QueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,32 @@ public static async Task WriteArray<TDoc, TOut>(
stream.Position = 0;
await stream.CopyToAsync(context.Response.Body, context.RequestAborted).ConfigureAwait(false);
}

/// <summary>
/// Write an raw SQL query result directly to the HttpContext
/// </summary>
/// <param name="session"></param>
/// <param name="sql"></param>
/// <param name="context"></param>
/// <param name="contentType"></param>
/// <param name="onFoundStatus">Defaults to 200</param>
/// <returns></returns>
public static async Task WriteJson(
this IQuerySession session,
string sql,
HttpContext context,
string contentType = "application/json",
int onFoundStatus = 200
)
{
var stream = new MemoryStream();
_ = await session.StreamJson<int>(stream, context.RequestAborted, sql).ConfigureAwait(false);

context.Response.StatusCode = onFoundStatus;
context.Response.ContentLength = stream.Length;
context.Response.ContentType = contentType;

stream.Position = 0;
await stream.CopyToAsync(context.Response.Body, context.RequestAborted).ConfigureAwait(false);
}
}

0 comments on commit 0e5facc

Please sign in to comment.