1
+ use sea_orm:: { entity:: * , query:: * , DbErr } ;
2
+ use crate :: models:: allocators:: { Column , ActiveModel , Entity as Allocator , Model as AllocatorModel } ;
3
+ use crate :: get_database_connection;
4
+
5
+ /**
6
+ * Get all allocators from the database
7
+ *
8
+ * # Returns
9
+ * @return Result<Vec<AllocatorModel>, sea_orm::DbErr> - The result of the operation
10
+ */
11
+ pub async fn get_allocators ( ) ->Result < Vec < AllocatorModel > , sea_orm:: DbErr > {
12
+ let conn = get_database_connection ( ) . await ?;
13
+ Allocator :: find ( ) . all ( & conn) . await
14
+ }
15
+
16
+ /**
17
+ * Update an allocator in the database
18
+ *
19
+ * # Arguments
20
+ * @param owner: &str - The owner of the repository
21
+ * @param repo: &str - The repository name
22
+ * @param installation_id: Option<i64> - The installation ID
23
+ * @param multisig_address: Option<String> - The multisig address
24
+ * @param verifiers_gh_handles: Option<String> - The GitHub handles of the verifiers
25
+ *
26
+ * # Returns
27
+ * @return Result<AllocatorModel, sea_orm::DbErr> - The result of the operation
28
+ */
29
+ pub async fn update_allocator (
30
+ owner : & str ,
31
+ repo : & str ,
32
+ installation_id : Option < i64 > ,
33
+ multisig_address : Option < String > ,
34
+ verifiers_gh_handles : Option < String > ,
35
+ ) -> Result < AllocatorModel , sea_orm:: DbErr > {
36
+ let conn = get_database_connection ( ) . await ?;
37
+
38
+ let existing_allocator = get_allocator ( owner, repo) . await ?;
39
+ if let Some ( allocator_model) = existing_allocator {
40
+ let mut allocator_active_model = allocator_model. into_active_model ( ) ;
41
+
42
+ allocator_active_model. installation_id = Set ( installation_id) ;
43
+ allocator_active_model. multisig_address = Set ( multisig_address) ;
44
+ allocator_active_model. verifiers_gh_handles = Set ( verifiers_gh_handles) ;
45
+
46
+ let updated_model = allocator_active_model. update ( & conn) . await ?;
47
+
48
+ Ok ( updated_model)
49
+ } else {
50
+ Err ( DbErr :: Custom ( format ! ( "Allocator not found" ) . into ( ) ) )
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Get an allocator from the database
56
+ *
57
+ * # Arguments
58
+ * @param owner: &str - The owner of the repository
59
+ * @param repo: &str - The repository name
60
+ *
61
+ * # Returns
62
+ * @return Result<Option<AllocatorModel>, sea_orm::DbErr> - The result of the operation
63
+ */
64
+ pub async fn get_allocator (
65
+ owner : & str ,
66
+ repo : & str ,
67
+ ) -> Result < Option < AllocatorModel > , sea_orm:: DbErr > {
68
+ let conn = get_database_connection ( ) . await ?;
69
+ Allocator :: find ( )
70
+ . filter ( Column :: Owner . eq ( owner) )
71
+ . filter ( Column :: Repo . eq ( repo) )
72
+ . one ( & conn)
73
+ . await
74
+ }
75
+
76
+ /**
77
+ * Creates or updates an allocator in the database
78
+ *
79
+ * # Arguments
80
+ * @param owner: String - The owner of the repository
81
+ * @param repo: String - The repository name
82
+ * @param installation_id: Option<i64> - The installation ID
83
+ * @param multisig_address: Option<String> - The multisig address
84
+ * @param verifiers_gh_handles: Option<String> - The GitHub handles of the verifiers
85
+ *
86
+ * # Returns
87
+ * @return Result<AllocatorModel, sea_orm::DbErr> - The result of the operation
88
+ */
89
+ pub async fn create_or_update_allocator (
90
+ owner : String ,
91
+ repo : String ,
92
+ installation_id : Option < i64 > ,
93
+ multisig_address : Option < String > ,
94
+ verifiers_gh_handles : Option < String > ,
95
+ ) -> Result < AllocatorModel , sea_orm:: DbErr > {
96
+
97
+ let existing_allocator = get_allocator ( & owner, & repo) . await ?;
98
+ if let Some ( allocator_model) = existing_allocator {
99
+ let conn = get_database_connection ( ) . await ?;
100
+ let mut allocator_active_model = allocator_model. into_active_model ( ) ;
101
+
102
+ allocator_active_model. installation_id = Set ( installation_id) ;
103
+ allocator_active_model. multisig_address = Set ( multisig_address) ;
104
+ allocator_active_model. verifiers_gh_handles = Set ( verifiers_gh_handles) ;
105
+
106
+ let updated_model = allocator_active_model. update ( & conn) . await ?;
107
+
108
+ Ok ( updated_model)
109
+ } else {
110
+ let new_allocator = ActiveModel {
111
+ owner : Set ( owner) ,
112
+ repo : Set ( repo) ,
113
+ installation_id : Set ( installation_id) ,
114
+ multisig_address : Set ( multisig_address) ,
115
+ verifiers_gh_handles : Set ( verifiers_gh_handles) ,
116
+ ..Default :: default ( )
117
+ } ;
118
+
119
+ let conn = get_database_connection ( ) . await . expect ( "Failed to get DB connection" ) ;
120
+ new_allocator. insert ( & conn) . await
121
+ }
122
+ }
123
+
124
+ /**
125
+ * Delete an allocator from the database
126
+ *
127
+ * # Arguments
128
+ * @param owner: &str - The owner of the repository
129
+ * @param repo: &str - The repository name
130
+ *
131
+ * # Returns
132
+ * @return Result<(), sea_orm::DbErr> - The result of the operation
133
+ */
134
+ pub async fn delete_allocator (
135
+ owner : & str ,
136
+ repo : & str ,
137
+ ) -> Result < ( ) , sea_orm:: DbErr > {
138
+ let conn = get_database_connection ( ) . await ?;
139
+ let allocator = get_allocator ( owner, repo) . await ?;
140
+ let allocator = match allocator {
141
+ Some ( allocator) => allocator,
142
+ None => return Err ( DbErr :: Custom ( format ! ( "Allocator not found" ) . into ( ) ) ) ,
143
+ } ;
144
+ allocator. delete ( & conn) . await ?;
145
+ Ok ( ( ) )
146
+ }
0 commit comments