feat(db/models): Add handling of CreatePost message
This commit is contained in:
		
							parent
							
								
									55b28f8136
								
							
						
					
					
						commit
						148dfc39c8
					
				
					 2 changed files with 30 additions and 2 deletions
				
			
		
							
								
								
									
										25
									
								
								src/db.rs
									
										
									
									
									
								
							
							
						
						
									
										25
									
								
								src/db.rs
									
										
									
									
									
								
							| 
						 | 
					@ -75,7 +75,28 @@ impl Handler<CreateThread> for DbExecutor {
 | 
				
			||||||
        let conn = self.0.get()?;
 | 
					        let conn = self.0.get()?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Ok(diesel::insert_into(threads::table)
 | 
					        Ok(diesel::insert_into(threads::table)
 | 
				
			||||||
            .values(&msg.0)
 | 
					           .values(&msg.0)
 | 
				
			||||||
            .get_result(&conn)?)
 | 
					           .get_result(&conn)?)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Message used to create a new reply
 | 
				
			||||||
 | 
					pub struct CreatePost(pub NewPost);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl Message for CreatePost {
 | 
				
			||||||
 | 
					    type Result = Result<Post>;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl Handler<CreatePost> for DbExecutor {
 | 
				
			||||||
 | 
					    type Result = <CreatePost as Message>::Result;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn handle(&mut self, msg: CreatePost, _: &mut Self::Context) -> Self::Result {
 | 
				
			||||||
 | 
					        use schema::posts;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        let conn = self.0.get()?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Ok(diesel::insert_into(posts::table)
 | 
				
			||||||
 | 
					           .values(&msg.0)
 | 
				
			||||||
 | 
					           .get_result(&conn)?)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -24,3 +24,10 @@ pub struct NewThread {
 | 
				
			||||||
    pub title: String,
 | 
					    pub title: String,
 | 
				
			||||||
    pub body: String,
 | 
					    pub body: String,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Deserialize, Insertable)]
 | 
				
			||||||
 | 
					#[table_name="posts"]
 | 
				
			||||||
 | 
					pub struct NewPost {
 | 
				
			||||||
 | 
					    pub thread_id: i32,
 | 
				
			||||||
 | 
					    pub body: String,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue