Skip to content

Commit 3b3020c

Browse files
committed
Completed applications cache
1 parent c9196d5 commit 3b3020c

File tree

12 files changed

+942
-241
lines changed

12 files changed

+942
-241
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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+
}
Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
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

Comments
 (0)