7 changed files with 240 additions and 36 deletions
@ -0,0 +1,32 @@
|
||||
use crate::api::{APIError, CurrentUser, PolicyError::*, API, UGC}; |
||||
use crate::db_conn::DbConn; |
||||
use crate::models::*; |
||||
use rocket::form::Form; |
||||
use rocket::serde::json::{json, Value}; |
||||
|
||||
#[derive(FromForm)] |
||||
pub struct DeleteInput<'r> { |
||||
#[field(name = "type")] |
||||
id_type: &'r str, |
||||
id: i32, |
||||
note: &'r str, |
||||
} |
||||
|
||||
#[post("/delete", data = "<di>")] |
||||
pub fn delete(di: Form<DeleteInput>, user: CurrentUser, conn: DbConn) -> API<Value> { |
||||
match di.id_type { |
||||
"cid" => { |
||||
let c = Comment::get(&conn, di.id).map_err(APIError::from_db)?; |
||||
c.soft_delete(&user, &conn)?; |
||||
} |
||||
"pid" => { |
||||
let p = Post::get(&conn, di.id).map_err(APIError::from_db)?; |
||||
p.soft_delete(&user, &conn)?; |
||||
} |
||||
_ => return Err(APIError::PcError(NotAllowed)), |
||||
} |
||||
|
||||
Ok(json!({ |
||||
"code": 0 |
||||
})) |
||||
} |
@ -0,0 +1,38 @@
|
||||
use diesel::r2d2::{ConnectionManager, Pool, PooledConnection}; |
||||
use std::env; |
||||
use std::ops::Deref; |
||||
use rocket::http::Status; |
||||
use rocket::request::{FromRequest, Request, Outcome}; |
||||
|
||||
pub type Conn = diesel::SqliteConnection; |
||||
pub type DbPool = Pool<ConnectionManager<Conn>>; |
||||
pub struct DbConn(pub PooledConnection<ConnectionManager<Conn>>); |
||||
|
||||
#[rocket::async_trait] |
||||
impl<'r> FromRequest<'r> for DbConn { |
||||
type Error = (); |
||||
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> { |
||||
let pool = request.rocket().state::<DbPool>().unwrap(); |
||||
match pool.get() { |
||||
Ok(conn) => Outcome::Success(DbConn(conn)), |
||||
Err(_) => Outcome::Failure((Status::ServiceUnavailable, ())), |
||||
} |
||||
} |
||||
} |
||||
|
||||
// For the convenience of using an &DbConn as an &Connection.
|
||||
impl Deref for DbConn { |
||||
type Target = Conn; |
||||
|
||||
fn deref(&self) -> &Self::Target { |
||||
&self.0 |
||||
} |
||||
} |
||||
|
||||
pub fn init_pool() -> DbPool { |
||||
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); |
||||
let manager = ConnectionManager::<Conn>::new(database_url); |
||||
Pool::builder() |
||||
.build(manager) |
||||
.expect("database poll init fail") |
||||
} |
Loading…
Reference in new issue