diff options
Diffstat (limited to 'server/cldb.erl')
-rw-r--r-- | server/cldb.erl | 48 |
1 files changed, 28 insertions, 20 deletions
diff --git a/server/cldb.erl b/server/cldb.erl index 982aafc..0602b7d 100644 --- a/server/cldb.erl +++ b/server/cldb.erl @@ -1,7 +1,7 @@ % The clientdatabase -module(cldb). --export([init/0, ask/2, login/2, decVote/2, incVote/2, register/3, check_rights/2]). +-export([init/0, ask/2, login/2, decVote/1, incVote/1, register/3, check_rights/1]). -record(user, {name, passwd, votes, rights}). init() -> @@ -9,45 +9,53 @@ init() -> mnesia:start(), mnesia:create_table(user, [{attributes, record_info(fields, user)}]), io:format("Userdb up and running \n"). - + ask(User, Pwd) -> F = fun() -> - mnesia:match_object({user, User, Pwd, '_'}) + mnesia:match_object({user, User, Pwd, '_', '_'}) end, mnesia:transaction(F). - - - + login(User, Pwd) -> - {_, Reason} = ask(User, Pwd), - Reason. + case ask(User, Pwd) of + {atomic, [UserRow|_]} -> {ok, UserRow}; + _-> {error} + end. register(User, Pwd, Root) -> - F = fun() -> - mnesia:write(#user{name = User, passwd = Pwd, votes = 5, rights = Root}) - end, - {_, Reason} = mnesia:transaction(F), - Reason. + case ask(User, '_') of + {atomic, []} -> + F = fun() -> + mnesia:write(#user{name = User, passwd = Pwd, votes = 5, rights = Root}) + end, + case mnesia:transaction(F) of + {atomic, ok} -> + {ok, user_created}; + {atomic, Why} -> + {error, Why} + end; + _ -> + {error, duplicated_user} + end. -decVote(User, Pwd) -> +decVote(User) -> F = fun() -> - [Head|_] = ask(User, Pwd), + [Head|_] = ask(User, '_'), Votes = Head#user.votes - 1, New = Head#user{votes = Votes}, mnesia:write(New) end, mnesia:transaction(F). -incVote(User, Pwd) -> +incVote(User) -> F = fun() -> - [Head|_] = ask(User, Pwd), + [Head|_] = ask(User, '_'), Votes = Head#user.votes + 1, New = Head#user{votes = Votes}, mnesia:write(New) end, mnesia:transaction(F). -check_rights(User, Name) -> - {_, _, Rights} = ask(User, Name), +check_rights(User) -> + {_, _, Rights} = ask(User, '_'), Rights. - |