-
Notifications
You must be signed in to change notification settings - Fork 1
/
TodosServer.dart
59 lines (47 loc) · 1.36 KB
/
TodosServer.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#import("dart:io");
#import("dart:json");
#import("vendor/Express/Express.dart");
#import("vendor/Mixins/Mixin.dart");
#import("vendor/DartRedisClient/RedisClient.dart");
void main(){
RedisClient client = new RedisClient();
Express app = new Express();
app
.use(new StaticFileHandler())
.get("/todos", (HttpContext ctx){
client.keys("todo:*").then((keys) =>
client.mget(keys).then(ctx.sendJson)
);
})
.get("/todos/:id", (HttpContext ctx){
var id = ctx.params["id"];
client.get("todo:$id}").then((todo) =>
todo != null ?
ctx.sendJson(todo) :
ctx.notFound("todo $id does not exist")
);
})
.post("/todos", (HttpContext ctx){
ctx.readAsJson().then((x){
client.incr("ids:todo").then((newId){
var todo = $(x).defaults({"content":null,"done":false,"order":0});
todo["id"] = newId;
client.set("todo:$newId", todo);
ctx.sendJson(todo);
});
});
})
.put("/todos/:id", (HttpContext ctx){
var id = ctx.params["id"];
ctx.readAsJson().then((todo){
client.set("todo:$id", todo);
ctx.sendJson(todo);
});
})
.delete("/todos/:id", (HttpContext ctx){
client.del("todo:${ctx.params['id']}");
ctx.send();
})
.listen("0.0.0.0", 8000);
print("listening on 8000...");
}