Skip to content

Instantly share code, notes, and snippets.

View graphicbeacon's full-sized avatar
🏠
Working from home

Jermaine Oppong graphicbeacon

🏠
Working from home
View GitHub Profile
@graphicbeacon
graphicbeacon / books_controller.dart
Created May 22, 2018 20:35
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Part 3)" post on Medium (4)
@httpGet
Future<Response> getBook(@HTTPPath("index") int idx) async {
var query = new Query<Book>()..where.id = whereEqualTo(idx);
var book = await query.fetchOne();
if (book == null) {
return new Response.notFound(body: 'Book does not exist')
..contentType = ContentType.TEXT;
}
return new Response.ok(book);
@graphicbeacon
graphicbeacon / books_controller.dart
Last active May 22, 2018 20:41
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Part 3)" post on Medium (5)
class BooksController extends HTTPController {
@httpGet
Future<Response> getAllBooks() async {
var query = new Query<Book>();
return new Response.ok(await query.fetch());
}
@httpGet
Future<Response> getBook(@HTTPPath("index") int idx) async {
var query = new Query<Book>()..where.id = whereEqualTo(idx);
@graphicbeacon
graphicbeacon / fave_reads_sink.dart
Last active May 22, 2018 21:01
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Part 3)" post on Medium (2)
class FaveReadsSink extends RequestSink {
FaveReadsSink(ApplicationConfiguration appConfig) : super(appConfig) {
logger.onRecord.listen(
(rec) => print("$rec ${rec.error ?? ""} ${rec.stackTrace ?? ""}"));
var managedDataModel = new ManagedDataModel.fromCurrentMirrorSystem(); // load our models
var persistentStore = new PostgreSQLPersistentStore.fromConnectionInfo(
"dartuser", "dbpass123", "localhost", 5432, "fave_reads"); // configure the db connection
ManagedContext.defaultContext = new ManagedContext(managedDataModel, persistentStore);
@graphicbeacon
graphicbeacon / example_test.dart
Created May 30, 2018 17:54
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Part 4)" post on Medium
TestApplication app = new TestApplication();
// Runs before all tests
setUpAll(() async {
await app.start();
});
// Runs after all tests
tearDownAll(() async {
await app.stop();
@graphicbeacon
graphicbeacon / fave_reads_sink.dart
Last active May 30, 2018 18:13
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Part 4)" post on Medium (2)
class FaveReadsSink extends RequestSink {
FaveReadsConfiguration config;
FaveReadsSink(ApplicationConfiguration appConfig) : super(appConfig) {
logger.onRecord.listen(
(rec) => print("$rec ${rec.error ?? ""} ${rec.stackTrace ?? ""}"));
var configFilePath = appConfig.configurationFilePath;
config = new FaveReadsConfiguration(configFilePath);
@graphicbeacon
graphicbeacon / fave_reads_sink.dart
Created May 30, 2018 18:20
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Part 4)" post on Medium (3)
class FaveReadsConfiguration extends ConfigurationItem {
FaveReadsConfiguration(String fileName) : super.fromFile(fileName);
DatabaseConnectionConfiguration database;
}
@graphicbeacon
graphicbeacon / utils.dart
Last active May 30, 2018 18:27
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Part 4)" post on Medium (4)
// This goes in lib/utils/utils.dart
import 'dart:async';
import 'package:aqueduct/aqueduct.dart';
Future createDatabaseSchema(ManagedContext context, bool isTemporary) async {
try {
var builder = new SchemaBuilder.toSchema(
context.persistentStore, new Schema.fromDataModel(context.dataModel),
isTemporary: isTemporary);
@graphicbeacon
graphicbeacon / fave_reads_sink.dart
Created May 30, 2018 18:34
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Part 4)" post on Medium (5)
import 'fave_reads.dart';
import './controller/books_controller.dart';
import './utils/utils.dart'; // 👈👈👈
class FaveReadsSink extends RequestSink {
//...
//...
@override
Future willOpen() async {
await createDatabaseSchema(ManagedContext.defaultContext, config.database.isTemporary);
@graphicbeacon
graphicbeacon / books_controller_test.dart
Created May 30, 2018 18:42
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Part 4)" post on Medium (6)
import 'harness/app.dart';
import 'package:fave_reads/model/book.dart';
Future main() async {
TestApplication app = new TestApplication();
setUpAll(() async {
await app.start();
});
@graphicbeacon
graphicbeacon / books_controller_test.dart
Created May 30, 2018 18:59
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Part 4)" post on Medium (7)
group("books controller", () {
test("GET /books returns list of books", () async {
// Arrange
var request = app.client.request("/books");
// Act
var response = await request.get();
// Assert
expectResponse(response, 200,