This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TestApplication app = new TestApplication(); | |
// Runs before all tests | |
setUpAll(() async { | |
await app.start(); | |
}); | |
// Runs after all tests | |
tearDownAll(() async { | |
await app.stop(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FaveReadsConfiguration extends ConfigurationItem { | |
FaveReadsConfiguration(String fileName) : super.fromFile(fileName); | |
DatabaseConnectionConfiguration database; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'harness/app.dart'; | |
import 'package:fave_reads/model/book.dart'; | |
Future main() async { | |
TestApplication app = new TestApplication(); | |
setUpAll(() async { | |
await app.start(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |