1
+ use sea_orm:: { entity:: * , query:: * , DbErr } ;
2
+ use crate :: models:: applications:: { Column , ActiveModel , Entity as Application , Model as ApplicationModel } ;
3
+ use crate :: get_database_connection;
4
+
5
+ /**
6
+ * Get all applications from the database
7
+ *
8
+ * # Returns
9
+ * @return Result<Vec<ApplicationModel>, sea_orm::DbErr> - The result of the operation
10
+ */
11
+ pub async fn get_applications ( ) ->Result < Vec < ApplicationModel > , sea_orm:: DbErr > {
12
+ let conn = get_database_connection ( ) . await ?;
13
+ Application :: find ( ) . all ( & conn) . await
14
+ }
15
+
16
+ /**
17
+ * Get merged applications from the database
18
+ *
19
+ * # Arguments
20
+ * @param owner: String - The owner of the repository
21
+ * @param repo: String - The repository name
22
+ *
23
+ * # Returns
24
+ * @return Result<Vec<ApplicationModel>, sea_orm::DbErr> - The result of the operation
25
+ */
26
+ pub async fn get_merged_applications ( owner : String , repo : String ) -> Result < Vec < ApplicationModel > , sea_orm:: DbErr > {
27
+ let conn = get_database_connection ( ) . await ?;
28
+ Application :: find ( )
29
+ . filter ( Column :: Owner . contains ( owner) )
30
+ . filter ( Column :: Repo . contains ( repo) )
31
+ . filter ( Column :: PrNumber . eq ( 0 ) )
32
+ . all ( & conn)
33
+ . await
34
+ }
35
+
36
+ /**
37
+ * Get active applications from the database
38
+ *
39
+ * # Arguments
40
+ * @param owner: String - The owner of the repository
41
+ * @param repo: String - The repository name
42
+ *
43
+ * # Returns
44
+ * @return Result<Vec<ApplicationModel>, sea_orm::DbErr> - The result of the operation
45
+ */
46
+ pub async fn get_active_applications ( owner : String , repo : String ) -> Result < Vec < ApplicationModel > , sea_orm:: DbErr > {
47
+ let conn = get_database_connection ( ) . await ?;
48
+ Application :: find ( )
49
+ . filter ( Column :: Owner . contains ( owner) )
50
+ . filter ( Column :: Repo . contains ( repo) )
51
+ . filter ( Column :: PrNumber . ne ( 0 ) )
52
+ . all ( & conn)
53
+ . await
54
+ }
55
+
56
+ /**
57
+ * Get an application from the database with max pr_number for given id, owner and repo
58
+ *
59
+ * # Arguments
60
+ * @param id: String - The ID of the application
61
+ * @param owner: String - The owner of the repository
62
+ * @param repo: String - The repository name
63
+ * @param pr_number: Option<u64> - Optional PR number to filter by
64
+ *
65
+ * # Returns
66
+ * @return Result<ApplicationModel, sea_orm::DbErr> - The result of the operation
67
+ */
68
+ pub async fn get_application ( id : String , owner : String , repo : String , pr_number : Option < u64 > ) -> Result < ApplicationModel , sea_orm:: DbErr > {
69
+ let conn = get_database_connection ( ) . await ?;
70
+ let mut query = Application :: find ( )
71
+ . filter ( Column :: Id . eq ( id) )
72
+ . filter ( Column :: Owner . contains ( owner) )
73
+ . filter ( Column :: Repo . contains ( repo) ) ;
74
+ if let Some ( number) = pr_number {
75
+ query = query. filter ( Column :: PrNumber . eq ( number as i64 ) ) ;
76
+ }
77
+
78
+ let result = query
79
+ . order_by ( Column :: PrNumber , Order :: Desc )
80
+ . one ( & conn)
81
+ . await ?;
82
+
83
+ match result {
84
+ Some ( application) => Ok ( application) ,
85
+ None => return Err ( DbErr :: Custom ( format ! ( "Application not found" ) . into ( ) ) ) ,
86
+ }
87
+ }
88
+
89
+ /**
90
+ * Get an application from the database with given pr_number
91
+ *
92
+ * # Arguments
93
+ * @param owner: String - The owner of the repository
94
+ * @param repo: String - The repository name
95
+ * @param pr_number: u64 - The PR number
96
+ *
97
+ * # Returns
98
+ * @return Result<ApplicationModel, sea_orm::DbErr> - The result of the operation
99
+ */
100
+ pub async fn get_application_by_pr_number ( owner : String , repo : String , pr_number : u64 ) -> Result < ApplicationModel , sea_orm:: DbErr > {
101
+ let conn = get_database_connection ( ) . await ?;
102
+ let result = Application :: find ( )
103
+ . filter ( Column :: Owner . contains ( owner) )
104
+ . filter ( Column :: Repo . contains ( repo) )
105
+ . filter ( Column :: PrNumber . eq ( pr_number as i64 ) )
106
+ . one ( & conn)
107
+ . await ?;
108
+
109
+ match result {
110
+ Some ( application) => Ok ( application) ,
111
+ None => return Err ( DbErr :: Custom ( format ! ( "Application not found" ) . into ( ) ) ) ,
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Merge an application in the database
117
+ *
118
+ * # Arguments
119
+ * @param owner: String - The owner of the repository
120
+ * @param repo: String - The repository name
121
+ * @param pr_number: u64 - The PR number
122
+ *
123
+ * # Returns
124
+ * @return Result<ApplicationModel, sea_orm::DbErr> - The result of the operation
125
+ */
126
+ pub async fn merge_application_by_pr_number ( owner : String , repo : String , pr_number : u64 ) -> Result < ApplicationModel , sea_orm:: DbErr > {
127
+ let conn = get_database_connection ( ) . await ?;
128
+ let existing_application = get_application_by_pr_number ( owner. clone ( ) , repo. clone ( ) , pr_number) . await ?;
129
+ let mut active_application = existing_application. into_active_model ( ) ;
130
+
131
+ let application_id = if let Some ( id) = active_application. id . take ( ) {
132
+ id
133
+ } else {
134
+ return Err ( DbErr :: Custom ( "Application ID is not set" . into ( ) ) ) ;
135
+ } ;
136
+
137
+ // Delete the application with pr_number 0
138
+ Application :: delete_many ( )
139
+ . filter ( Column :: Id . eq ( application_id) )
140
+ . filter ( Column :: Owner . contains ( owner) )
141
+ . filter ( Column :: Repo . contains ( repo) )
142
+ . filter ( Column :: PrNumber . eq ( 0 ) )
143
+ . exec ( & conn)
144
+ . await ?;
145
+
146
+ // Update the application and set pr_number to 0
147
+ active_application. pr_number = Set ( 0 ) ;
148
+ let updated_application = active_application. update ( & conn) . await ?;
149
+ Ok ( updated_application)
150
+ }
151
+
152
+ /**
153
+ * Update an application in the database
154
+ *
155
+ * # Arguments
156
+ * @param id: String - The ID of the application
157
+ * @param owner: String - The owner of the repository
158
+ * @param repo: String - The repository name
159
+ * @param pr_number: u64 - The PR number
160
+ * @param app_file: String - The application file
161
+ * @param sha: Option<String> - The SHA of the application
162
+ * @param path: Option<String> - The path of the application
163
+ *
164
+ * # Returns
165
+ * @return Result<ApplicationModel, sea_orm::DbErr> - The result of the operation
166
+ */
167
+ pub async fn update_application ( id : String , owner : String , repo : String , pr_number : u64 , app_file : String , sha : Option < String > , path : Option < String > ) -> Result < ApplicationModel , sea_orm:: DbErr > {
168
+ let conn = get_database_connection ( ) . await ?;
169
+
170
+ match get_application ( id. clone ( ) , owner. clone ( ) , repo. clone ( ) , Some ( pr_number) ) . await {
171
+ Ok ( existing_application) => {
172
+ let mut active_application = existing_application. into_active_model ( ) ;
173
+ active_application. application = Set ( Some ( app_file) ) ;
174
+ // If sha and path are provided, update them as well
175
+ if let Some ( sha) = sha {
176
+ active_application. sha = Set ( Some ( sha) ) ;
177
+ }
178
+ if let Some ( path) = path {
179
+ active_application. path = Set ( Some ( path) ) ;
180
+ }
181
+ let updated_application = active_application. update ( & conn) . await ?;
182
+ Ok ( updated_application)
183
+ } ,
184
+ Err ( _) => {
185
+ Err ( sea_orm:: DbErr :: Custom ( "Failed to find the application to update." . into ( ) ) )
186
+ }
187
+ }
188
+ }
189
+
190
+ /**
191
+ * Create an application in the database
192
+ *
193
+ * # Arguments
194
+ * @param id: String - The ID of the application
195
+ * @param owner: String - The owner of the repository
196
+ * @param repo: String - The repository name
197
+ * @param pr_number: u64 - The PR number
198
+ * @param app_file: String - The application file
199
+ * @param sha: String - The SHA of the application
200
+ * @param path: String - The path of the application
201
+ *
202
+ * # Returns
203
+ * @return Result<ApplicationModel, sea_orm::DbErr> - The result of the operation
204
+ */
205
+ pub async fn create_application ( id : String , owner : String , repo : String , pr_number : u64 , app_file : String , sha : String , path : String ) -> Result < ApplicationModel , sea_orm:: DbErr > {
206
+ let conn = get_database_connection ( ) . await ?;
207
+
208
+ let new_application = ActiveModel {
209
+ id : Set ( id) ,
210
+ owner : Set ( owner) ,
211
+ repo : Set ( repo) ,
212
+ pr_number : Set ( pr_number as i64 ) ,
213
+ application : Set ( Some ( app_file) ) ,
214
+ sha : Set ( Some ( sha) ) ,
215
+ path : Set ( Some ( path) ) ,
216
+ ..Default :: default ( )
217
+ } ;
218
+
219
+ let result = match new_application. insert ( & conn) . await {
220
+ Ok ( application) => Ok ( application) ,
221
+ Err ( e) => Err ( sea_orm:: DbErr :: Custom ( format ! ( "Failed to insert new application: {}" , e) ) ) ,
222
+ } ;
223
+
224
+ result
225
+ }
226
+
227
+ /**
228
+ * Delete an application from the database
229
+ *
230
+ * # Arguments
231
+ * @param id: String - The ID of the application
232
+ * @param owner: String - The owner of the repository
233
+ * @param repo: String - The repository name
234
+ * @param pr_number: u64 - The PR number
235
+ *
236
+ * # Returns
237
+ * @return Result<(), sea_orm::DbErr> - The result of the operation
238
+ */
239
+ pub async fn delete_application ( id : String , owner : String , repo : String , pr_number : u64 ) -> Result < ( ) , sea_orm:: DbErr > {
240
+ let conn = get_database_connection ( ) . await ?;
241
+ let application = get_application ( id. clone ( ) , owner. clone ( ) , repo. clone ( ) , Some ( pr_number) ) . await ?;
242
+ application. delete ( & conn) . await ?;
243
+ Ok ( ( ) )
244
+ }
245
+
0 commit comments