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_test.dart
Created June 15, 2018 15:53
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Bonus content)" post on Medium (3)
// Populate DB
var books = [
new Book()
..title = "Head First Design Patterns"
..authors = (new ManagedSet()
..add(new Author()..name = "Bert Bates")
..add(new Author()..name = "Kathy Sierra")
..add(new Author()..name = "Eric Freeman"))
..year = 2004,
new Book()
@graphicbeacon
graphicbeacon / books_controller_test.dart
Last active June 15, 2018 15:55
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Bonus content)" post on Medium (4)
// Query for books
await Future.forEach(books, (Book b) async {
var query = new Query<Book>()..values = b;
var insertedBook = await query.insert();
// Query for authors
await Future.forEach(b.authors, (Author a) async {
var query = new Query<Author>()
..values = a
..values.book = insertedBook; // Reference to its associated book, which becomes a foreign key column `book_id`
@graphicbeacon
graphicbeacon / books_controller_test.dart
Created June 15, 2018 15:59
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Bonus content)" post on Medium (5)
// ...
// ...
test("GET /books returns list of books", () async {
// ..
// ..
expectResponse(response, 200, body: allOf([
hasLength(greaterThan(0)),
everyElement(partial(
{
"title": isString,
@graphicbeacon
graphicbeacon / books_controller.dart
Created June 15, 2018 16:03
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Bonus content)" post on Medium (6)
// ..
class BooksController extends HTTPController {
@httpGet
Future<Response> getAllBooks() async {
var query = new Query<Book>()..join(set: (book) => book.authors);
return new Response.ok(await query.fetch());
}
// ..
// ..
}
@graphicbeacon
graphicbeacon / books_controller.dart
Last active June 15, 2018 16:08
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Bonus content)" post on Medium (7)
// ..
class BooksController extends HTTPController {
//..
@httpPost
Future<Response> addBook(@HTTPBody() Book book) async {
var query = new Query<Book>()..values = book;
var insertedBook = await query.insert();
// Insert authors from payload
await Future.forEach(book.authors, (Author a) async {
@graphicbeacon
graphicbeacon / letter-animation-effect.dart
Last active June 28, 2018 14:35
Letter animation effect
import 'dart:async';
void main() {
List<String> alphabets = 'abcdefghijklmnopqrstuvwxyz'.split('');
var stream = new Stream.fromIterable(alphabets);
var count = 1;
stream.listen((char) {
Timer(Duration(seconds: 1 * count), () {
print(char);
@graphicbeacon
graphicbeacon / variables-and-inbuilt-types.dart
Last active June 28, 2018 14:34
Variables and Built-in types in Dart
void main() {
// Variables and constants
var edition = 2;
int year = 2011;
const language = 'Dart';
String name; // == null
// Number
var num1 = 5;
var num2 = 10.0;
@graphicbeacon
graphicbeacon / top_10_array_utilities.dart
Last active June 28, 2018 16:31
Sample snippet for "Top 10 Array utility methods you should know (Dart) 🎯" article on Medium (Full solution)
void main() {
// forEach()
var fruits = ['banana', 'pineapple', 'watermelon'];
fruits.forEach((fruit) => print(fruit)); // => banana pineapple watermelon
print('\n---\n');
// map()
var mappedFruits = fruits.map((fruit) => 'I love $fruit').toList();
@graphicbeacon
graphicbeacon / index.html
Created July 6, 2018 23:11
Code for a timer
<span class="clock"></span>
@graphicbeacon
graphicbeacon / top_10_map_utilties.dart
Created July 6, 2018 23:25
Sample code for "Top 10 Map/Object utility methods you should know (Dart) 🎯" (Complete solution)
void main() {
var user = {
"firstName": "Tom",
"age": 25,
};
// 1. addAll()
user.addAll({
"lastName": "Smith",
"age": 26,