Permalink
Browse files

Merge pull request #10 from cedricss/master

Code ported to mongodb backend
  • Loading branch information...
2 parents 83a0435 + ea7c95d commit 2562d8905dbe2f26c31a9bbfbe158935406cebb7 @tsloughter committed Mar 11, 2012
Showing with 125 additions and 122 deletions.
  1. +2 −2 Makefile
  2. +13 −13 README.md
  3. +2 −0 resources/style.css
  4. +11 −11 src/admin.opa
  5. +10 −15 src/main.opa
  6. +25 −34 src/todo.opa
  7. +34 −0 src/type.opa
  8. +0 −2 src/ui.opa
  9. +28 −45 src/user.opa
View
@@ -1,11 +1,11 @@
NAME = main.exe
-SRC = src/todo.opa src/user.opa src/admin.opa src/main.opa src/ui.opa
+SRC = src/type.opa src/todo.opa src/user.opa src/admin.opa src/main.opa src/ui.opa
all: $(NAME)
$(NAME): $(SRC)
- opa --parser js-like $(SRC) -o $(NAME)
+ opa --database mongo $(SRC) -o $(NAME)
clean:
rm -f $(NAME)
View
@@ -1,17 +1,17 @@
-## Install Opa: [http://opalang.org](http://opalang.org)
+## Install
-## Compile and Run:
+- Opa 9.0.1: [http://opalang.org](http://opalang.org)
+- Mongodb 2.0: http://mongodb.org
+
+## Compile
+
+```bash
+make
+```
+
+## Run
```bash
-19:05 (master) λ make
-opa --parser js-like src/todo.opa src/user.opa src/admin.opa src/main.opa -o main.exe
-Embedding file "/Users/tristan/Devel/opado/resources/destroy.png" as resource "resources/destroy.png" with mimetype "image/png"
-Embedding file "/Users/tristan/Devel/opado/resources/js/bugherd.js" as resource "resources/js/bugherd.js" with mimetype "application/javascript"
-Embedding file "/Users/tristan/Devel/opado/resources/js/google_analytics.js" as resource "resources/js/google_analytics.js" with mimetype "application/javascript"
-Embedding file "/Users/tristan/Devel/opado/resources/todos.css" as resource "resources/todos.css" with mimetype "text/css"
-[tristan@marx ~/Devel/opado]
-19:05 (master) λ ./main.exe
-Accesses logged to access.log
-Messages logged to error.log
-Http (OPA/1056) serving on http://marx.local:8080
+mongod --dbpath . > log.log &
+./main.exe
```
View
@@ -150,6 +150,8 @@ ul#todo_list .display:hover .icon.icon-remove {display:inline-block;}
.todo_content {padding:0 3px;font-size:18px; line-height:22px;color:#6C6B66;}
+.done .todo_content{text-decoration: line-through;}
+
/* Edit input */
#todo_list .editing .display, #todo_list .edit {display: none;}
#todo_list .editing .edit {display: block;}
View
@@ -1,16 +1,15 @@
-package opado.admin
-
-import opado.ui
-import opado.todo
-import opado.user
+import stdlib.database.mongo
module Admin {
function add_users() {
- users = /users;
- Map.iter((function(_, y){
- items = /todo_items[y.username];
- add_user_to_page(y.username, y.fullname, y.is_oauth, Map.size(items))
- }), users)
+ dbset(User.t, _) users = /opado/users;
+ it = DbSet.iterator(users);
+ Iter.iter((function(user){
+ useref = user.ref;
+ dbset(Todo.t, _) items = /opado/todos[ useref == useref ];
+ it = DbSet.iterator(items);
+ add_user_to_page(user.username, user.fullname, user.is_oauth, Iter.count(it))
+ }), it)
}
function add_user_to_page(string username, string fullname, bool is_oauth, int size) {
@@ -33,7 +32,8 @@ module Admin {
}
resource =
- (Parser.general_parser((http_request -> 'toto))) parser (.*) -> function(_req){
+ (Parser.general_parser((http_request -> 'toto))) parser { (.*) : function(_req){
mypage("Admin", admin())
}
+ }
}
View
@@ -1,22 +1,17 @@
-package opado.main
-
-import opado.user
-import opado.admin
-import opado.todo
-
function with_request(f){
f(ThreadContext.get({current}).request ? error("no request"))
}
-urls = parser
- {Rule.debug_parse_string((function(s){Log.notice("URL", s)}))}
- Rule.fail -> error("")
- | "/todos?" result={Todo.resource} -> with_request(result)
- | "/connect?" data=(.*) -> User.connect(Text.to_string(data))
- | "/user" result={User.resource} -> with_request(result)
- | "/login" result={User.resource} -> with_request(result)
- | "/admin" result={Admin.resource} -> with_request(result)
- | (.*) result={Todo.resource} -> with_request(result)
+urls = parser {
+ //Rule.debug_parse_string((function(s){Log.notice("URL", s)}))
+ //Rule.fail : error("Error")
+ | "/todos?" result={Todo.resource} : with_request(result)
+ | "/connect?" data=(.*) : User.connect(Text.to_string(data))
+ | "/user" result={User.resource} : with_request(result)
+ | "/login" result={User.resource} : with_request(result)
+ | "/admin" result={Admin.resource} : with_request(result)
+ | (.*) result={Todo.resource} : with_request(result)
+ }
Server.start(Server.http,
[{resources:@static_resource_directory("resources")},
View
@@ -1,18 +1,5 @@
-package opado.todo
-
-import opado.user
-import opado.ui
import stdlib.web.client
-type todo_item = {
- string value,
- bool done,
- string created_at
-}
-
-database stringmap(stringmap(todo_item)) /todo_items
-database /todo_items[_][_]/done = false
-
module Todo {
function update_counts() {
num_done = Dom.length(Dom.select_class("done"));
@@ -32,11 +19,8 @@ module Todo {
}
exposed @async function db_make_done(string id) {
- username = User.get_username();
- items = /todo_items[username];
- item = Option.get(StringMap.get(id, items));
- @/todo_items[username] <-
- StringMap.add(id, {item with done : true}, items)
+ useref = User.get_username();
+ /opado/todos[~{ id }] <- { done : true };
}
function remove_item(string id) {
@@ -46,9 +30,9 @@ module Todo {
}
exposed @async function db_remove_item(string id) {
- username = User.get_username();
- items = /todo_items[username];
- @/todo_items[username] <- StringMap.remove(id, items)
+ useref = User.get_username();
+ Db.remove(@/opado/todos[~{ id }]);
+ void
}
@async function remove_all_done() {
@@ -62,17 +46,16 @@ module Todo {
add_todo_to_page(id, x, false)
}
- exposed @async function db_add_todo(string id, string x) {
- username = User.get_username();
- items = /todo_items[username];
- @/todo_items[username] <-
- StringMap.add(id, { value : x, done : false, created_at : "" }, items)
+ exposed @async function db_add_todo(string id, string value) {
+ useref = User.get_username();
+ /opado/todos[~{ id }] <- { id : id, useref : useref, value : value } // not necessary to specify default values
}
exposed function add_todos() {
- username = User.get_username();
- items = /todo_items[username];
- StringMap.iter((function(x,y){add_todo_to_page(x, y.value, y.done)}), items)
+ useref = User.get_username();
+ dbset(Todo.t, _) items = /opado/todos[ useref == useref];
+ it = DbSet.iterator(items);
+ Iter.iter((function(item){add_todo_to_page(item.id, item.value, item.done)}), it)
}
function update_todo(string id, string value) {
@@ -95,12 +78,18 @@ module Todo {
}
function add_todo_to_page(string id, string value, bool is_done) {
+ done = if (is_done) "done" else ""
+ checkbox = if (is_done) {
+ <input checked="yes" class="check" type="checkbox" onclick={function(_){make_done(id)}}/>
+ }else{
+ <input class="check" type="checkbox" onclick={function(_){make_done(id)}}/>
+ }
line =
- <li><div class="todo {if (is_done) "done" else ""}" id={ id }>
+ <li><div class="todo {done}" id={ id }>
<div class="display">
<span id={id^"_destroy"} class="todo_destroy icon icon-remove" onclick={function(_){remove_item(id)}}></span>
- <input class="check" type="checkbox" onclick={function(_){make_done(id)}}/>
- <div id={id^"_todo"} class="todo_content" onclick={function(_){make_editable(id, value)}}>{ value }</div>
+ {checkbox}
+ <div id={id^"_todo"} class="todo_content" onclick={function(_){ if (is_done) { }else{ make_editable(id, value) }}}>{ value }</div>
</div>
</div></li>
Dom.transform([#todo_list =+ line]);
@@ -154,6 +143,8 @@ module Todo {
}
resource =
- (Parser.general_parser((http_request -> resource))) parser
- (.*) -> function(_req){todos()}
+ (Parser.general_parser((http_request -> resource))) parser {
+ (.*) : function(_req){todos()}
+ }
+
}
View
@@ -0,0 +1,34 @@
+type User.password = string
+type User.ref = string
+
+type User.t = {
+ User.ref ref,
+ string username,
+ string fullname,
+ User.password password,
+ bool is_oauth,
+}
+
+type User.status = {User.ref logged} or {unlogged}
+
+type User.info = UserContext.t(User.status)
+type User.map('a) = ordered_map(User.ref, 'a, String.order)
+
+type Todo.id = string
+
+type Todo.t = {
+ Todo.id id,
+ string useref,
+ string value,
+ bool done,
+ string created_at
+}
+
+database opado @mongo {
+ User.t /users[{ref}]
+ /users[_]/is_oauth = false
+ Todo.t /todos[{id}]
+ /todos[_]/done = false
+ // Default value for string is "" (empty string)
+}
+
View
@@ -2,8 +2,6 @@
* @author Ida Swarczewskaja
**/
-package opado.ui
-
import stdlib.themes.bootstrap.core
/*module Desktop {
Oops, something went wrong.

0 comments on commit 2562d89

Please sign in to comment.