mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-05-20 02:38:05 +00:00
Add update tagline endpoint
This commit is contained in:
parent
a1a7e2ca33
commit
ab938062e4
|
@ -1,4 +1,4 @@
|
|||
use lemmy_db_schema::source::tagline::Tagline;
|
||||
use lemmy_db_schema::{newtypes::TaglineId, source::tagline::Tagline};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_with::skip_serializing_none;
|
||||
#[cfg(feature = "full")]
|
||||
|
@ -12,6 +12,15 @@ pub struct CreateTagline {
|
|||
pub content: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
|
||||
#[cfg_attr(feature = "full", derive(TS))]
|
||||
#[cfg_attr(feature = "full", ts(export))]
|
||||
/// Update a tagline
|
||||
pub struct UpdateTagline {
|
||||
pub id: TaglineId,
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
#[cfg_attr(feature = "full", derive(TS))]
|
||||
#[cfg_attr(feature = "full", ts(export))]
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
pub mod create;
|
||||
pub mod list;
|
||||
pub mod update;
|
||||
|
|
34
crates/api_crud/src/tagline/update.rs
Normal file
34
crates/api_crud/src/tagline/update.rs
Normal file
|
@ -0,0 +1,34 @@
|
|||
use activitypub_federation::config::Data;
|
||||
use actix_web::web::Json;
|
||||
use lemmy_api_common::{
|
||||
context::LemmyContext,
|
||||
tagline::{UpdateTagline, TaglineResponse},
|
||||
utils::is_admin,
|
||||
};
|
||||
use lemmy_db_schema::{source::{
|
||||
local_site::LocalSite,
|
||||
tagline::{Tagline, TaglineUpdateForm},
|
||||
}, utils::naive_now};
|
||||
use lemmy_db_views::structs::LocalUserView;
|
||||
use lemmy_utils::error::LemmyError;
|
||||
|
||||
#[tracing::instrument(skip(context))]
|
||||
pub async fn update_tagline(
|
||||
data: Json<UpdateTagline>,
|
||||
context: Data<LemmyContext>,
|
||||
local_user_view: LocalUserView,
|
||||
) -> Result<Json<TaglineResponse>, LemmyError> {
|
||||
let local_site = LocalSite::read(&mut context.pool()).await?;
|
||||
// Make sure user is an admin
|
||||
is_admin(&local_user_view)?;
|
||||
|
||||
let tagline_form = TaglineUpdateForm {
|
||||
local_site_id: local_site.id,
|
||||
content: data.content.to_string(),
|
||||
updated: Some(naive_now()),
|
||||
};
|
||||
|
||||
let tagline = Tagline::update(&mut context.pool(), data.id, &tagline_form).await?;
|
||||
|
||||
Ok(Json(TaglineResponse { tagline }))
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
use crate::{
|
||||
newtypes::LocalSiteId,
|
||||
newtypes::{LocalSiteId, TaglineId},
|
||||
schema::tagline::dsl::{local_site_id, published, tagline},
|
||||
source::tagline::{Tagline, TaglineInsertForm},
|
||||
source::tagline::{Tagline, TaglineInsertForm, TaglineUpdateForm},
|
||||
utils::{get_conn, limit_and_offset, DbPool},
|
||||
};
|
||||
use diesel::{insert_into, result::Error, ExpressionMethods, QueryDsl};
|
||||
|
@ -52,6 +52,18 @@ impl Tagline {
|
|||
.await
|
||||
}
|
||||
|
||||
pub async fn update(
|
||||
pool: &mut DbPool<'_>,
|
||||
tagline_id: TaglineId,
|
||||
form: &TaglineUpdateForm,
|
||||
) -> Result<Self, Error> {
|
||||
let conn = &mut get_conn(pool).await?;
|
||||
diesel::update(tagline.find(tagline_id))
|
||||
.set(form)
|
||||
.get_result::<Self>(conn)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn clear(conn: &mut AsyncPgConnection) -> Result<usize, Error> {
|
||||
diesel::delete(tagline).execute(conn).await
|
||||
}
|
||||
|
|
|
@ -152,6 +152,12 @@ pub struct LocalSiteId(i32);
|
|||
/// The custom emoji id.
|
||||
pub struct CustomEmojiId(i32);
|
||||
|
||||
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
|
||||
#[cfg_attr(feature = "full", derive(DieselNewType, TS))]
|
||||
#[cfg_attr(feature = "full", ts(export))]
|
||||
/// The tagline id.
|
||||
pub struct TaglineId(i32);
|
||||
|
||||
#[cfg(feature = "full")]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(remote = "Ltree")]
|
||||
|
|
|
@ -37,3 +37,12 @@ pub struct TaglineInsertForm {
|
|||
pub content: String,
|
||||
pub updated: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
|
||||
#[cfg_attr(feature = "full", diesel(table_name = tagline))]
|
||||
pub struct TaglineUpdateForm {
|
||||
pub local_site_id: LocalSiteId,
|
||||
pub content: String,
|
||||
pub updated: Option<DateTime<Utc>>,
|
||||
}
|
|
@ -123,7 +123,11 @@ use lemmy_api_crud::{
|
|||
update::update_private_message,
|
||||
},
|
||||
site::{create::create_site, read::get_site, update::update_site},
|
||||
tagline::{create::create_tagline, list::list_taglines},
|
||||
tagline::{
|
||||
create::create_tagline,
|
||||
list::list_taglines,
|
||||
update::update_tagline
|
||||
},
|
||||
user::{create::register, delete::delete_account},
|
||||
};
|
||||
use lemmy_apub::api::{
|
||||
|
@ -370,6 +374,7 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
|
|||
web::scope("/tagline")
|
||||
.wrap(rate_limit.message())
|
||||
.route("/", web::post().to(create_tagline))
|
||||
.route("/", web::put().to(update_tagline))
|
||||
.route("/list", web::get().to(list_taglines)),
|
||||
),
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue