unused imports etc. cleaned up, added routes and handlers for /users/profiles and /languages

This commit is contained in:
gulliver 2021-06-05 23:32:08 +02:00
parent 1d9e23856d
commit 5244596e0c
3 changed files with 87 additions and 64 deletions

View File

@ -3,7 +3,7 @@ use serde::Serialize;
#[derive(Serialize, Queryable)]
pub struct User {
pub id: Option<i64>,
id: Option<i64>,
pub nickname: Option<String>,
pub pronouns: Option<String>,
pub address_1_name: Option<String>,
@ -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<i64>,
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<i64>,
pub name: Option<String>,
}
#[derive(Serialize, Queryable)]
pub struct UserExperience {
id: i64,
user_id: i64,
description: String,
start: DateTime<Utc>,
end: DateTime<Utc>,
pub id: i64,
pub user_id: i64,
pub description: String,
pub start: DateTime<Utc>,
pub end: DateTime<Utc>,
}
#[derive(Serialize, Queryable)]
@ -66,24 +66,24 @@ pub struct UserContact {
#[derive(Serialize, Queryable)]
pub struct Topic {
id: i64,
name: String,
pub id: Option<i64>,
pub name: Option<String>,
}
#[derive(Serialize, Queryable)]
pub struct Language {
id: i64,
name: String,
pub id: Option<i64>,
pub name: Option<String>,
}
#[derive(Serialize, Queryable)]
pub struct Experience {
id: i64,
name: String,
pub id: Option<i64>,
pub name: Option<String>,
}
#[derive(Serialize, Queryable)]
pub struct ContactType {
id: i64,
name: String,
pub id: Option<i64>,
pub name: Option<String>,
}

View File

@ -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<ConnectionManager<SqliteConnection>>;
#[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::<SqliteConnection>::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()

View File

@ -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<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")]
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");
// 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::<models::Skill>(&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<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}")]
//pub /*async*/ fn skills(state: web::Data<AppState>) -> Result<impl Responder>// {
// 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)
//}