78 lines
1.8 KiB
Dart
78 lines
1.8 KiB
Dart
library titama.server;
|
|
|
|
import 'package:rpc/rpc.dart';
|
|
|
|
import '../common/messages.dart';
|
|
|
|
@ApiClass(version: 'v1')
|
|
class TitamaApi {
|
|
|
|
final 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);
|
|
}
|
|
|
|
@ApiMethod(path: 'courses')
|
|
List<Course> listCourses() {
|
|
if (_courses.isEmpty) {
|
|
throw new NotFoundError('Could not find any courses.');
|
|
}
|
|
return _courses;
|
|
}
|
|
|
|
@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\'.');
|
|
}
|
|
|
|
@ApiMethod(method: 'DELETE', path: 'course/{id}')
|
|
List<Course> deleteCourse(int id) {
|
|
_courses.removeWhere((course) => course.id == id);
|
|
return _courses;
|
|
}
|
|
|
|
@ApiMethod(method: 'POST', path: 'course')
|
|
Course addCourse (Course newCourse) {
|
|
newCourse.id = _courses.length;
|
|
_courses.add(newCourse);
|
|
|
|
return newCourse;
|
|
}
|
|
|
|
@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]);
|
|
return course;
|
|
}
|
|
}
|
|
|