unused imports etc. cleaned up, added routes and handlers for /users/profiles and /languages
This commit is contained in:
parent
1d9e23856d
commit
5244596e0c
@ -3,7 +3,7 @@ use serde::Serialize;
|
|||||||
|
|
||||||
#[derive(Serialize, Queryable)]
|
#[derive(Serialize, Queryable)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
pub id: Option<i64>,
|
id: Option<i64>,
|
||||||
pub nickname: Option<String>,
|
pub nickname: Option<String>,
|
||||||
pub pronouns: Option<String>,
|
pub pronouns: Option<String>,
|
||||||
pub address_1_name: Option<String>,
|
pub address_1_name: Option<String>,
|
||||||
@ -19,10 +19,10 @@ pub struct User {
|
|||||||
|
|
||||||
#[derive(Serialize, Queryable)]
|
#[derive(Serialize, Queryable)]
|
||||||
pub struct UserSkill {
|
pub struct UserSkill {
|
||||||
id: i64,
|
pub id: Option<i64>,
|
||||||
user_id: i64,
|
pub user_id: i64,
|
||||||
skill_id: i64,
|
pub skill_id: i64,
|
||||||
level: i64,
|
pub level: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Queryable)]
|
#[derive(Serialize, Queryable)]
|
||||||
@ -36,17 +36,17 @@ pub struct UserLanguage {
|
|||||||
|
|
||||||
#[derive(Serialize, Queryable)]
|
#[derive(Serialize, Queryable)]
|
||||||
pub struct Skill {
|
pub struct Skill {
|
||||||
id: i64,
|
pub id: Option<i64>,
|
||||||
name: String,
|
pub name: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Queryable)]
|
#[derive(Serialize, Queryable)]
|
||||||
pub struct UserExperience {
|
pub struct UserExperience {
|
||||||
id: i64,
|
pub id: i64,
|
||||||
user_id: i64,
|
pub user_id: i64,
|
||||||
description: String,
|
pub description: String,
|
||||||
start: DateTime<Utc>,
|
pub start: DateTime<Utc>,
|
||||||
end: DateTime<Utc>,
|
pub end: DateTime<Utc>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Queryable)]
|
#[derive(Serialize, Queryable)]
|
||||||
@ -66,24 +66,24 @@ pub struct UserContact {
|
|||||||
|
|
||||||
#[derive(Serialize, Queryable)]
|
#[derive(Serialize, Queryable)]
|
||||||
pub struct Topic {
|
pub struct Topic {
|
||||||
id: i64,
|
pub id: Option<i64>,
|
||||||
name: String,
|
pub name: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Queryable)]
|
#[derive(Serialize, Queryable)]
|
||||||
pub struct Language {
|
pub struct Language {
|
||||||
id: i64,
|
pub id: Option<i64>,
|
||||||
name: String,
|
pub name: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Queryable)]
|
#[derive(Serialize, Queryable)]
|
||||||
pub struct Experience {
|
pub struct Experience {
|
||||||
id: i64,
|
pub id: Option<i64>,
|
||||||
name: String,
|
pub name: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Queryable)]
|
#[derive(Serialize, Queryable)]
|
||||||
pub struct ContactType {
|
pub struct ContactType {
|
||||||
id: i64,
|
pub id: Option<i64>,
|
||||||
name: String,
|
pub name: Option<String>,
|
||||||
}
|
}
|
||||||
|
@ -1,27 +1,25 @@
|
|||||||
#![allow(proc_macro_derive_resolution_fallback)]
|
|
||||||
|
|
||||||
#[macro_use]
|
extern crate dotenv;
|
||||||
extern crate diesel;
|
extern crate diesel;
|
||||||
extern crate serde;
|
extern crate serde;
|
||||||
extern crate sqlite3;
|
extern crate sqlite3;
|
||||||
|
|
||||||
use actix_web::{ HttpServer, Responder, middleware, App};
|
use actix_web::{ HttpServer, App};
|
||||||
use diesel::sqlite::SqliteConnection;
|
use diesel::sqlite::SqliteConnection;
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
mod routes;
|
mod routes;
|
||||||
|
|
||||||
use self::routes::{index,projects,skills};
|
use self::routes::{profiles,skills,languages};
|
||||||
use diesel::r2d2::{self, ConnectionManager};
|
use diesel::r2d2::{self, ConnectionManager};
|
||||||
|
use dotenv::dotenv;
|
||||||
|
|
||||||
type DbPool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
|
type DbPool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
|
dotenv().ok();
|
||||||
// set up database connection pool
|
// 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::<SqliteConnection>::new(connspec);
|
let manager = ConnectionManager::<SqliteConnection>::new(connspec);
|
||||||
let pool = r2d2::Pool::builder()
|
let pool = r2d2::Pool::builder()
|
||||||
.build(manager)
|
.build(manager)
|
||||||
@ -30,9 +28,9 @@ async fn main() -> std::io::Result<()> {
|
|||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
App::new()
|
App::new()
|
||||||
.data(pool.clone())
|
.data(pool.clone())
|
||||||
.service(index)
|
|
||||||
.service(projects)
|
|
||||||
.service(skills)
|
.service(skills)
|
||||||
|
.service(profiles)
|
||||||
|
.service(languages)
|
||||||
})
|
})
|
||||||
.bind("127.0.0.1:8080")?
|
.bind("127.0.0.1:8080")?
|
||||||
.run()
|
.run()
|
||||||
|
@ -1,53 +1,66 @@
|
|||||||
use crate::{sqlite3::db::schema, sqlite3::db::models};
|
use crate::{sqlite3::db::models};
|
||||||
use actix_web::{
|
use actix_web::{ web, get, Responder };
|
||||||
web,get,
|
|
||||||
Responder,
|
|
||||||
Result,
|
|
||||||
error::{ErrorInternalServerError, ErrorNotFound},
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
use diesel::prelude::*;
|
|
||||||
|
|
||||||
use crate::DbPool;
|
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<u32>) -> impl Responder {
|
|
||||||
format!("Project id:{}", id)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn load_skills(
|
|
||||||
conn: &SqliteConnection,
|
|
||||||
) -> Result<Vec<models::Skill>, diesel::result::Error> {
|
|
||||||
use crate::sqlite3::db::schema::skills::dsl::*;
|
|
||||||
|
|
||||||
let s = skills.load::<models::Skill>(conn);
|
|
||||||
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
#[get("/skills")]
|
#[get("/skills")]
|
||||||
async fn skills(pool: web::Data<DbPool>) -> impl Responder
|
async fn skills(pool: web::Data<DbPool>) -> impl Responder
|
||||||
{
|
{
|
||||||
|
use crate::sqlite3::db::schema::skills::dsl::*;
|
||||||
let conn = pool.get().expect("couldn't get db connection from pool");
|
let conn = pool.get().expect("couldn't get db connection from pool");
|
||||||
|
|
||||||
// use web::block to offload blocking Diesel code without blocking server thread
|
// use web::block to offload blocking Diesel code without blocking
|
||||||
let skills = web::block(move || load_skills( &conn))
|
// server thread
|
||||||
|
let r = web::block(move || skills.load::<models::Skill>(&conn))
|
||||||
.await
|
.await
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
eprintln!("{}", e);
|
eprintln!("{}", e);
|
||||||
// HttpResponse::InternalServerError().reason("").finish()
|
// HttpResponse::InternalServerError().reason("").finish()
|
||||||
});
|
});
|
||||||
web::Json(skills.unwrap())
|
web::Json(r.unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[get("/users/profiles")]
|
||||||
|
async fn profiles(pool: web::Data<DbPool>) -> 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::<models::User>(&conn))
|
||||||
|
.await
|
||||||
|
.map_err(|e| {
|
||||||
|
eprintln!("{}", e);
|
||||||
|
// HttpResponse::InternalServerError().reason("").finish()
|
||||||
|
});
|
||||||
|
web::Json(r.unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------
|
||||||
|
#[get("/languages")]
|
||||||
|
async fn languages(pool: web::Data<DbPool>) -> 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::<models::Language>(&conn))
|
||||||
|
.await
|
||||||
|
.map_err(|e| {
|
||||||
|
eprintln!("{}", e);
|
||||||
|
// HttpResponse::InternalServerError().reason("").finish()
|
||||||
|
});
|
||||||
|
web::Json(r.unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//GET `/languages/{id}/icon`
|
||||||
|
|
||||||
//#[get("/skills/{id}")]
|
//#[get("/skills/{id}")]
|
||||||
//pub /*async*/ fn skills(state: web::Data<AppState>) -> Result<impl Responder>// {
|
//pub /*async*/ fn skills(state: web::Data<AppState>) -> Result<impl Responder>// {
|
||||||
// let results = schema::skills::table.load::<models::Skill>(&state.db);
|
// let results = schema::skills::table.load::<models::Skill>(&state.db);
|
||||||
@ -58,3 +71,15 @@ async fn skills(pool: web::Data<DbPool>) -> 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<u32>) -> impl Responder {
|
||||||
|
// format!("Project id:{}", id)
|
||||||
|
//}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user