Add a Reshare model
This commit is contained in:
		
							parent
							
								
									14bf3f00aa
								
							
						
					
					
						commit
						045e885821
					
				
							
								
								
									
										2
									
								
								migrations/2018-05-19-091428_create_reshares/down.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								migrations/2018-05-19-091428_create_reshares/down.sql
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,2 @@ | ||||
| -- This file should undo anything in `up.sql` | ||||
| DROP TABLE reshares; | ||||
							
								
								
									
										8
									
								
								migrations/2018-05-19-091428_create_reshares/up.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								migrations/2018-05-19-091428_create_reshares/up.sql
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,8 @@ | ||||
| -- Your SQL goes here | ||||
| CREATE TABLE reshares ( | ||||
|     id SERIAL PRIMARY KEY, | ||||
|     user_id INTEGER REFERENCES users(id) ON DELETE CASCADE NOT NULL, | ||||
|     post_id INTEGER REFERENCES posts(id) ON DELETE CASCADE NOT NULL, | ||||
|     ap_url VARCHAR NOT NULL DEFAULT '', | ||||
|     creation_date TIMESTAMP NOT NULL DEFAULT now() | ||||
| ) | ||||
| @ -7,4 +7,5 @@ pub mod likes; | ||||
| pub mod notifications; | ||||
| pub mod post_authors; | ||||
| pub mod posts; | ||||
| pub mod reshares; | ||||
| pub mod users; | ||||
|  | ||||
							
								
								
									
										38
									
								
								src/models/reshares.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								src/models/reshares.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,38 @@ | ||||
| use chrono::NaiveDateTime; | ||||
| use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods}; | ||||
| 
 | ||||
| use schema::reshares; | ||||
| 
 | ||||
| #[derive(Serialize, Deserialize, Queryable, Identifiable)] | ||||
| pub struct Reshare { | ||||
|     id: i32, | ||||
|     user_id: i32, | ||||
|     post_id: i32, | ||||
|     ap_url: String, | ||||
|     creation_date: NaiveDateTime | ||||
| } | ||||
| 
 | ||||
| #[derive(Insertable)] | ||||
| #[table_name = "reshares"] | ||||
| pub struct NewReshare { | ||||
|     user_id: i32, | ||||
|     post_id: i32, | ||||
|     ap_url: String | ||||
| } | ||||
| 
 | ||||
| impl Reshare { | ||||
|     pub fn insert(conn: &PgConnection, new: NewReshare) -> Reshare { | ||||
|         diesel::insert_into(reshares::table) | ||||
|             .values(new) | ||||
|             .get_result(conn) | ||||
|             .expect("Couldn't save reshare") | ||||
|     } | ||||
| 
 | ||||
|     pub fn get(conn: &PgConnection, id: i32) -> Option<Reshare> { | ||||
|         reshares::table.filter(reshares::id.eq(id)) | ||||
|             .limit(1) | ||||
|             .load::<Reshare>(conn) | ||||
|             .expect("Could'nt load reshare") | ||||
|             .into_iter().nth(0) | ||||
|     } | ||||
| } | ||||
| @ -98,6 +98,16 @@ table! { | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| table! { | ||||
|     reshares (id) { | ||||
|         id -> Int4, | ||||
|         user_id -> Int4, | ||||
|         post_id -> Int4, | ||||
|         ap_url -> Varchar, | ||||
|         creation_date -> Timestamp, | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| table! { | ||||
|     users (id) { | ||||
|         id -> Int4, | ||||
| @ -129,6 +139,8 @@ joinable!(notifications -> users (user_id)); | ||||
| joinable!(post_authors -> posts (post_id)); | ||||
| joinable!(post_authors -> users (author_id)); | ||||
| joinable!(posts -> blogs (blog_id)); | ||||
| joinable!(reshares -> posts (post_id)); | ||||
| joinable!(reshares -> users (user_id)); | ||||
| joinable!(users -> instances (instance_id)); | ||||
| 
 | ||||
| allow_tables_to_appear_in_same_query!( | ||||
| @ -141,5 +153,6 @@ allow_tables_to_appear_in_same_query!( | ||||
|     notifications, | ||||
|     post_authors, | ||||
|     posts, | ||||
|     reshares, | ||||
|     users, | ||||
| ); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user