From 33661164041b75b992b2b50f801fa27a253da953 Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Sun, 12 Nov 2023 17:52:30 +0100 Subject: [PATCH] Backend: Added basic support for Data Base implementation. --- backend/src/db.rs | 53 ++++++++++++++++++++++++++++++++++ backend/src/main.rs | 69 +++++++++++++++++++++++++++++++++------------ 2 files changed, 104 insertions(+), 18 deletions(-) create mode 100644 backend/src/db.rs diff --git a/backend/src/db.rs b/backend/src/db.rs new file mode 100644 index 0000000..61200d1 --- /dev/null +++ b/backend/src/db.rs @@ -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, + pub phone_number: Option, + pub email: Option, + pub softskills: Option, + pub interests: Option, + pub birth_year: Option, +} + +#[derive(Deserialize, Serialize)] +pub struct Education { + pub id: i64, + pub start_year: Option, + pub end_year: Option, + pub school: Option, + pub speciality: Option, + pub school_location: Option, + pub school_options: Option, +} + +#[derive(Deserialize, Serialize)] +pub struct Experience { + pub id: i64, + pub job_position: Option, + pub job_description: Option, + pub enterprise: Option, + pub enterprise_location: Option, + pub start_year: Option, + pub end_year: Option, +} + +#[derive(Deserialize, Serialize)] +pub struct Project { + pub id: i64, + pub date_done: Option, + pub title: Option, + pub description: Option, + pub github_link: Option, + pub id_skills: i64, +} + +#[derive(Deserialize, Serialize)] +pub struct Skills { + pub id: i64, + pub programming_lang: Option, + pub software: Option, + pub languages: Option, +} \ No newline at end of file diff --git a/backend/src/main.rs b/backend/src/main.rs index 1b979bb..234f1d0 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -5,14 +5,13 @@ use axum::{ }; use axum_error::Result; use dotenv::dotenv; -use serde::{Deserialize, Serialize}; -use sqlx::{ - postgres::{PgPool, PgPoolOptions}, - types::chrono::NaiveDate, -}; +use sqlx::postgres::{PgPool, PgPoolOptions}; use std::net::SocketAddr; use tower_http::cors::CorsLayer; +mod db; +use db::{Education, Experience, Info, Project, Skills}; + #[tokio::main] async fn main() -> Result<()> { // Environnement table + pool connection @@ -26,6 +25,9 @@ async fn main() -> Result<()> { // Creating a router let router = Router::new() .route("/info", get(info)) + .route("/education", get(education)) + .route("/experience", get(experience)) + .route("/skills", get(skills)) .with_state(pool) .layer(CorsLayer::very_permissive()); @@ -36,23 +38,54 @@ async fn main() -> Result<()> { .await?) } -#[derive(Deserialize, Serialize)] -struct Info { - id: i64, - full_name: Option, - phone_number: Option, - email: Option, - softskills: Option, - interests: Option, - birth_year: Option, -} - async fn info(State(pool): State) -> Result>> { - let infos = sqlx::query_as!( + let datas = sqlx::query_as!( Info, "SELECT id, full_name, phone_number, email, softskills, interests, birth_year FROM public.info" ) .fetch_all(&pool) .await?; - Ok(Json(infos)) + Ok(Json(datas)) +} + +async fn education(State(pool): State) -> Result>> { + 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) -> Result>> { + 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) -> Result, Vec)>> { + // 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))) }