Add a Media model
This commit is contained in:
		
							parent
							
								
									2c33e1612d
								
							
						
					
					
						commit
						0b5eb2c946
					
				
							
								
								
									
										2
									
								
								migrations/2018-09-02-111458_create_medias/down.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								migrations/2018-09-02-111458_create_medias/down.sql
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,2 @@
 | 
			
		||||
-- This file should undo anything in `up.sql`
 | 
			
		||||
DROP TABLE medias;
 | 
			
		||||
							
								
								
									
										10
									
								
								migrations/2018-09-02-111458_create_medias/up.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								migrations/2018-09-02-111458_create_medias/up.sql
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,10 @@
 | 
			
		||||
-- Your SQL goes here
 | 
			
		||||
CREATE TABLE medias (
 | 
			
		||||
    id SERIAL PRIMARY KEY,
 | 
			
		||||
    file_path TEXT NOT NULL DEFAULT '',
 | 
			
		||||
    alt_text TEXT NOT NULL DEFAULT '',
 | 
			
		||||
    is_remote BOOLEAN NOT NULL DEFAULT 'f',
 | 
			
		||||
    remote_url TEXT,
 | 
			
		||||
    sensitive BOOLEAN NOT NULL DEFAULT 'f',
 | 
			
		||||
    content_warning TEXT
 | 
			
		||||
)
 | 
			
		||||
@ -78,7 +78,7 @@ fn get_next_id(conn: &PgConnection, seq: &str) -> i32 {
 | 
			
		||||
    // We cant' use currval because it may fail if nextval have never been called before
 | 
			
		||||
    let next = select(nextval(seq)).get_result::<i64>(conn).expect("Next ID fail");
 | 
			
		||||
    if next > 1 {
 | 
			
		||||
        select(setval(seq, next - 1)).get_result::<i64>(conn).expect("Reset ID fail");        
 | 
			
		||||
        select(setval(seq, next - 1)).get_result::<i64>(conn).expect("Reset ID fail");
 | 
			
		||||
    }
 | 
			
		||||
    next as i32
 | 
			
		||||
}
 | 
			
		||||
@ -87,7 +87,7 @@ fn get_next_id(conn: &PgConnection, seq: &str) -> i32 {
 | 
			
		||||
lazy_static! {
 | 
			
		||||
    pub static ref BASE_URL: String = env::var("BASE_URL")
 | 
			
		||||
        .unwrap_or(format!("127.0.0.1:{}", env::var("ROCKET_PORT").unwrap_or(String::from("8000"))));
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    pub static ref DB_URL: String = env::var("DB_URL")
 | 
			
		||||
        .unwrap_or(format!("postgres://plume:plume@localhost/{}", env::var("DB_NAME").unwrap_or(String::from("plume"))));
 | 
			
		||||
 | 
			
		||||
@ -111,6 +111,7 @@ pub mod db_conn;
 | 
			
		||||
pub mod follows;
 | 
			
		||||
pub mod instance;
 | 
			
		||||
pub mod likes;
 | 
			
		||||
pub mod medias;
 | 
			
		||||
pub mod mentions;
 | 
			
		||||
pub mod notifications;
 | 
			
		||||
pub mod post_authors;
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										29
									
								
								plume-models/src/medias.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								plume-models/src/medias.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,29 @@
 | 
			
		||||
use diesel::{self, PgConnection, QueryDsl, ExpressionMethods, RunQueryDsl};
 | 
			
		||||
use schema::medias;
 | 
			
		||||
 | 
			
		||||
#[derive(Queryable)]
 | 
			
		||||
pub struct Media {
 | 
			
		||||
    pub id: i32,
 | 
			
		||||
    pub file_path: String,
 | 
			
		||||
    pub alt_text: String,
 | 
			
		||||
    pub is_remote: bool,
 | 
			
		||||
    pub remote_url: Option<String>,
 | 
			
		||||
    pub sensitive: bool,
 | 
			
		||||
    pub content_warning: Option<String>
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Insertable)]
 | 
			
		||||
#[table_name = "medias"]
 | 
			
		||||
pub struct NewMedia {
 | 
			
		||||
    pub file_path: String,
 | 
			
		||||
    pub alt_text: String,
 | 
			
		||||
    pub is_remote: bool,
 | 
			
		||||
    pub remote_url: Option<String>,
 | 
			
		||||
    pub sensitive: bool,
 | 
			
		||||
    pub content_warning: Option<String>
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Media {
 | 
			
		||||
    insert!(medias, NewMedia);
 | 
			
		||||
    get!(medias);
 | 
			
		||||
}
 | 
			
		||||
@ -72,6 +72,18 @@ table! {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
table! {
 | 
			
		||||
    medias (id) {
 | 
			
		||||
        id -> Int4,
 | 
			
		||||
        file_path -> Text,
 | 
			
		||||
        alt_text -> Text,
 | 
			
		||||
        is_remote -> Bool,
 | 
			
		||||
        remote_url -> Nullable<Text>,
 | 
			
		||||
        sensitive -> Bool,
 | 
			
		||||
        content_warning -> Nullable<Text>,
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
table! {
 | 
			
		||||
    mentions (id) {
 | 
			
		||||
        id -> Int4,
 | 
			
		||||
@ -170,6 +182,7 @@ allow_tables_to_appear_in_same_query!(
 | 
			
		||||
    follows,
 | 
			
		||||
    instances,
 | 
			
		||||
    likes,
 | 
			
		||||
    medias,
 | 
			
		||||
    mentions,
 | 
			
		||||
    notifications,
 | 
			
		||||
    post_authors,
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user