feat: edit cw
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
use crate::api::comment::{c2output, CommentOutput};
|
use crate::api::comment::{c2output, CommentOutput};
|
||||||
use crate::api::{APIError, CurrentUser, PolicyError::*, API};
|
use crate::api::{APIError, CurrentUser, PolicyError::*, API};
|
||||||
|
use crate::db_conn::DbConn;
|
||||||
use crate::models::*;
|
use crate::models::*;
|
||||||
use chrono::NaiveDateTime;
|
use chrono::NaiveDateTime;
|
||||||
use rocket::form::Form;
|
use rocket::form::Form;
|
||||||
@@ -7,7 +8,6 @@ use rocket::serde::{
|
|||||||
json::{json, Value},
|
json::{json, Value},
|
||||||
Serialize,
|
Serialize,
|
||||||
};
|
};
|
||||||
use crate::db_conn::DbConn;
|
|
||||||
|
|
||||||
#[derive(FromForm)]
|
#[derive(FromForm)]
|
||||||
pub struct PostInput<'r> {
|
pub struct PostInput<'r> {
|
||||||
@@ -40,6 +40,13 @@ pub struct PostOutput {
|
|||||||
reply: i32,
|
reply: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(FromForm)]
|
||||||
|
pub struct CwInput<'r> {
|
||||||
|
pid: i32,
|
||||||
|
#[field(validate = len(0..33))]
|
||||||
|
cw: &'r str,
|
||||||
|
}
|
||||||
|
|
||||||
fn p2output(p: &Post, user: &CurrentUser, conn: &DbConn) -> PostOutput {
|
fn p2output(p: &Post, user: &CurrentUser, conn: &DbConn) -> PostOutput {
|
||||||
PostOutput {
|
PostOutput {
|
||||||
pid: p.id,
|
pid: p.id,
|
||||||
@@ -131,3 +138,13 @@ pub fn publish_post(poi: Form<PostInput>, user: CurrentUser, conn: DbConn) -> AP
|
|||||||
"code": 0
|
"code": 0
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[post("/editcw", data = "<cwi>")]
|
||||||
|
pub fn edit_cw(cwi: Form<CwInput>, user: CurrentUser, conn: DbConn) -> API<Value> {
|
||||||
|
let p = Post::get(&conn, cwi.pid).map_err(APIError::from_db)?;
|
||||||
|
if !(user.is_admin || p.author_hash == user.namehash) {
|
||||||
|
return Err(APIError::PcError(NotAllowed));
|
||||||
|
}
|
||||||
|
_ = p.update_cw(&conn, cwi.cw);
|
||||||
|
Ok(json!({"code": 0}))
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,16 +4,14 @@ extern crate rocket;
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate diesel;
|
extern crate diesel;
|
||||||
|
|
||||||
|
|
||||||
mod api;
|
mod api;
|
||||||
|
mod db_conn;
|
||||||
mod models;
|
mod models;
|
||||||
mod random_hasher;
|
mod random_hasher;
|
||||||
mod schema;
|
mod schema;
|
||||||
mod db_conn;
|
|
||||||
|
|
||||||
|
|
||||||
use random_hasher::RandomHasher;
|
|
||||||
use db_conn::init_pool;
|
use db_conn::init_pool;
|
||||||
|
use random_hasher::RandomHasher;
|
||||||
|
|
||||||
#[launch]
|
#[launch]
|
||||||
fn rocket() -> _ {
|
fn rocket() -> _ {
|
||||||
@@ -26,6 +24,7 @@ fn rocket() -> _ {
|
|||||||
api::post::get_list,
|
api::post::get_list,
|
||||||
api::post::get_one,
|
api::post::get_one,
|
||||||
api::post::publish_post,
|
api::post::publish_post,
|
||||||
|
api::post::edit_cw,
|
||||||
api::systemlog::get_systemlog,
|
api::systemlog::get_systemlog,
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ type MR<T> = Result<T, diesel::result::Error>;
|
|||||||
no_arg_sql_function!(RANDOM, (), "Represents the sql RANDOM() function");
|
no_arg_sql_function!(RANDOM, (), "Represents the sql RANDOM() function");
|
||||||
|
|
||||||
|
|
||||||
#[derive(Queryable, Debug)]
|
#[derive(Queryable, Identifiable)]
|
||||||
pub struct Post {
|
pub struct Post {
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
pub author_hash: String,
|
pub author_hash: String,
|
||||||
@@ -82,6 +82,10 @@ impl Post {
|
|||||||
// TODO: tags
|
// TODO: tags
|
||||||
insert_into(posts::table).values(&new_post).execute(conn)
|
insert_into(posts::table).values(&new_post).execute(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn update_cw(&self, conn: &Conn, new_cw: &str) -> MR<usize> {
|
||||||
|
diesel::update(self).set(posts::cw.eq(new_cw)).execute(conn)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Queryable, Debug)]
|
#[derive(Queryable, Debug)]
|
||||||
|
|||||||
Reference in New Issue
Block a user