Implemented read json courses
This commit is contained in:
parent
a1de036338
commit
fc5c786b00
4 changed files with 47 additions and 28 deletions
|
@ -6,6 +6,8 @@ import 'package:logging/logging.dart';
|
||||||
import 'package:rpc/rpc.dart';
|
import 'package:rpc/rpc.dart';
|
||||||
|
|
||||||
import '../lib/server/titamaapi.dart';
|
import '../lib/server/titamaapi.dart';
|
||||||
|
import '../lib/server/titamaio.dart';
|
||||||
|
import '../lib/common/messages.dart';
|
||||||
|
|
||||||
final ApiServer _apiServer = new ApiServer(prettyPrint: true);
|
final ApiServer _apiServer = new ApiServer(prettyPrint: true);
|
||||||
|
|
||||||
|
@ -14,7 +16,10 @@ main() async {
|
||||||
..level = Level.INFO
|
..level = Level.INFO
|
||||||
..onRecord.listen(print);
|
..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);
|
HttpServer server = await HttpServer.bind(InternetAddress.ANY_IP_V4, 8080);
|
||||||
server.listen(_apiServer.httpRequestHandler);
|
server.listen(_apiServer.httpRequestHandler);
|
||||||
|
|
|
@ -43,4 +43,15 @@ class Course {
|
||||||
map["id"] = id;
|
map["id"] = id;
|
||||||
return map;
|
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"];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,32 +8,11 @@ import './titamaio.dart';
|
||||||
@ApiClass(version: 'v1')
|
@ApiClass(version: 'v1')
|
||||||
class TitamaApi {
|
class TitamaApi {
|
||||||
|
|
||||||
final List<Course> _courses = new List<Course>();
|
List<Course> _courses = new List<Course>();
|
||||||
|
|
||||||
TitamaApi() {
|
TitamaApi(List<Course> courses) {
|
||||||
// example course, to remove once the database connection is implemented
|
// assign saved courses to list
|
||||||
Course course = new Course();
|
_courses = courses;
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiMethod(path: 'courses')
|
@ApiMethod(path: 'courses')
|
||||||
|
@ -84,4 +63,3 @@ class TitamaApi {
|
||||||
return course;
|
return course;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ library titama.io;
|
||||||
|
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
import '../common/messages.dart';
|
import '../common/messages.dart';
|
||||||
|
|
||||||
|
@ -13,7 +14,7 @@ class TitamaIo {
|
||||||
final _filename = "./data.json";
|
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.
|
* @param List<Courses> _course List of courses to save.
|
||||||
*/
|
*/
|
||||||
writeJson(List<Course> _courses) async {
|
writeJson(List<Course> _courses) async {
|
||||||
|
@ -21,4 +22,28 @@ class TitamaIo {
|
||||||
await new File(_filename).writeAsString(_json);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue