Last active
May 26, 2021 08:44
-
-
Save tejainece/cb71e71ba21954e1a0a478f9f5c6a0e2 to your computer and use it in GitHub Desktop.
Examples on Jaguar website
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:jaguar/jaguar.dart'; | |
import 'package:jaguar_auth/jaguar_auth.dart'; | |
import 'package:jaguar_example_session_models/jaguar_example_session_models.dart'; | |
main() async { | |
final server = Jaguar(port: 10000); | |
// Register user fetcher | |
server.userFetchers[User] = DummyUserFetcher(users); | |
server.postJson( | |
'/login', | |
// Authentication | |
(Context ctx) async => await BasicAuth.authenticate<User>(ctx), | |
); | |
server.getJson( | |
'/books', | |
(Context ctx) => books.values.toList(), | |
before: [Authorizer<User>()], // Authorization | |
); | |
await server.serve(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:jaguar/jaguar.dart'; | |
import 'package:jaguar_example_session_models/jaguar_example_session_models.dart'; | |
@GenController(path: '/books') | |
class BookRoutes extends Controller { | |
@GetJson() | |
List<Book> getAll(Context ctx) => books.values.toList(); | |
@GetJson(path: '/:id') | |
Book get(Context ctx) => books[ctx.pathParams['id']]; | |
@Post() | |
Future<void> create(Context ctx) async { | |
Book book = await ctx.bodyAsJson(convert: Book.fromMap); | |
books[book.id] = book; | |
} | |
@Delete(path: '/:id') | |
void delete(Context ctx) => books.remove(ctx.pathParams['id']); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:jaguar/jaguar.dart'; | |
import 'package:jaguar_session_jwt/jaguar_session_jwt.dart'; | |
/// JWT config used by [JwtSession] | |
const jwtConfig = const JwtConfig('sdgdflgujsdgndsflkgjsdlnwertwert78676', | |
issuer: 'jaguar.com'); | |
main() async { | |
final server = Jaguar( | |
sessionManager: JwtSession(jwtConfig), // Use JWT Session Manager | |
); | |
server.get('/session', (ctx) async { | |
Session session = await ctx.session; | |
String userId = session['user']; // Reading session data | |
session['user'] = '5'; // Updating session data | |
}); | |
await server.serve(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Bean logic is generated by Jaguar ORM | |
@GenBean() | |
class UserBean extends Bean<User> with _UserBean { | |
UserBean(Adapter adapter) : super(adapter); | |
} | |
/// The adapter | |
final PgAdapter _adapter = | |
PgAdapter('example', username: 'postgres', password: 'dart_jaguar'); | |
main() async { | |
final bean = UserBean(_adapter); // Create bean | |
await bean.drop(); // Drop old tables | |
await bean.createTable(); // Create new tables | |
// Insert a new record | |
await bean.insert(User(id: '1', name: 'teja', age: 29)); | |
// Fetching record by primary key | |
User user = await bean.find('1'); | |
// Get all | |
List<User> users = await bean.getAll(); | |
// Find with expression | |
await bean.findWhere((UserBean user) => (user.age > 20) & (user.age < 30)); | |
// Updating a record | |
user.name = 'teja hackborn'; | |
await bean.update(user); | |
// Remove | |
await bean.remove('1'); | |
// Remove all | |
await bean.removeAll(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:jaguar/jaguar.dart'; | |
main() async { | |
final server = Jaguar(); | |
server.get('/session', (ctx) async { | |
// Get the session | |
Session session = await ctx.session; | |
// Reading session data | |
String userId = session['user']; | |
// Updating session data | |
session['user'] = '5'; | |
}); | |
await server.serve(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:jaguar/jaguar.dart'; | |
main() => Jaguar(port: 8080) | |
..get('/about', (ctx) => 'Jaguar') | |
..postJson('/echo', (ctx) => ctx.bodyAsJson()) | |
..serve(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main() => Jaguar() | |
..staticFiles('/static/*', Directory('public')) | |
..serve(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:jaguar/jaguar.dart'; | |
import 'package:stencil/stencil.dart'; | |
main() async { | |
final server = Jaguar(); | |
server.get('/addition.html', (ctx) { | |
num a = ctx.query.getInt('a'), b = ctx.query.getInt('b'); | |
return ''' | |
<html> | |
<head><title>Stencil: An awesome template engine</title></head> | |
<body> | |
${when(a == null ||b == null, ''' | |
<div>Invalid request!</div> | |
''', ''' | |
<div>Result is <strong>${a + b}</strong>.</div> | |
''')} | |
</body> | |
</html> | |
'''; | |
}, mimeType: MimeType.html); | |
await server.serve(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment