Merge branch 'master' of gitlab.com:mmk2410/titama-backend

This commit is contained in:
Marcel Kapfer (mmk2410) 2016-11-21 18:03:59 +01:00
commit 2a5bee4688
11 changed files with 367 additions and 29 deletions

1
.gitignore vendored
View File

@ -26,3 +26,4 @@ doc/api/
/index.html
/json/
/test.js
data.json

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

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

View File

@ -7,6 +7,12 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3+1"
analyzer:
description:
name: analyzer
url: "https://pub.dartlang.org"
source: hosted
version: "0.27.2"
args:
description:
name: args
@ -19,6 +25,18 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.11.0"
barback:
description:
name: barback
url: "https://pub.dartlang.org"
source: hosted
version: "0.15.2+8"
boolean_selector:
description:
name: boolean_selector
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
charcode:
description:
name: charcode
@ -43,6 +61,12 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.1"
csslib:
description:
name: csslib
url: "https://pub.dartlang.org"
source: hosted
version: "0.13.2"
discoveryapis_generator:
description:
name: discoveryapis_generator
@ -55,6 +79,12 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.0+13"
glob:
description:
name: glob
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.3"
googleapis:
description:
name: googleapis
@ -67,12 +97,24 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.27.1"
html:
description:
name: html
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.2+2"
http:
description:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.11.3+7"
http_multi_server:
description:
name: http_multi_server
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
http_parser:
description:
name: http_parser
@ -97,12 +139,36 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.9.3"
package_config:
description:
name: package_config
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.5"
path:
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.9"
plugin:
description:
name: plugin
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.0"
pool:
description:
name: pool
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.4"
pub_semver:
description:
name: pub_semver
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
quiver:
description:
name: quiver
@ -115,6 +181,36 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.5.6+1"
shelf:
description:
name: shelf
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.5+2"
shelf_static:
description:
name: shelf_static
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.4"
shelf_web_socket:
description:
name: shelf_web_socket
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.1"
source_map_stack_trace:
description:
name: source_map_stack_trace
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.4"
source_maps:
description:
name: source_maps
url: "https://pub.dartlang.org"
source: hosted
version: "0.10.1+1"
source_span:
description:
name: source_span
@ -139,6 +235,12 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.5"
test:
description:
name: test
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.15+1"
typed_data:
description:
name: typed_data
@ -157,10 +259,22 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.9.0+3"
watcher:
description:
name: watcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.9.7+2"
web_socket_channel:
description:
name: web_socket_channel
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.4"
yaml:
description:
name: yaml
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.9"
sdk: ">=1.16.0 <2.0.0"
sdk: ">=1.16.0 <1.20.0"

View File

@ -7,3 +7,4 @@ environment:
dependencies:
rpc: "^0.5.6+1"
logging: "^0.11.3"
test: "^0.12.15+1"

101
test/TitamaIo_test.dart Normal file
View File

@ -0,0 +1,101 @@
import 'dart:io';
import 'package:test/test.dart';
import '../lib/common/messages.dart';
import '../lib/server/titamaio.dart';
String jsonContent = '[{"title":"UlmApi","time":"18:00","day":"Monday","kind":"Lab","place":"Weinhof9","prof":"","turnin":"","id":0},{"title":"CCC Ulm","time":"20:00","day":"Monday","kind":"Meeting","place":"Cafe Einstein","prof":"","turnin":"","id":1}]';
void main() {
group("JSON Serialization", () {
test("writeJson() writes a course list as a JSON object to a file.", () async {
List<Course> _courses = new List<Course>();
Course course = new Course();
course
..title = "UlmApi"
..time = "18:00"
..day = "Monday"
..id = 0
..kind = "Lab"
..place = "Weinhof9"
..prof = ""
..turnin = "";
_courses.add(course);
course = new Course();
course
..title = "CCC Ulm"
..time = "20:00"
..day = "Monday"
..id = 1
..kind = "Meeting"
..place = "Cafe Einstein"
..prof = ""
..turnin = "";
_courses.add(course);
TitamaIo io = new TitamaIo();
await io.writeJson(_courses);
String content = await new File('data.json').readAsString();
expect(content, equals(jsonContent));
});
test("readJson() reads the JSON storage file and parses it", () async {
final filename = "data.json";
List<Course> _courses = new List<Course>();
Course course = new Course();
course
..title = "UlmApi"
..time = "18:00"
..day = "Monday"
..id = 0
..kind = "Lab"
..place = "Weinhof9"
..prof = ""
..turnin = "";
_courses.add(course);
course = new Course();
course
..title = "CCC Ulm"
..time = "20:00"
..day = "Monday"
..id = 1
..kind = "Meeting"
..place = "Cafe Einstein"
..prof = ""
..turnin = "";
_courses.add(course);
await new File(filename).writeAsString(jsonContent);
TitamaIo io = new TitamaIo();
List<Course> courses = await io.readJson();
expect(courses, equals(_courses));
});
});
}

101
test/TitamaIo_test.dart~ Normal file
View File

@ -0,0 +1,101 @@
import 'dart:io';
import 'package:test/test.dart';
import '../lib/common/messages.dart';
import '../lib/server/titamaio.dart';
String jsonContent = '[{"title":"UlmApi","time":"18:00","day":"Monday","kind":"Lab","place":"Weinhof9","prof":"","turnin":"","id":0},{"title":"CCC Ulm","time":"20:00","day":"Monday","kind":"Meeting","place":"Cafe Einstein","prof":"","turnin":"","id":1}]';
void main() {
group("JSON Serialization", () {
test("writeJson() writes a course list as a JSON object to a file.", () async {
List<Course> _courses = new List<Course>();
Course course = new Course();
course
..title = "UlmApi"
..time = "18:00"
..day = "Monday"
..id = 0
..kind = "Lab"
..place = "Weinhof9"
..prof = ""
..turnin = "";
_courses.add(course);
course = new Course();
course
..title = "CCC Ulm"
..time = "20:00"
..day = "Monday"
..id = 1
..kind = "Meeting"
..place = "Cafe Einstein"
..prof = ""
..turnin = "";
_courses.add(course);
TitamaIo io = new TitamaIo();
await io.writeJson(_courses);
String content = await new File('data.json').readAsString();
expect(content, equals(jsonContent));
});
test("readJson() reads the JSON storage file and parses it", () async {
final filename = "data.json";
List<Course> _courses = new List<Course>();
Course course = new Course();
course
..title = "UlmApi"
..time = "18:00"
..day = "Monday"
..id = 0
..kind = "Lab"
..place = "Weinhof9"
..prof = ""
..turnin = "";
_courses.add(course);
course = new Course();
course
..title = "CCC Ulm"
..time = "20:00"
..day = "Monday"
..id = 1
..kind = "Meeting"
..place = "Cafe Einstein"
..prof = ""
..turnin = "";
_courses.add(course);
await new File(filename).writeAsString(jsonContent);
TitamaIo io = new TitamaIo();
List<Course> courses = await io.readJson();
expect(courses, equals(_courses));
});
});
}

1
test/packages Symbolic link
View File

@ -0,0 +1 @@
../packages