Skip to content

Commit 1bd37cd

Browse files
authored
Merge pull request #119 from filecoin-project/feat/create-or-update-allocator
modify create -> create or update allocator
2 parents 9c75884 + 3e6e052 commit 1bd37cd

File tree

4 files changed

+31
-23
lines changed

4 files changed

+31
-23
lines changed

fplus-database/src/database/mod.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub async fn get_allocator(
7474
}
7575

7676
/**
77-
* Create an allocator in the database
77+
* Creates or updates an allocator in the database
7878
*
7979
* # Arguments
8080
* @param owner: String - The owner of the repository
@@ -86,7 +86,7 @@ pub async fn get_allocator(
8686
* # Returns
8787
* @return Result<AllocatorModel, sea_orm::DbErr> - The result of the operation
8888
*/
89-
pub async fn create_allocator(
89+
pub async fn create_or_update_allocator(
9090
owner: String,
9191
repo: String,
9292
installation_id: Option<i64>,
@@ -95,21 +95,30 @@ pub async fn create_allocator(
9595
) -> Result<AllocatorModel, sea_orm::DbErr> {
9696

9797
let existing_allocator = get_allocator(&owner, &repo).await?;
98-
match existing_allocator {
99-
Some(_) => return Err(DbErr::Custom(format!("Allocator already exists").into())),
100-
None => (),
101-
};
102-
let new_allocator = ActiveModel {
103-
owner: Set(owner),
104-
repo: Set(repo),
105-
installation_id: Set(installation_id),
106-
multisig_address: Set(multisig_address),
107-
verifiers_gh_handles: Set(verifiers_gh_handles),
108-
..Default::default()
109-
};
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();
110101

111-
let conn = get_database_connection().await.expect("Failed to get DB connection");
112-
new_allocator.insert(&conn).await
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+
}
113122
}
114123

115124
/**

fplus-database/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ mod tests {
103103
let multisig_address = Some("0x1234567890".to_string());
104104
let verifiers_gh_handles = Some("test_verifier_1, test_verifier_2".to_string());
105105

106-
let result = database::create_allocator(
106+
let result = database::create_or_update_allocator(
107107
owner,
108108
repo,
109109
installation_id,
@@ -150,7 +150,7 @@ mod tests {
150150
("0x1234567890".to_string());
151151
let verifiers_gh_handles = Some("test_verifier_1, test_verifier_2".to_string());
152152

153-
let result = database::create_allocator(
153+
let result = database::create_or_update_allocator(
154154
owner.clone(),
155155
repo.clone(),
156156
installation_id,
@@ -217,7 +217,7 @@ mod tests {
217217
let multisig_address = Some("0x1234567890".to_string());
218218
let verifiers_gh_handles = Some("test_verifier_1, test_verifier_2".to_string());
219219

220-
let result = database::create_allocator(
220+
let result = database::create_or_update_allocator(
221221
owner.clone(),
222222
repo.clone(),
223223
installation_id,

fplus-http-server/src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ async fn main() -> std::io::Result<()> {
4444
.service(router::blockchain::verified_clients)
4545
.service(router::govteam::gov_team_members)
4646
.service(router::allocator::allocators)
47-
.service(router::allocator::create)
48-
.service(router::allocator::update)
47+
.service(router::allocator::create_or_update)
4948
.service(router::allocator::allocator)
5049
.service(router::allocator::delete)
5150
})

fplus-http-server/src/router/allocator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ pub async fn allocators() -> impl Responder {
3030
* @return HttpResponse - The result of the operation
3131
*/
3232
#[post("/allocator")]
33-
pub async fn create(info: web::Json<Allocator>) -> impl Responder {
34-
match database::create_allocator(
33+
pub async fn create_or_update(info: web::Json<Allocator>) -> impl Responder {
34+
match database::create_or_update_allocator(
3535
info.owner.clone(),
3636
info.repo.clone(),
3737
info.installation_id,

0 commit comments

Comments
 (0)