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 16, 2018 22:38
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Part 2)" post on Medium (7)
// lib/controller/book_controller.dart
import '../fave_reads.dart';
import 'model/book.dart';
List books = [
new Book(
title: 'Head First Design Patterns',
author: 'Eric Freeman',
year: 2004
),
@graphicbeacon
graphicbeacon / book.dart
Created May 21, 2018 20:57
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Part 3)" post on Medium
import '../fave_reads.dart';
class Book extends ManagedObject<_Book> implements _Book {}
class _Book {
@managedPrimaryKey
int id;
String title;
String author;
@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 / fave_reads_sink.dart
Last active May 22, 2018 20:19
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Part 3)" post on Medium (3)
class FaveReadsSink extends RequestSink {
//...
//...
//...
Future createDatabaseSchema(ManagedContext context) async {
var builder = new SchemaBuilder.toSchema(
context.persistentStore,
new Schema.fromDataModel(context.dataModel),
isTemporary: false); // Set to false to persist our data
@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 / 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);