Implemented read json courses

This commit is contained in:
Marcel Kapfer (mmk2410) 2016-07-17 23:41:24 +02:00
parent a1de036338
commit fc5c786b00
4 changed files with 47 additions and 28 deletions

View file

@ -8,32 +8,11 @@ import './titamaio.dart';
@ApiClass(version: 'v1')
class TitamaApi {
final List<Course> _courses = new List<Course>();
List<Course> _courses = new List<Course>();
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<Course> courses) {
// assign saved courses to list
_courses = courses;
}
@ApiMethod(path: 'courses')
@ -84,4 +63,3 @@ class TitamaApi {
return course;
}
}

View file

@ -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<Courses> _course List of courses to save.
*/
writeJson(List<Course> _courses) async {
@ -21,4 +22,28 @@ class TitamaIo {
await new File(_filename).writeAsString(_json);
}
/**
* Read a saved json file.
* @return Future<List<Course>> future with list of courses.
*/
Future<List<Course>> readJson() async {
List<Course> courses = new List<Course>();
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;
}
}