Backend: Added basic support for Data Base implementation.
This commit is contained in:
parent
182fbb69e8
commit
3366116404
2 changed files with 104 additions and 18 deletions
53
backend/src/db.rs
Normal file
53
backend/src/db.rs
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use sqlx::types::chrono::NaiveDate;
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize)]
|
||||||
|
pub struct Info {
|
||||||
|
pub id: i64,
|
||||||
|
pub full_name: Option<String>,
|
||||||
|
pub phone_number: Option<String>,
|
||||||
|
pub email: Option<String>,
|
||||||
|
pub softskills: Option<String>,
|
||||||
|
pub interests: Option<String>,
|
||||||
|
pub birth_year: Option<NaiveDate>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize)]
|
||||||
|
pub struct Education {
|
||||||
|
pub id: i64,
|
||||||
|
pub start_year: Option<NaiveDate>,
|
||||||
|
pub end_year: Option<NaiveDate>,
|
||||||
|
pub school: Option<String>,
|
||||||
|
pub speciality: Option<String>,
|
||||||
|
pub school_location: Option<String>,
|
||||||
|
pub school_options: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize)]
|
||||||
|
pub struct Experience {
|
||||||
|
pub id: i64,
|
||||||
|
pub job_position: Option<String>,
|
||||||
|
pub job_description: Option<String>,
|
||||||
|
pub enterprise: Option<String>,
|
||||||
|
pub enterprise_location: Option<String>,
|
||||||
|
pub start_year: Option<NaiveDate>,
|
||||||
|
pub end_year: Option<NaiveDate>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize)]
|
||||||
|
pub struct Project {
|
||||||
|
pub id: i64,
|
||||||
|
pub date_done: Option<NaiveDate>,
|
||||||
|
pub title: Option<String>,
|
||||||
|
pub description: Option<String>,
|
||||||
|
pub github_link: Option<String>,
|
||||||
|
pub id_skills: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize)]
|
||||||
|
pub struct Skills {
|
||||||
|
pub id: i64,
|
||||||
|
pub programming_lang: Option<String>,
|
||||||
|
pub software: Option<String>,
|
||||||
|
pub languages: Option<String>,
|
||||||
|
}
|
|
@ -5,14 +5,13 @@ use axum::{
|
||||||
};
|
};
|
||||||
use axum_error::Result;
|
use axum_error::Result;
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
use serde::{Deserialize, Serialize};
|
use sqlx::postgres::{PgPool, PgPoolOptions};
|
||||||
use sqlx::{
|
|
||||||
postgres::{PgPool, PgPoolOptions},
|
|
||||||
types::chrono::NaiveDate,
|
|
||||||
};
|
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use tower_http::cors::CorsLayer;
|
use tower_http::cors::CorsLayer;
|
||||||
|
|
||||||
|
mod db;
|
||||||
|
use db::{Education, Experience, Info, Project, Skills};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
// Environnement table + pool connection
|
// Environnement table + pool connection
|
||||||
|
@ -26,6 +25,9 @@ async fn main() -> Result<()> {
|
||||||
// Creating a router
|
// Creating a router
|
||||||
let router = Router::new()
|
let router = Router::new()
|
||||||
.route("/info", get(info))
|
.route("/info", get(info))
|
||||||
|
.route("/education", get(education))
|
||||||
|
.route("/experience", get(experience))
|
||||||
|
.route("/skills", get(skills))
|
||||||
.with_state(pool)
|
.with_state(pool)
|
||||||
.layer(CorsLayer::very_permissive());
|
.layer(CorsLayer::very_permissive());
|
||||||
|
|
||||||
|
@ -36,23 +38,54 @@ async fn main() -> Result<()> {
|
||||||
.await?)
|
.await?)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
|
||||||
struct Info {
|
|
||||||
id: i64,
|
|
||||||
full_name: Option<String>,
|
|
||||||
phone_number: Option<String>,
|
|
||||||
email: Option<String>,
|
|
||||||
softskills: Option<String>,
|
|
||||||
interests: Option<String>,
|
|
||||||
birth_year: Option<NaiveDate>,
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn info(State(pool): State<PgPool>) -> Result<Json<Vec<Info>>> {
|
async fn info(State(pool): State<PgPool>) -> Result<Json<Vec<Info>>> {
|
||||||
let infos = sqlx::query_as!(
|
let datas = sqlx::query_as!(
|
||||||
Info,
|
Info,
|
||||||
"SELECT id, full_name, phone_number, email, softskills, interests, birth_year FROM public.info"
|
"SELECT id, full_name, phone_number, email, softskills, interests, birth_year FROM public.info"
|
||||||
)
|
)
|
||||||
.fetch_all(&pool)
|
.fetch_all(&pool)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(Json(infos))
|
Ok(Json(datas))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn education(State(pool): State<PgPool>) -> Result<Json<Vec<Education>>> {
|
||||||
|
let datas = sqlx::query_as!(
|
||||||
|
Education,
|
||||||
|
"SELECT id, start_year, end_year, school, speciality, school_location, school_options FROM public.education"
|
||||||
|
)
|
||||||
|
.fetch_all(&pool)
|
||||||
|
.await?;
|
||||||
|
Ok(Json(datas))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn experience(State(pool): State<PgPool>) -> Result<Json<Vec<Experience>>> {
|
||||||
|
let datas = sqlx::query_as!(
|
||||||
|
Experience,
|
||||||
|
"SELECT id, job_position, job_description, enterprise, enterprise_location, start_year, end_year FROM public.experience"
|
||||||
|
)
|
||||||
|
.fetch_all(&pool)
|
||||||
|
.await?;
|
||||||
|
Ok(Json(datas))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn skills(State(pool): State<PgPool>) -> Result<Json<(Vec<Project>, Vec<Skills>)>> {
|
||||||
|
// Gathering skills
|
||||||
|
let skills = sqlx::query_as!(
|
||||||
|
Skills,
|
||||||
|
"SELECT id, programming_lang, software, languages FROM public.skills"
|
||||||
|
)
|
||||||
|
.fetch_all(&pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// Gathering Projects
|
||||||
|
let projects = sqlx::query_as!(
|
||||||
|
Project,
|
||||||
|
"SELECT id, date_done, title, description, github_link, id_skills FROM public.project"
|
||||||
|
)
|
||||||
|
.fetch_all(&pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
Ok(Json((projects,skills)))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue