feat: switch Dioxus to fullstack, add a form for creating a project
This commit is contained in:
		
							
								
								
									
										12
									
								
								src/server/database_connection.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								src/server/database_connection.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | ||||
| use diesel::pg::PgConnection; | ||||
| use diesel::prelude::*; | ||||
| use dotenvy::dotenv; | ||||
| use std::env; | ||||
|  | ||||
| pub(crate) fn establish_database_connection() -> PgConnection { | ||||
|     dotenv().ok(); | ||||
|  | ||||
|     let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); | ||||
|     PgConnection::establish(&database_url) | ||||
|         .unwrap_or_else(|_| panic!("error connecting to {}", database_url)) | ||||
| } | ||||
							
								
								
									
										2
									
								
								src/server/mod.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								src/server/mod.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,2 @@ | ||||
| mod database_connection; | ||||
| pub(crate) mod projects; | ||||
							
								
								
									
										21
									
								
								src/server/projects.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								src/server/projects.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| use crate::models::project::{NewProject, Project}; | ||||
| use crate::server::database_connection::establish_database_connection; | ||||
| use diesel::{RunQueryDsl, SelectableHelper}; | ||||
| use dioxus::prelude::*; | ||||
|  | ||||
| #[server] | ||||
| pub(crate) async fn create_project(title: String) -> Result<Project, ServerFnError> { | ||||
|     use crate::schema::projects; | ||||
|  | ||||
|     let mut connection = establish_database_connection(); | ||||
|  | ||||
|     let new_project = NewProject { | ||||
|         title: title.as_str(), | ||||
|     }; | ||||
|  | ||||
|     Ok(diesel::insert_into(projects::table) | ||||
|         .values(&new_project) | ||||
|         .returning(Project::as_returning()) | ||||
|         .get_result(&mut connection) | ||||
|         .expect("error saving a new project")) | ||||
| } | ||||
		Reference in New Issue
	
	Block a user