From c252f06b16ac1668355fb011c3294e2dd8c6461d Mon Sep 17 00:00:00 2001 From: realaravinth Date: Sat, 19 Feb 2022 21:32:48 +0530 Subject: [PATCH] feat: return comment ID after comment creation --- database/db-core/src/lib.rs | 6 ++-- database/db-core/src/tests.rs | 4 ++- database/db-sqlx-postgres/sqlx-data.json | 23 ++++++++++++++ database/db-sqlx-postgres/src/lib.rs | 40 ++++++++++++++++++++++-- database/db-sqlx-sqlite/sqlx-data.json | 18 +++++++++++ database/db-sqlx-sqlite/src/lib.rs | 37 ++++++++++++++++++++-- 6 files changed, 120 insertions(+), 8 deletions(-) diff --git a/database/db-core/src/lib.rs b/database/db-core/src/lib.rs index 6e28297..d52e27b 100644 --- a/database/db-core/src/lib.rs +++ b/database/db-core/src/lib.rs @@ -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; /// Get comments on a gist async fn get_comments_on_gist(&self, public_id: &str) -> DBResult>; /// Get a specific comment using its database assigned ID @@ -364,7 +364,7 @@ impl GistDatabase for Box { (**self).delete_gist(owner, public_id).await } - async fn new_comment(&self, comment: &CreateGistComment) -> DBResult<()> { + async fn new_comment(&self, comment: &CreateGistComment) -> DBResult { (**self).new_comment(comment).await } diff --git a/database/db-core/src/tests.rs b/database/db-core/src/tests.rs index 03745a0..52fe029 100644 --- a/database/db-core/src/tests.rs +++ b/database/db-core/src/tests.rs @@ -136,7 +136,7 @@ pub async fn gists_work( 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( // 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(); diff --git a/database/db-sqlx-postgres/sqlx-data.json b/database/db-sqlx-postgres/sqlx-data.json index 584dba1..442ee19 100644 --- a/database/db-sqlx-postgres/sqlx-data.json +++ b/database/db-sqlx-postgres/sqlx-data.json @@ -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": { diff --git a/database/db-sqlx-postgres/src/lib.rs b/database/db-sqlx-postgres/src/lib.rs index 8991f16..8fae2e2 100644 --- a/database/db-sqlx-postgres/src/lib.rs +++ b/database/db-sqlx-postgres/src/lib.rs @@ -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 { 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, + } + + 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> { diff --git a/database/db-sqlx-sqlite/sqlx-data.json b/database/db-sqlx-sqlite/sqlx-data.json index d4b0b92..f0ac94b 100644 --- a/database/db-sqlx-sqlite/sqlx-data.json +++ b/database/db-sqlx-sqlite/sqlx-data.json @@ -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": { diff --git a/database/db-sqlx-sqlite/src/lib.rs b/database/db-sqlx-sqlite/src/lib.rs index 42fe23e..5351dd3 100644 --- a/database/db-sqlx-sqlite/src/lib.rs +++ b/database/db-sqlx-sqlite/src/lib.rs @@ -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 { 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> {