Impl Crud for Tagline

Remove superfluous properties
This commit is contained in:
Freakazoid182 2024-04-05 22:36:08 +02:00
parent f1b993f90e
commit 144c112247
5 changed files with 28 additions and 35 deletions

View file

@ -5,9 +5,12 @@ use lemmy_api_common::{
tagline::{CreateTagline, TaglineResponse},
utils::is_admin,
};
use lemmy_db_schema::source::{
local_site::LocalSite,
tagline::{Tagline, TaglineInsertForm},
use lemmy_db_schema::{
source::{
local_site::LocalSite,
tagline::{Tagline, TaglineInsertForm},
},
traits::Crud,
};
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::LemmyError;
@ -25,7 +28,6 @@ pub async fn create_tagline(
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?;

View file

@ -6,7 +6,7 @@ use lemmy_api_common::{
utils::is_admin,
SuccessResponse,
};
use lemmy_db_schema::source::tagline::Tagline;
use lemmy_db_schema::{source::tagline::Tagline, traits::Crud};
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::LemmyError;

View file

@ -6,10 +6,8 @@ use lemmy_api_common::{
utils::is_admin,
};
use lemmy_db_schema::{
source::{
local_site::LocalSite,
tagline::{Tagline, TaglineUpdateForm},
},
source::tagline::{Tagline, TaglineUpdateForm},
traits::Crud,
utils::naive_now,
};
use lemmy_db_views::structs::LocalUserView;
@ -21,14 +19,12 @@ pub async fn update_tagline(
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()),
content: Some(data.content.to_string()),
updated: Some(Some(naive_now())),
};
let tagline = Tagline::update(&mut context.pool(), data.id, &tagline_form).await?;

View file

@ -2,13 +2,19 @@ use crate::{
newtypes::{LocalSiteId, TaglineId},
schema::tagline::dsl::{local_site_id, published, tagline},
source::tagline::{Tagline, TaglineInsertForm, TaglineUpdateForm},
traits::Crud,
utils::{get_conn, limit_and_offset, DbPool},
};
use diesel::{insert_into, result::Error, ExpressionMethods, OptionalExtension, QueryDsl};
use diesel_async::RunQueryDsl;
impl Tagline {
pub async fn create(pool: &mut DbPool<'_>, form: &TaglineInsertForm) -> Result<Self, Error> {
#[async_trait]
impl Crud for Tagline {
type InsertForm = TaglineInsertForm;
type UpdateForm = TaglineUpdateForm;
type IdType = TaglineId;
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
insert_into(tagline)
.values(form)
@ -16,34 +22,25 @@ impl Tagline {
.await
}
pub async fn update(
async fn update(
pool: &mut DbPool<'_>,
tagline_id: TaglineId,
form: &TaglineUpdateForm,
new_tagline: &Self::UpdateForm,
) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
diesel::update(tagline.find(tagline_id))
.set(form)
.set(new_tagline)
.get_result::<Self>(conn)
.await
}
pub async fn delete(pool: &mut DbPool<'_>, tagline_id: TaglineId) -> Result<usize, Error> {
async fn delete(pool: &mut DbPool<'_>, id: Self::IdType) -> Result<usize, Error> {
let conn = &mut get_conn(pool).await?;
diesel::delete(tagline.find(tagline_id)).execute(conn).await
}
pub async fn get_all(
pool: &mut DbPool<'_>,
for_local_site_id: LocalSiteId,
) -> Result<Vec<Self>, Error> {
let conn = &mut get_conn(pool).await?;
tagline
.filter(local_site_id.eq(for_local_site_id))
.get_results::<Self>(conn)
.await
diesel::delete(tagline.find(id)).execute(conn).await
}
}
impl Tagline {
pub async fn list(
pool: &mut DbPool<'_>,
for_local_site_id: LocalSiteId,

View file

@ -35,14 +35,12 @@ pub struct Tagline {
pub struct TaglineInsertForm {
pub local_site_id: LocalSiteId,
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>>,
pub content: Option<String>,
pub updated: Option<Option<DateTime<Utc>>>,
}