use postgresql
This commit is contained in:
@@ -18,11 +18,12 @@ pub async fn delete(di: Form<DeleteInput>, user: CurrentUser, db: Db) -> API<Val
|
||||
"cid" => {
|
||||
let c = Comment::get(&db, di.id).await.m()?;
|
||||
c.soft_delete(&user, &db).await?;
|
||||
let p = Post::get(&db, c.post_id).await.m()?;
|
||||
p.change_n_comments(&db, -1).await.m()?;
|
||||
}
|
||||
"pid" => {
|
||||
let p = Post::get(&db, di.id).await.m()?;
|
||||
p.soft_delete(&user, &db).await?;
|
||||
p.change_n_comments(&db, -1).await.m()?;
|
||||
}
|
||||
_ => return Err(APIError::PcError(NotAllowed)),
|
||||
}
|
||||
|
||||
@@ -140,7 +140,7 @@ pub async fn get_list(
|
||||
|
||||
#[post("/dopost", data = "<poi>")]
|
||||
pub async fn publish_post(poi: Form<PostInput>, user: CurrentUser, db: Db) -> JsonAPI {
|
||||
let r = Post::create(
|
||||
let p = Post::create(
|
||||
&db,
|
||||
NewPost {
|
||||
content: poi.text.to_string(),
|
||||
@@ -156,7 +156,6 @@ pub async fn publish_post(poi: Form<PostInput>, user: CurrentUser, db: Db) -> Js
|
||||
.m()?;
|
||||
// TODO: attention
|
||||
Ok(json!({
|
||||
"data": r,
|
||||
"code": 0
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use rocket_sync_db_pools::{database, diesel};
|
||||
|
||||
pub type Conn = diesel::SqliteConnection;
|
||||
pub type Conn = diesel::pg::PgConnection;
|
||||
|
||||
#[database("sqlite_v2")]
|
||||
#[database("pg_v2")]
|
||||
pub struct Db(Conn);
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ macro_rules! get {
|
||||
macro_rules! get_multi {
|
||||
($table:ident) => {
|
||||
pub async fn get_multi(db: &Db, ids: Vec<i32>) -> QueryResult<Vec<Self>> {
|
||||
// can use eq(any()) for postgres
|
||||
db.run(move |c| $table::table.filter($table::id.eq_any(ids)).load(c))
|
||||
.await
|
||||
}
|
||||
@@ -106,9 +107,9 @@ impl Post {
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn create(db: &Db, new_post: NewPost) -> QueryResult<usize> {
|
||||
pub async fn create(db: &Db, new_post: NewPost) -> QueryResult<Self> {
|
||||
// TODO: tags
|
||||
db.run(move |c| insert_into(posts::table).values(&new_post).execute(c))
|
||||
db.run(move |c| insert_into(posts::table).values(&new_post).get_result(c))
|
||||
.await
|
||||
}
|
||||
|
||||
@@ -169,6 +170,7 @@ pub struct Comment {
|
||||
pub content: String,
|
||||
pub create_time: NaiveDateTime,
|
||||
pub is_deleted: bool,
|
||||
pub allow_search: bool,
|
||||
pub post_id: i32,
|
||||
}
|
||||
|
||||
@@ -187,9 +189,13 @@ impl Comment {
|
||||
|
||||
set_deleted!(comments);
|
||||
|
||||
pub async fn create(db: &Db, new_comment: NewComment) -> QueryResult<usize> {
|
||||
db.run(move |c| insert_into(comments::table).values(&new_comment).execute(c))
|
||||
.await
|
||||
pub async fn create(db: &Db, new_comment: NewComment) -> QueryResult<Self> {
|
||||
db.run(move |c| {
|
||||
insert_into(comments::table)
|
||||
.values(&new_comment)
|
||||
.get_result(c)
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn gets_by_post_id(db: &Db, post_id: i32) -> QueryResult<Vec<Self>> {
|
||||
|
||||
@@ -1,40 +1,41 @@
|
||||
table! {
|
||||
comments (id) {
|
||||
id -> Integer,
|
||||
author_hash -> Text,
|
||||
author_title -> Text,
|
||||
id -> Int4,
|
||||
author_hash -> Varchar,
|
||||
author_title -> Varchar,
|
||||
is_tmp -> Bool,
|
||||
content -> Text,
|
||||
create_time -> Timestamp,
|
||||
is_deleted -> Bool,
|
||||
post_id -> Integer,
|
||||
allow_search -> Bool,
|
||||
post_id -> Int4,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
posts (id) {
|
||||
id -> Integer,
|
||||
author_hash -> Text,
|
||||
id -> Int4,
|
||||
author_hash -> Varchar,
|
||||
content -> Text,
|
||||
cw -> Text,
|
||||
author_title -> Text,
|
||||
cw -> Varchar,
|
||||
author_title -> Varchar,
|
||||
is_tmp -> Bool,
|
||||
n_attentions -> Integer,
|
||||
n_comments -> Integer,
|
||||
n_attentions -> Int4,
|
||||
n_comments -> Int4,
|
||||
create_time -> Timestamp,
|
||||
last_comment_time -> Timestamp,
|
||||
is_deleted -> Bool,
|
||||
is_reported -> Bool,
|
||||
hot_score -> Integer,
|
||||
hot_score -> Int4,
|
||||
allow_search -> Bool,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
users (id) {
|
||||
id -> Integer,
|
||||
name -> Text,
|
||||
token -> Text,
|
||||
id -> Int4,
|
||||
name -> Varchar,
|
||||
token -> Varchar,
|
||||
is_admin -> Bool,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user