mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-11-04 09:34:04 +00:00 
			
		
		
		
	Can create text posts
This commit is contained in:
		@@ -12,7 +12,7 @@ use crate::data::post::{Post, PostAccessLevel, PostFile, PostKind, PostPageKind,
 | 
			
		||||
use crate::data::post::PostKind::{POST_KIND_COUNTDOWN, POST_KIND_IMAGE, POST_KIND_MOVIE, POST_KIND_PDF, POST_KIND_SURVEY, POST_KIND_WEBLINK, POST_KIND_YOUTUBE};
 | 
			
		||||
use crate::data::user::UserID;
 | 
			
		||||
use crate::helpers::{database, friends_helper, groups_helper, user_helper};
 | 
			
		||||
use crate::utils::date_utils::time;
 | 
			
		||||
use crate::utils::date_utils::{mysql_date, time};
 | 
			
		||||
 | 
			
		||||
impl PostVisibilityLevel {
 | 
			
		||||
    pub fn to_db(&self) -> u32 {
 | 
			
		||||
@@ -35,6 +35,54 @@ impl PostVisibilityLevel {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl PostKind {
 | 
			
		||||
    pub fn to_db(&self) -> String {
 | 
			
		||||
        match self {
 | 
			
		||||
            PostKind::POST_KIND_TEXT => "texte",
 | 
			
		||||
            POST_KIND_IMAGE(_) => "image",
 | 
			
		||||
            POST_KIND_WEBLINK(_) => "webpage_link",
 | 
			
		||||
            POST_KIND_PDF(_) => "pdf",
 | 
			
		||||
            POST_KIND_MOVIE(_) => "video",
 | 
			
		||||
            POST_KIND_COUNTDOWN(_) => "count_down",
 | 
			
		||||
            POST_KIND_SURVEY => "sondage",
 | 
			
		||||
            POST_KIND_YOUTUBE(_) => "youtube",
 | 
			
		||||
        }.to_string()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Create a new post
 | 
			
		||||
pub fn create(p: &Post) -> ResultBoxError<u64> {
 | 
			
		||||
 | 
			
		||||
    // Determine post target
 | 
			
		||||
    let (user_id, friend_id, group_id) = match &p.target_page {
 | 
			
		||||
        PostPageKind::PAGE_KIND_USER(user_id) => {
 | 
			
		||||
            (user_id, Some(&p.user_id), None)
 | 
			
		||||
        }
 | 
			
		||||
        PostPageKind::PAGE_KIND_GROUP(group_id) => {
 | 
			
		||||
            (&p.user_id, None, Some(group_id))
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    // Start insert query
 | 
			
		||||
    let insert_query = database::InsertQuery::new(POSTS_TABLE)
 | 
			
		||||
        .add_user_id("ID_personne", user_id)
 | 
			
		||||
        .add_u64("ID_amis", friend_id.map(|f| f.id()).unwrap_or(0))
 | 
			
		||||
        .add_u64("group_id", group_id.map(|f| f.id()).unwrap_or(0))
 | 
			
		||||
        .add_str("date_envoi", &mysql_date())
 | 
			
		||||
        .add_u64("time_insert", p.time_create)
 | 
			
		||||
        .add_u32("niveau_visibilite", p.visibility.to_db())
 | 
			
		||||
        .add_str("type", &p.kind.to_db())
 | 
			
		||||
        .add_opt_str("texte", p.content.as_ref());
 | 
			
		||||
 | 
			
		||||
    // Execute insertion
 | 
			
		||||
    let post_id = match insert_query.insert()? {
 | 
			
		||||
        None => Err(ExecError::new("Insert post query did not return a result!")),
 | 
			
		||||
        Some(id) => Ok(id),
 | 
			
		||||
    }?;
 | 
			
		||||
 | 
			
		||||
    Ok(post_id)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
pub struct PostsQuery {
 | 
			
		||||
    /// The ID of the user making the request
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user