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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
open Lwt.Syntax
exception No_such_group
type id = Types.Group.id [@@deriving json]
type t = Types.Group.t = {id : id; name : string; desc : string option}
[@@deriving json]
(** Create a group of type [Types.Group.t] using db information. *)
let create_group_from_db (groupid, name, description) : Types.Group.t =
{id = groupid; name; desc = description}
let id_of_group (g : Types.Group.t) = g.id
let name_of_group (g : Types.Group.t) = g.name
let desc_of_group (g : Types.Group.t) = g.desc
module MCache = Request_cache.Make (struct
type key = string
type value = Types.Group.t
let compare = compare
let get key =
Lwt.catch
(fun () ->
let* g = Db.Groups.group_of_name key in
Lwt.return (create_group_from_db g)
)
(function
| Db.No_such_resource -> Lwt.fail No_such_group | exc -> Lwt.reraise exc
)
end)
(** Helper function which creates a new group and return it as
* a record of type [Types.Group.t]. *)
let create ?description name =
let group_of_name name =
let* g = Db.Groups.group_of_name name in
Lwt.return (create_group_from_db g)
in
Lwt.catch
(fun () -> group_of_name name)
(function
| Db.No_such_resource ->
let* () = Db.Groups.create ?description name in
Lwt.catch
(fun () ->
let* g = group_of_name name in
Lwt.return g
)
(function
| Db.No_such_resource -> Lwt.fail No_such_group
| exc -> Lwt.reraise exc
)
| exc -> Lwt.reraise exc
)
(** Overwrite the function [group_of_name] of [Db.Group] and use
* the [get] function of the cache module. *)
let group_of_name = MCache.get
let add_user_in_group ~(group : Types.Group.t) =
Db.Groups.add_user_in_group ~groupid:group.id
let remove_user_in_group ~(group : Types.Group.t) =
Db.Groups.remove_user_in_group ~groupid:group.id
let in_group ?dbh ~(group : Types.Group.t) ~userid () =
Db.Groups.in_group ?dbh ~groupid:group.id ~userid ()
(** Returns all the groups of the database. *)
let all () =
let* groups = Db.Groups.all () in
Lwt.return (List.map create_group_from_db groups)