Skip to content

Commit 9108fc6

Browse files
committed
sentry - db - channel - insert_channel
1 parent b6003be commit 9108fc6

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

sentry/src/db/channel.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,22 @@ pub async fn get_channel_by_id_and_validator(
4545
})
4646
.await
4747
}
48+
49+
pub async fn insert_channel(pool: &DbPool, channel: &Channel) -> Result<bool, RunError<bb8_postgres::tokio_postgres::Error>> {
50+
pool
51+
.run(move |connection| {
52+
async move {
53+
match connection.prepare("INSERT INTO channels (id, creator, deposit_asset, deposit_amount, valid_until, spec) values ($1, $2, $3, $4, $5, $6)").await {
54+
Ok(stmt) => match connection.execute(&stmt, &[&channel.id, &channel.creator, &channel.deposit_asset, &channel.deposit_amount, &channel.valid_until, &channel.spec]).await {
55+
Ok(row) => {
56+
let inserted = row == 1;
57+
Ok((inserted, connection))
58+
},
59+
Err(e) => Err((e, connection)),
60+
},
61+
Err(e) => Err((e, connection)),
62+
}
63+
}
64+
})
65+
.await
66+
}

sentry/src/routes/channel.rs

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use self::channel_list::ChannelListQuery;
2-
use crate::db::get_channel_by_id;
2+
use crate::db::{get_channel_by_id, insert_channel};
33
use crate::success_response;
44
use crate::Application;
55
use crate::ResponseError;
@@ -22,7 +22,6 @@ pub async fn channel_status<A: Adapter>(
2222
channel: &'a Channel,
2323
}
2424

25-
// get request params
2625
let channel = req
2726
.extensions()
2827
.get::<Channel>()
@@ -45,29 +44,18 @@ pub async fn create_channel<A: Adapter>(
4544
return Err(ResponseError::BadRequest(e.to_string()));
4645
}
4746

48-
let spec = serde_json::to_string(&channel.spec)?;
49-
let result = app.pool
50-
.run(move |connection| {
51-
async move {
52-
match connection.prepare("INSERT INTO channels (channel_id, creator, deposit_asset, deposit_amount, valid_until, spec) values ($1, $2, $3, $4, $5, $6)").await {
53-
Ok(stmt) => match connection.execute(&stmt, &[&channel.id, &channel.creator, &channel.deposit_asset, &channel.deposit_amount, &channel.valid_until, &spec]).await {
54-
Ok(row) => {
55-
Ok((row, connection))
56-
},
57-
Err(e) => Err((e, connection)),
58-
},
59-
Err(e) => Err((e, connection)),
60-
}
61-
}
62-
})
63-
.await;
64-
65-
if let Err(err) = result {
66-
error!(&app.logger, "{}", &err; "module" => "create_channel");
67-
return Err(ResponseError::BadRequest(
68-
"err occured; please try again later".into(),
69-
));
70-
}
47+
match insert_channel(&app.pool, &channel).await {
48+
Err(err) => {
49+
error!(&app.logger, "{}", &err; "module" => "create_channel");
50+
Err(ResponseError::BadRequest(
51+
"err occurred; please try again later".into(),
52+
))
53+
},
54+
Ok(false) => Err(ResponseError::BadRequest(
55+
"err occurred; please try again later".into(),
56+
)),
57+
_ => Ok(())
58+
}?;
7159

7260
let create_response = SuccessResponse { success: true };
7361

0 commit comments

Comments
 (0)