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 / main.dart
Created April 21, 2018 20:10
Sample code for 'Learn Dart Before You Flutter' blog post on Medium (Complete)
class Order {
// private properties
int _id;
String _reference;
DateTime _date;
// public properties
String code;
String from;
List<String> bookings;
@graphicbeacon
graphicbeacon / fave_reads_sink.dart
Last active May 16, 2018 21:32
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Part 2)" post on Medium
@override
void setupRouter((Router router) async {
router.route('/books[/:index]').listen((Request incomingRequest) async {
return new Response.ok('Showing all books.');
});
router.route('/').listen((Request incomingRequest) async {
return new Response.ok('<h1>Welcome to FaveReads</h1>')
..contentType = ContentType.HTML;
});
});
@graphicbeacon
graphicbeacon / fave_reads_sink.dart
Created May 16, 2018 21:39
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Part 2)" post on Medium (2)
router.route('/books[/:index]').listen((Request incomingRequest) async {
String reqMethod = incomingRequest.innerRequest.method;
String index = incomingRequest.path.variables["index"];
if (reqMethod == 'GET') {
if(index != null) {
return new Response.ok('Showing book by index: $index');
}
return new Response.ok('Showing all books.');
} else if (reqMethod == 'POST') {
@graphicbeacon
graphicbeacon / books_controller.dart
Last active May 16, 2018 21:58
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Part 2)" post on Medium (3)
import '../fave_reads.dart';
class BooksController extends HTTPController {
// invoked for GET /books
@httpGet // HTTPMethod meta data
Future<Response> getAllBooks() async => new Response.ok('Showing all books');
// invoked for GET /books/:index
@httpGet // HTTPMethod meta data
Future<Response> getBook(@HTTPPath("index") int idx) async => new Response.ok('Showing single book');
@graphicbeacon
graphicbeacon / books_controller.dart
Last active May 16, 2018 22:10
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Part 2)" post on Medium (4)
import '../fave_reads.dart';
List books = [
{
'title': 'Head First Design Patterns',
'author': 'Eric Freeman',
'year': 2004
},
{
'title': 'Clean Code: A handbook of Agile Software Craftsmanship',
@graphicbeacon
graphicbeacon / books_controller.dart
Created May 16, 2018 22:13
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Part 2)" post on Medium (5)
class BooksController extends HTTPController {
@httpGet
Future<Response> getAll() async => new Response.ok(books);
@httpGet
Future<Response> getSingle(@HTTPPath("index") int idx) async {
if (idx < 0 || idx > books.length - 1) { // index out of range
return new Response.notFound(body: 'Book does not exist');
}
return new Response.ok(books[idx]);
@graphicbeacon
graphicbeacon / book.dart
Created May 16, 2018 22:29
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Part 2)" post on Medium (6)
import '../fave_reads.dart';
class Book extends HTTPSerializable {
String title;
String author;
int year;
Book({this.title, this.author, this.year});
@override
@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 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