Add create tagline endpoint

This commit is contained in:
Freakazoid182 2024-04-03 22:14:25 +02:00
parent 486c746eb9
commit a1a7e2ca33
6 changed files with 66 additions and 4 deletions

View file

@ -4,6 +4,21 @@ use serde_with::skip_serializing_none;
#[cfg(feature = "full")]
use ts_rs::TS;
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
/// Create a tagline
pub struct CreateTagline {
pub content: String,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
pub struct TaglineResponse {
pub tagline: Tagline,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]

View file

@ -0,0 +1,34 @@
use activitypub_federation::config::Data;
use actix_web::web::Json;
use lemmy_api_common::{
context::LemmyContext,
tagline::{CreateTagline, TaglineResponse},
utils::is_admin,
};
use lemmy_db_schema::source::{
local_site::LocalSite,
tagline::{Tagline, TaglineInsertForm},
};
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::LemmyError;
#[tracing::instrument(skip(context))]
pub async fn create_tagline(
data: Json<CreateTagline>,
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 = TaglineInsertForm {
local_site_id: local_site.id,
content: data.content.to_string(),
updated: None,
};
let tagline = Tagline::create(&mut context.pool(), &tagline_form).await?;
Ok(Json(TaglineResponse { tagline }))
}

View file

@ -1 +1,2 @@
pub mod create;
pub mod list;

View file

@ -1,7 +1,7 @@
use crate::{
newtypes::LocalSiteId,
schema::tagline::dsl::{local_site_id, published, tagline},
source::tagline::{Tagline, TaglineForm},
source::tagline::{Tagline, TaglineInsertForm},
utils::{get_conn, limit_and_offset, DbPool},
};
use diesel::{insert_into, result::Error, ExpressionMethods, QueryDsl};
@ -22,7 +22,7 @@ impl Tagline {
Self::clear(conn).await?;
for item in list {
let form = TaglineForm {
let form = TaglineInsertForm {
local_site_id: for_local_site_id,
content: item,
updated: None,
@ -41,6 +41,17 @@ impl Tagline {
}
}
pub async fn create(
pool: &mut DbPool<'_>,
form: &TaglineInsertForm,
) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
insert_into(tagline)
.values(form)
.get_result::<Self>(conn)
.await
}
async fn clear(conn: &mut AsyncPgConnection) -> Result<usize, Error> {
diesel::delete(tagline).execute(conn).await
}

View file

@ -32,7 +32,7 @@ pub struct Tagline {
#[derive(Clone, Default)]
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = tagline))]
pub struct TaglineForm {
pub struct TaglineInsertForm {
pub local_site_id: LocalSiteId,
pub content: String,
pub updated: Option<DateTime<Utc>>,

View file

@ -123,7 +123,7 @@ use lemmy_api_crud::{
update::update_private_message,
},
site::{create::create_site, read::get_site, update::update_site},
tagline::list::list_taglines,
tagline::{create::create_tagline, list::list_taglines},
user::{create::register, delete::delete_account},
};
use lemmy_apub::api::{
@ -369,6 +369,7 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
.service(
web::scope("/tagline")
.wrap(rate_limit.message())
.route("/", web::post().to(create_tagline))
.route("/list", web::get().to(list_taglines)),
),
);