aboutsummaryrefslogtreecommitdiffstats
path: root/server/cldb.erl
blob: 3886685dc1e332e10804730ab9cc2c2923b9a553 (plain) (blame)
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
% The clientdatabase

-module(cldb).
-export([init/0]).
-record(user, {name, passwd, votes, rights}).

init() ->
    mnesia:create_schema([node()]),
    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, '_'})
        end,
    mnesia:transaction(F).
    
                
    
login(User, Pwd) ->
    {_, Reason} = ask(User, Pwd),
    Reason.

register(User, Pwd, Root) ->
    F = fun() ->
                mnesia:write(#user{name = User, passwd = Pwd, votes = 5, rights = Root})
        end,
    {_, Reason} = mnesia:transaction(F),
    Reason.

decVote(User, Pwd) ->
    F = fun() ->
                [Head|_] = ask(User, Pwd),
                Votes = Head#user.votes - 1,
                New = Head#user{votes = Votes},
                mnesia:write(New)
        end,
    mnesia:transaction(F).

incVote(User, Pwd) ->
    F = fun() ->
                [Head|_] = ask(User, Pwd),
                Votes = Head#user.votes + 1,
                New = Head#user{votes = Votes},
                mnesia:write(New)
        end,
    mnesia:transaction(F).