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 May 30, 2018 19:07
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Part 4)" post on Medium (8)
test("POST /books creates a new book", () async {
var request = app.client.request("/books")
..json = {
"title": "Dart: Scalable Application Development",
"author": "Davy Mitchell, Sergey Akopkokhyants, Ivo Balbaert",
"year": 2007
};
expectResponse(await request.post(), 200,
body: partial({
"id": 4, // updated primary key from db
@graphicbeacon
graphicbeacon / books_controller_test.dart
Created May 30, 2018 19:53
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Part 4)" post on Medium (9)
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 / main.dart
Created June 3, 2018 23:01
Learn Dart: Query a RESTful JSON api in under 30 seconds
import 'dart:io';
import 'dart:async';
import 'dart:convert';
main() async {
Uri apiUrl = Uri.parse("https://swapi.co/api/people/1"); // Star Wars API
HttpClientRequest request = await new HttpClient().getUrl(apiUrl);
HttpClientResponse response = await request.close();
@graphicbeacon
graphicbeacon / main.dart
Last active June 3, 2018 23:03
Learn Dart: Write your first program in under 30 seconds
void main() {
print("This is my first Dart program.");
}
@graphicbeacon
graphicbeacon / main.dart
Created June 3, 2018 22:51
Learn Dart: Create an HTTP server in under 30 seconds
import "dart:io";
void main() {
HttpServer.bind("localhost", 8080).then((HttpServer server) {
server.listen((HttpRequest request) {
request.response.write("Hello world");
request.response.close();
});
});
}
@graphicbeacon
graphicbeacon / author.dart
Created June 15, 2018 15:40
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Bonus content)" post on Medium
import ‘../fave_reads.dart’;
import ‘./book.dart’;
class Author extends ManagedObject<_Author> implements _Author {}
class _Author {
@managedPrimaryKey
int id;
String name;
@graphicbeacon
graphicbeacon / book.dart
Created June 15, 2018 15:42
Sample code for "Building RESTful Web APIs with Dart, Aqueduct and PostgreSQL (Bonus content)" post on Medium (2)
import ‘../fave_reads.dart’;
import ‘./author.dart’;
class Book extends ManagedObject<_Book> implements _Book {}
class _Book {
@managedPrimaryKey
int id;
String title;
@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,