Skip to content
Browse files

Code migrated to mongo backend

  • Loading branch information...
1 parent 5ad3da9 commit a05eb5ecad5333cd09db2e9aeebdd859587c522f @cedricss cedricss committed Mar 7, 2012
Showing with 83 additions and 89 deletions.
  1. +2 −2 Makefile
  2. +10 −9 src/admin.opa
  3. +2 −8 src/main.opa
  4. +13 −29 src/todo.opa
  5. +35 −0 src/type.opa
  6. +0 −2 src/ui.opa
  7. +21 −39 src/user.opa
View
4 Makefile
@@ -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
19 src/admin.opa
@@ -1,15 +1,16 @@
-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))
+ dbset(User.t) users = /opado/users;
+ // dbset(User.t) users = /opado/users; // with Opa 9.0.1
+ users = DbSet.to_list(users)
+ // it = DbSet.iterator(users); // with Opa 9.0.1
+ List.iter((function(user){
+ useref = user.ref;
+ dbset(Todo.t) items = /opado/todos[ useref == useref ];
+ items = DbSet.to_list(items);
+ add_user_to_page(user.username, user.fullname, user.is_oauth, List.length(items))
}), users)
}
View
10 src/main.opa
@@ -1,16 +1,10 @@
-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("")
+ //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)
View
42 src/todo.opa
@@ -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,10 @@ 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();
+ // Not implemented in Opa 9.0.0 for mongo backend
+ // Db.remove(@/opado/todos[~{ id }]);
+ void
}
@async function remove_all_done() {
@@ -62,17 +47,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];
+ items = DbSet.to_list(items);
+ List.iter((function(item){add_todo_to_page(item.id, item.value, item.done)}), items)
}
function update_todo(string id, string value) {
View
35 src/type.opa
@@ -0,0 +1,35 @@
+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 { // with Opa 9.0.0
+database opado {
+ User.t /users[{ref}]
+ /users[_]/is_oauth = false
+ Todo.t /todos[{id}]
+ /todos[_]/done = false
+ // Default value for string is "" (empty string)
+}
+
View
2 src/ui.opa
@@ -2,8 +2,6 @@
* @author Ida Swarczewskaja
**/
-package opado.ui
-
import stdlib.themes.bootstrap.core
/*module Desktop {
View
60 src/user.opa
@@ -1,22 +1,25 @@
-/*
+/*:54
* USER.OPA
*
* @author Tristan Sloughter
* @author Matthieu Guffroy
**/
-package opado.user
-
import stdlib.crypto
import stdlib.web.client
import stdlib.core.web.core
import stdlib.widgets.core
import stdlib.widgets.loginbox
import stdlib.widgets.formbuilder
-import stdlib.{themes.bootstrap, widgets.bootstrap}
+import stdlib.{widgets.bootstrap}
import stdlib.apis.{facebook, facebook.auth, facebook.graph}
-import opado.ui
+/**
+database stringmap(stringmap(todo_item)) /todo_items
+database /todo_items[_][_]/done = false
+database User.map(User.t) /users
+database /users[_]/is_oauth = false
+*/
// DATA
@@ -32,40 +35,23 @@ FBA = FbAuth(OpaIntro1.config)
FBG = FbGraph
redirect = "http://opado.org/connect"
-abstract type User.password = string
-abstract type User.ref = string
-
-type User.t = { 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)
-
-database User.map(User.t) /users
-database /users[_]/is_oauth = false
-
-
module User_data {
function User.ref mk_ref(string login){
String.to_lower(login)
}
- function string ref_to_string(User.ref login){
- login
+ function string ref_to_string(User.ref ref){
+ ref
}
- function void save(User.ref ref,User.t user){
- @/users[ref] <- user
+ function void save(User.t user){
+ /opado/users[{ref : user.ref}] <- user
}
function option(User.t) get(User.ref ref){
- ?/users[ref]
+ ?/opado/users[~{ref}]
}
+
}
module User {
@@ -77,21 +63,17 @@ module User {
}
function user_create(username, password, is_oauth) {
- useref = User_data.mk_ref(username);
- user = User_data.get(useref);
+ ref = User_data.mk_ref(username);
+ user = User_data.get(ref);
match (user) {
- case { none }:
- user =
- (User.t) { username: useref
- , fullname : ""
- , password : Crypto.Hash.sha2(password)
- , is_oauth : is_oauth };
- @/users[username] <- user
+ case { none }: void
+ /opado/users[~{ ref }] <- { ref : ref, username: ref, password : Crypto.Hash.sha2(password), is_oauth : is_oauth}; // not necessary to specify default values
default: void
}
}
+
function get_status() {
UserContext.execute((function(a){a}), state)
}
@@ -224,11 +206,11 @@ module User {
<p>
Username : <input id=#{username_id}
onchange={function(_){
- User_data.save(r, {user with username : Dom.get_value(#{username_id})})
+ User_data.save({user with ref : r, username : Dom.get_value(#{username_id})})
}}
value={user.username} /><br />
Fullname : <input id=#{fullname_id}
- onchange={function(_){ User_data.save(r, {user with fullname : Dom.get_value(#{fullname_id})})
+ onchange={function(_){ User_data.save({user with ref : r, fullname : Dom.get_value(#{fullname_id})})
}}
value={user.fullname} />
</p>

0 comments on commit a05eb5e

Please sign in to comment.
Something went wrong with that request. Please try again.