feat: return comment ID after comment creation

master
Aravinth Manivannan 2022-02-19 21:32:48 +05:30
parent ae08828ac4
commit c252f06b16
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
6 changed files with 120 additions and 8 deletions

View File

@ -268,8 +268,8 @@ pub trait GistDatabase: std::marker::Send + std::marker::Sync + CloneGistDatabas
/// Delete gist
async fn delete_gist(&self, owner: &str, public_id: &str) -> DBResult<()>;
/// Create new comment
async fn new_comment(&self, comment: &CreateGistComment) -> DBResult<()>;
/// Create new comment, returns database ID of the newly created comment
async fn new_comment(&self, comment: &CreateGistComment) -> DBResult<i64>;
/// Get comments on a gist
async fn get_comments_on_gist(&self, public_id: &str) -> DBResult<Vec<GistComment>>;
/// Get a specific comment using its database assigned ID
@ -364,7 +364,7 @@ impl GistDatabase for Box<dyn GistDatabase> {
(**self).delete_gist(owner, public_id).await
}
async fn new_comment(&self, comment: &CreateGistComment) -> DBResult<()> {
async fn new_comment(&self, comment: &CreateGistComment) -> DBResult<i64> {
(**self).new_comment(comment).await
}

View File

@ -136,7 +136,7 @@ pub async fn gists_work<T: GistDatabase>(
gist_public_id: create_gist.public_id,
comment: "foo",
};
db.new_comment(&create_comment).await.unwrap();
let comment_id = db.new_comment(&create_comment).await.unwrap();
// get all comments on gist
let mut comments = db
.get_comments_on_gist(create_gist.public_id)
@ -149,6 +149,8 @@ pub async fn gists_work<T: GistDatabase>(
// get all comments by ID
let comment = db.get_comment_by_id(comment.id).await.unwrap();
assert_comments(&create_comment, &comment);
let comment_from_id = db.get_comment_by_id(comment_id).await.unwrap();
assert_comments(&create_comment, &comment_from_id);
// delete comment
db.delete_comment(username, comment.id).await.unwrap();

View File

@ -206,6 +206,29 @@
"nullable": []
}
},
"564376f79920fa30c6acb660b30fa3e45e1502c91ad4dafbb46709087550f296": {
"query": "\n SELECT\n ID\n FROM\n gists_comments_view\n WHERE\n owner = $1\n AND\n gist_public_id = $2\n AND\n created = $3\n AND\n comment = $4;\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Int4"
}
],
"parameters": {
"Left": [
"Text",
"Text",
"Timestamptz",
"Text"
]
},
"nullable": [
true
]
}
},
"7cc18cdd39aa42dcbb75b0b0d06b6df05ac654654b86db71be07344e3f09510d": {
"query": "UPDATE gists_users set username = $1 WHERE username = $2",
"describe": {

View File

@ -505,7 +505,7 @@ impl GistDatabase for Database {
}
/// Create new comment
async fn new_comment(&self, comment: &CreateGistComment) -> DBResult<()> {
async fn new_comment(&self, comment: &CreateGistComment) -> DBResult<i64> {
let now = OffsetDateTime::now_utc();
sqlx::query!(
"INSERT INTO gists_comments (owner_id, gist_id, comment, created)
@ -523,7 +523,43 @@ impl GistDatabase for Database {
.execute(&self.pool)
.await
.map_err(map_register_err)?;
Ok(())
struct ID {
id: Option<i32>,
}
let res = sqlx::query_as!(
ID,
"
SELECT
ID
FROM
gists_comments_view
WHERE
owner = $1
AND
gist_public_id = $2
AND
created = $3
AND
comment = $4;
",
comment.owner,
comment.gist_public_id,
&now,
comment.comment,
)
.fetch_one(&self.pool)
.await
.map_err(|e| match e {
Error::RowNotFound => DBError::CommentNotFound,
e => DBError::DBError(Box::new(e)),
})?;
match res.id {
None => Err(DBError::CommentNotFound),
Some(id) => Ok(id as i64),
}
}
/// Get comments on a gist
async fn get_comments_on_gist(&self, public_id: &str) -> DBResult<Vec<GistComment>> {

View File

@ -186,6 +186,24 @@
]
}
},
"39fd5eb5cc62c26e62a2c6673ea70319cb0c8c31d1022a0cada3cdd5ad8438be": {
"query": "\n SELECT\n ID\n FROM\n gists_comments_view\n WHERE\n owner = $1\n AND\n gist_public_id = $2\n AND\n created = $3\n AND\n comment = $4\n ",
"describe": {
"columns": [
{
"name": "ID",
"ordinal": 0,
"type_info": "Int64"
}
],
"parameters": {
"Right": 4
},
"nullable": [
false
]
}
},
"405772009a7aee0194b6b25c42955c2674c3ff92b812d7f15c4075d243879c60": {
"query": "SELECT password FROM gists_users WHERE username = ($1)",
"describe": {

View File

@ -468,7 +468,7 @@ impl GistDatabase for Database {
}
/// Create new comment
async fn new_comment(&self, comment: &CreateGistComment) -> DBResult<()> {
async fn new_comment(&self, comment: &CreateGistComment) -> DBResult<i64> {
let now = now_unix_time_stamp();
sqlx::query!(
"INSERT INTO gists_comments (owner_id, gist_id, comment, created)
@ -486,7 +486,40 @@ impl GistDatabase for Database {
.execute(&self.pool)
.await
.map_err(map_register_err)?;
Ok(())
#[allow(non_snake_case)]
struct ID {
ID: i64,
}
let res = sqlx::query_as!(
ID,
"
SELECT
ID
FROM
gists_comments_view
WHERE
owner = $1
AND
gist_public_id = $2
AND
created = $3
AND
comment = $4
",
comment.owner,
comment.gist_public_id,
now,
comment.comment,
)
.fetch_one(&self.pool)
.await
.map_err(|e| match e {
Error::RowNotFound => DBError::CommentNotFound,
e => DBError::DBError(Box::new(e)),
})?;
Ok(res.ID)
}
/// Get comments on a gist
async fn get_comments_on_gist(&self, public_id: &str) -> DBResult<Vec<GistComment>> {