Skip to content

Instantly share code, notes, and snippets.

@Layoric
Last active June 2, 2021 06:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Layoric/522d867ba7064d0732544de79088f746 to your computer and use it in GitHub Desktop.
Save Layoric/522d867ba7064d0732544de79088f746 to your computer and use it in GitHub Desktop.
autoquery-custom
// Connect your database
container.AddSingleton<IDbConnectionFactory>(c =>
new OrmLiteConnectionFactory(MapProjectPath("~/northwind.sqlite"), SqliteDialect.Provider));
// Add the AutoQuery Plugin
Plugins.Add(new AutoQueryFeature { MaxLimit = 100 });
// Return CustomRockstar result to control what the service returns
// Join with other tables
[Route("/rockstar-albums")]
public class QueryRockstarAlbums
: QueryDb<Rockstar,CustomRockstar>, IJoin<Rockstar,RockstarAlbum>
{
public int? Age { get; set; }
public string RockstarAlbumName { get; set; }
}
// Custom result
public class CustomRockstar
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int? Age { get; set; }
// Comes from joined table
public string RockstarAlbumName { get; set; }
}
// Override with custom implementation
public class MyQueryServices : Service
{
public IAutoQueryDb AutoQuery { get; set; }
public async Task<object> Any(QueryRockstarAlbums query)
{
using var db = AutoQuery.GetDb(query, base.Request);
var q = AutoQuery.CreateQuery(query, base.Request, db);
return await AutoQuery
.ExecuteAsync(query, q, base.Request, db);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment