diff --git a/model/sqlite3/src/db/models.rs b/model/sqlite3/src/db/models.rs index 7b6ed62..1f66e58 100644 --- a/model/sqlite3/src/db/models.rs +++ b/model/sqlite3/src/db/models.rs @@ -3,7 +3,7 @@ use serde::Serialize; #[derive(Serialize, Queryable)] pub struct User { - pub id: Option, + id: Option, pub nickname: Option, pub pronouns: Option, pub address_1_name: Option, @@ -19,10 +19,10 @@ pub struct User { #[derive(Serialize, Queryable)] pub struct UserSkill { - id: i64, - user_id: i64, - skill_id: i64, - level: i64, + pub id: Option, + pub user_id: i64, + pub skill_id: i64, + pub level: i64, } #[derive(Serialize, Queryable)] @@ -36,17 +36,17 @@ pub struct UserLanguage { #[derive(Serialize, Queryable)] pub struct Skill { - id: i64, - name: String, + pub id: Option, + pub name: Option, } #[derive(Serialize, Queryable)] pub struct UserExperience { - id: i64, - user_id: i64, - description: String, - start: DateTime, - end: DateTime, + pub id: i64, + pub user_id: i64, + pub description: String, + pub start: DateTime, + pub end: DateTime, } #[derive(Serialize, Queryable)] @@ -66,24 +66,24 @@ pub struct UserContact { #[derive(Serialize, Queryable)] pub struct Topic { - id: i64, - name: String, + pub id: Option, + pub name: Option, } #[derive(Serialize, Queryable)] pub struct Language { - id: i64, - name: String, + pub id: Option, + pub name: Option, } #[derive(Serialize, Queryable)] pub struct Experience { - id: i64, - name: String, + pub id: Option, + pub name: Option, } #[derive(Serialize, Queryable)] pub struct ContactType { - id: i64, - name: String, + pub id: Option, + pub name: Option, } diff --git a/server/src/main.rs b/server/src/main.rs index 4bd682b..191f8b5 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,27 +1,25 @@ -#![allow(proc_macro_derive_resolution_fallback)] -#[macro_use] +extern crate dotenv; extern crate diesel; extern crate serde; extern crate sqlite3; -use actix_web::{ HttpServer, Responder, middleware, App}; +use actix_web::{ HttpServer, App}; use diesel::sqlite::SqliteConnection; -use std::time::Duration; mod routes; -use self::routes::{index,projects,skills}; +use self::routes::{profiles,skills,languages}; use diesel::r2d2::{self, ConnectionManager}; - +use dotenv::dotenv; type DbPool = r2d2::Pool>; #[actix_web::main] async fn main() -> std::io::Result<()> { - + dotenv().ok(); // set up database connection pool - let connspec = std::env::var("DATABASE_URL").expect("DATABASE_URL"); + let connspec = dotenv::var("DATABASE_URL").expect("DATABASE_URL"); let manager = ConnectionManager::::new(connspec); let pool = r2d2::Pool::builder() .build(manager) @@ -30,9 +28,9 @@ async fn main() -> std::io::Result<()> { HttpServer::new(move || { App::new() .data(pool.clone()) - .service(index) - .service(projects) .service(skills) + .service(profiles) + .service(languages) }) .bind("127.0.0.1:8080")? .run() diff --git a/server/src/routes.rs b/server/src/routes.rs index 24c243c..16dedac 100644 --- a/server/src/routes.rs +++ b/server/src/routes.rs @@ -1,53 +1,66 @@ -use crate::{sqlite3::db::schema, sqlite3::db::models}; -use actix_web::{ - web,get, - Responder, - Result, - error::{ErrorInternalServerError, ErrorNotFound}, - }; - - -use diesel::prelude::*; +use crate::{sqlite3::db::models}; +use actix_web::{ web, get, Responder }; use crate::DbPool; +use diesel::prelude::*; -#[get("/{id}/{name}/index.html")] -pub async fn index( web::Path((id, name)): web::Path<(u32, String)>) - -> impl Responder { - format!("Hello {}! id:{}", name, id) -} - -#[get("/projects/{id}")] -pub async fn projects(web::Path(id): web::Path) -> impl Responder { - format!("Project id:{}", id) -} - - -pub fn load_skills( - conn: &SqliteConnection, -) -> Result, diesel::result::Error> { - use crate::sqlite3::db::schema::skills::dsl::*; - - let s = skills.load::(conn); - - return s -} #[get("/skills")] async fn skills(pool: web::Data) -> impl Responder { + use crate::sqlite3::db::schema::skills::dsl::*; let conn = pool.get().expect("couldn't get db connection from pool"); - // use web::block to offload blocking Diesel code without blocking server thread - let skills = web::block(move || load_skills( &conn)) + // use web::block to offload blocking Diesel code without blocking + // server thread + let r = web::block(move || skills.load::(&conn)) .await .map_err(|e| { eprintln!("{}", e); // HttpResponse::InternalServerError().reason("").finish() }); - web::Json(skills.unwrap()) + web::Json(r.unwrap()) } +#[get("/users/profiles")] +async fn profiles(pool: web::Data) -> impl Responder +{ + use crate::sqlite3::db::schema::users::dsl::*; + let conn = pool.get().expect("couldn't get db connection from pool"); + + // use web::block to offload blocking Diesel code without blocking + // server thread + let r = web::block(move || users.load::(&conn)) + .await + .map_err(|e| { + eprintln!("{}", e); +// HttpResponse::InternalServerError().reason("").finish() + }); + web::Json(r.unwrap()) +} + +// ---------------- +#[get("/languages")] +async fn languages(pool: web::Data) -> impl Responder +{ + use crate::sqlite3::db::schema::languages::dsl::*; + let conn = pool.get().expect("couldn't get db connection from pool"); + + // use web::block to offload blocking Diesel code without blocking + // server thread + let r = web::block(move || languages.load::(&conn)) + .await + .map_err(|e| { + eprintln!("{}", e); +// HttpResponse::InternalServerError().reason("").finish() + }); + web::Json(r.unwrap()) +} + + + +//GET `/languages/{id}/icon` + //#[get("/skills/{id}")] //pub /*async*/ fn skills(state: web::Data) -> Result// { // let results = schema::skills::table.load::(&state.db); @@ -58,3 +71,15 @@ async fn skills(pool: web::Data) -> impl Responder // } //} + +//#[get("/{id}/{name}/index.html")] +//pub async fn index( web::Path((id, name)): web::Path<(u32, String)>) +// -> impl Responder { +// format!("Hello {}! id:{}", name, id) +//} + +//#[get("/projects/{id}")] +//pub async fn projects(web::Path(id): web::Path) -> impl Responder { +// format!("Project id:{}", id) +//} +