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 /index.html
/json/ /json/
/test.js /test.js
data.json

View file

@ -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);

View file

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

View file

View file

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

View file

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

View file

@ -7,6 +7,12 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.1.3+1" version: "0.1.3+1"
analyzer:
description:
name: analyzer
url: "https://pub.dartlang.org"
source: hosted
version: "0.27.2"
args: args:
description: description:
name: args name: args
@ -19,6 +25,18 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.11.0" 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: charcode:
description: description:
name: charcode name: charcode
@ -43,6 +61,12 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.1" version: "1.1.1"
csslib:
description:
name: csslib
url: "https://pub.dartlang.org"
source: hosted
version: "0.13.2"
discoveryapis_generator: discoveryapis_generator:
description: description:
name: discoveryapis_generator name: discoveryapis_generator
@ -55,6 +79,12 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.2.0+13" version: "0.2.0+13"
glob:
description:
name: glob
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.3"
googleapis: googleapis:
description: description:
name: googleapis name: googleapis
@ -67,12 +97,24 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.27.1" version: "0.27.1"
html:
description:
name: html
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.2+2"
http: http:
description: description:
name: http name: http
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.11.3+7" 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: http_parser:
description: description:
name: http_parser name: http_parser
@ -97,12 +139,36 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.9.3" version: "0.9.3"
package_config:
description:
name: package_config
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.5"
path: path:
description: description:
name: path name: path
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.3.9" 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: quiver:
description: description:
name: quiver name: quiver
@ -115,6 +181,36 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.5.6+1" 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: source_span:
description: description:
name: source_span name: source_span
@ -139,6 +235,12 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.1.5" version: "0.1.5"
test:
description:
name: test
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.15+1"
typed_data: typed_data:
description: description:
name: typed_data name: typed_data
@ -157,10 +259,22 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.9.0+3" 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: yaml:
description: description:
name: yaml name: yaml
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.9" 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: dependencies:
rpc: "^0.5.6+1" rpc: "^0.5.6+1"
logging: "^0.11.3" 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