diff --git a/bin/titama.dart b/bin/titama.dart index 3785ded..56deb0d 100644 --- a/bin/titama.dart +++ b/bin/titama.dart @@ -6,6 +6,8 @@ import 'package:logging/logging.dart'; import 'package:rpc/rpc.dart'; import '../lib/server/titamaapi.dart'; +import '../lib/server/titamaio.dart'; +import '../lib/common/messages.dart'; final ApiServer _apiServer = new ApiServer(prettyPrint: true); @@ -14,7 +16,10 @@ main() async { ..level = Level.INFO ..onRecord.listen(print); - _apiServer.addApi(new TitamaApi()); + // read saved data + List courses = await new TitamaIo().readJson(); + + _apiServer.addApi(new TitamaApi(courses)); HttpServer server = await HttpServer.bind(InternetAddress.ANY_IP_V4, 8080); server.listen(_apiServer.httpRequestHandler); diff --git a/lib/common/messages.dart b/lib/common/messages.dart index 3a20ca9..0ba62cf 100644 --- a/lib/common/messages.dart +++ b/lib/common/messages.dart @@ -43,4 +43,15 @@ class Course { map["id"] = id; return map; } + + Course.fromJson(Map courseData) { + title = courseData["title"]; + time = courseData["time"]; + day = courseData["day"]; + kind = courseData["kind"]; + place = courseData["place"]; + prof = courseData["prof"]; + turnin = courseData["turnin"]; + id = courseData["id"]; + } } diff --git a/lib/server/titamaapi.dart b/lib/server/titamaapi.dart index 5dac42a..b2f522e 100644 --- a/lib/server/titamaapi.dart +++ b/lib/server/titamaapi.dart @@ -8,32 +8,11 @@ import './titamaio.dart'; @ApiClass(version: 'v1') class TitamaApi { - final List _courses = new List(); + List _courses = new List(); - TitamaApi() { - // example course, to remove once the database connection is implemented - Course course = new Course(); - course - ..title = "UlmAPI" - ..time = "18:00" - ..day = "Monday" - ..id = 0 - ..kind = "Lab" - ..place = "O27/343" - ..prof = "" - ..turnin = ""; - _courses.add(course); - Course course2 = new Course(); - course2 - ..title = "CCC Ulm" - ..time = "20:00" - ..day = "Monday" - ..id = 1 - ..kind = "Meeting" - ..place = "Cafe Einstein" - ..prof = "" - ..turnin = ""; - _courses.add(course2); + TitamaApi(List courses) { + // assign saved courses to list + _courses = courses; } @ApiMethod(path: 'courses') @@ -84,4 +63,3 @@ class TitamaApi { return course; } } - diff --git a/lib/server/titamaio.dart b/lib/server/titamaio.dart index 0dec2e3..5169b4c 100644 --- a/lib/server/titamaio.dart +++ b/lib/server/titamaio.dart @@ -2,6 +2,7 @@ library titama.io; import 'dart:io'; import 'dart:convert'; +import 'dart:async'; import '../common/messages.dart'; @@ -13,7 +14,7 @@ class TitamaIo { final _filename = "./data.json"; /** - * Write courses as JSON in a file.k + * Write courses as JSON in a file. * @param List _course List of courses to save. */ writeJson(List _courses) async { @@ -21,4 +22,28 @@ class TitamaIo { await new File(_filename).writeAsString(_json); } + /** + * Read a saved json file. + * @return Future> future with list of courses. + */ + Future> readJson() async { + List courses = new List(); + + File data = new File(_filename); + + if (await data.exists()) { + + String _content = await data.readAsString(); + Map parsed = JSON.decode(_content); + + + for (int i = 0; i < parsed.length; i++) { + courses.add(new Course.fromJson(parsed[i])); + } + + } + + return courses; + } + }