2016-06-13 17:13:02 +02:00
|
|
|
library titama.server;
|
|
|
|
|
|
|
|
import 'package:rpc/rpc.dart';
|
|
|
|
|
|
|
|
import '../common/messages.dart';
|
2016-06-25 10:44:10 +02:00
|
|
|
import './titamaio.dart';
|
2016-06-13 17:13:02 +02:00
|
|
|
|
|
|
|
@ApiClass(version: 'v1')
|
|
|
|
class TitamaApi {
|
|
|
|
|
2016-07-17 23:41:24 +02:00
|
|
|
List<Course> _courses = new List<Course>();
|
|
|
|
|
|
|
|
TitamaApi(List<Course> courses) {
|
|
|
|
// assign saved courses to list
|
|
|
|
_courses = courses;
|
2016-06-13 17:13:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@ApiMethod(path: 'courses')
|
|
|
|
List<Course> listCourses() {
|
2016-06-14 22:04:04 +02:00
|
|
|
if (_courses.isEmpty) {
|
|
|
|
throw new NotFoundError('Could not find any courses.');
|
|
|
|
}
|
2016-06-13 17:13:02 +02:00
|
|
|
return _courses;
|
|
|
|
}
|
|
|
|
|
2016-06-14 21:47:25 +02:00
|
|
|
@ApiMethod(path: 'course/{id}')
|
|
|
|
Course getCourse(int id) {
|
|
|
|
for (Course course in _courses) {
|
|
|
|
if (course.id == id) {
|
|
|
|
return course;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new NotFoundError('Could not find course \'$id\'.');
|
|
|
|
}
|
2016-06-13 17:13:02 +02:00
|
|
|
|
2016-06-19 12:40:21 +02:00
|
|
|
@ApiMethod(method: 'DELETE', path: 'course/{id}')
|
|
|
|
List<Course> deleteCourse(int id) {
|
|
|
|
_courses.removeWhere((course) => course.id == id);
|
2016-06-25 10:44:10 +02:00
|
|
|
|
|
|
|
new TitamaIo().writeJson(_courses);
|
|
|
|
|
2016-06-19 12:40:21 +02:00
|
|
|
return _courses;
|
|
|
|
}
|
|
|
|
|
2016-06-18 23:41:51 +02:00
|
|
|
@ApiMethod(method: 'POST', path: 'course')
|
|
|
|
Course addCourse (Course newCourse) {
|
|
|
|
newCourse.id = _courses.length;
|
|
|
|
_courses.add(newCourse);
|
|
|
|
|
2016-06-25 10:44:10 +02:00
|
|
|
new TitamaIo().writeJson(_courses);
|
|
|
|
|
2016-06-18 23:41:51 +02:00
|
|
|
return newCourse;
|
|
|
|
}
|
2016-06-19 13:19:22 +02:00
|
|
|
|
|
|
|
@ApiMethod(method: 'PUT', path: 'course/{id}')
|
|
|
|
Course updateCourse(int id, Course course) {
|
|
|
|
course.id = id;
|
|
|
|
int index = _courses.indexOf(_courses.singleWhere((crs) => crs.id == id));
|
|
|
|
_courses.replaceRange(index, index + 1, [course]);
|
2016-06-25 10:44:10 +02:00
|
|
|
|
|
|
|
new TitamaIo().writeJson(_courses);
|
|
|
|
|
2016-06-19 13:19:22 +02:00
|
|
|
return course;
|
|
|
|
}
|
2016-06-13 17:13:02 +02:00
|
|
|
}
|