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

@ -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<Course> courses = await new TitamaIo().readJson();
_apiServer.addApi(new TitamaApi(courses));
HttpServer server = await HttpServer.bind(InternetAddress.ANY_IP_V4, 8080);
server.listen(_apiServer.httpRequestHandler);

View File

@ -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"];
}
}

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;
}
}