forked from za-songguo/blog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b6edfd2
commit d7935dd
Showing
9 changed files
with
133 additions
and
22 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod view; | ||
pub mod new; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use ntex::web::types::{Json, State}; | ||
use std::sync::Arc; | ||
|
||
use crate::{ | ||
errors::CustomError, | ||
models::{comment::Comment, user::User}, | ||
AppState, | ||
}; | ||
|
||
/// 新增评论 | ||
/// 需要用户权限 | ||
pub async fn new_comment( | ||
user: User, | ||
comment: Json<Comment>, | ||
state: State<Arc<AppState>>, | ||
) -> Result<String, CustomError> { | ||
let db_pool = &state.db_pool; | ||
|
||
let user_id = user.id; | ||
let article_id = match comment.article { | ||
Some(id) => id, | ||
None => return Err(CustomError::BadRequest("请提供要评论的文章的 ID".into())), | ||
}; | ||
|
||
// 如果要评论的文章不存在 | ||
if sqlx::query!("SELECT id FROM articles WHERE id = $1", article_id as i32) | ||
.fetch_optional(db_pool) | ||
.await? | ||
.is_none() | ||
{ | ||
return Err(CustomError::BadRequest("要评论的文章不存在".into())); | ||
} | ||
|
||
sqlx::query!( | ||
"INSERT INTO comments (user_id, content, article) VALUES ($1, $2, $3)", | ||
user_id, | ||
comment.content, | ||
article_id as i32 | ||
) | ||
.execute(db_pool) | ||
.await?; | ||
|
||
Ok("新增评论成功!".into()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use std::sync::Arc; | ||
|
||
use ntex::web::types::{Json, Path, State}; | ||
|
||
use crate::{ | ||
errors::CustomError, | ||
models::{comment::Comment, user::GithubUserInfo}, | ||
AppState, | ||
}; | ||
/// 通过文章 ID 获取该文章的所有评论(包含发表评论的用户的信息) | ||
pub async fn get_comments_for_article( | ||
article_id: Path<(u32,)>, | ||
state: State<Arc<AppState>>, | ||
) -> Result<Json<Vec<Comment>>, CustomError> { | ||
let db_pool = &state.db_pool; | ||
|
||
let article_id = article_id.0; | ||
|
||
// 查找对应文章的所有评论,拿到它们的 user_id, content, date 和 users 表里相同 user_id(对应的是 users 表里的 id) 的记录的 name, avatar_url | ||
let comments = sqlx::query!( | ||
"SELECT comments.user_id, comments.content, comments.date, users.name, users.avatar_url FROM comments JOIN users ON comments.user_id = users.id WHERE comments.article = $1", article_id as i32 | ||
) | ||
.fetch_all(db_pool) | ||
.await? | ||
.iter() | ||
.map(|i| Comment { | ||
id: None, | ||
user: Some(GithubUserInfo { | ||
id: i.user_id, | ||
login: i.name.clone(), | ||
avatar_url: i.avatar_url.clone(), | ||
}), | ||
content: i.content.clone(), | ||
date: Some(i.date), | ||
article: None, | ||
}) | ||
.collect::<Vec<Comment>>(); | ||
|
||
Ok(Json(comments)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::user::GithubUserInfo; | ||
|
||
/// 评论 | ||
#[derive(Debug, Clone, Deserialize, Serialize)] | ||
pub struct Comment { | ||
// 评论 ID | ||
pub id: Option<u32>, | ||
/// 发表评论的用户的信息 | ||
/// 实现 Serialize 和 Deserialize | ||
pub user: Option<GithubUserInfo>, | ||
/// 评论内容 | ||
pub content: String, | ||
/// 评论日期 | ||
pub date: Option<chrono::NaiveDate>, | ||
/// 评论的文章 | ||
pub article: Option<u32>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod comment; | ||
pub mod user; | ||
pub mod article; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters