diff --git a/backend/db/8_project_tags.sql b/backend/db/8_project_tags.sql new file mode 100644 index 0000000..1a03abc --- /dev/null +++ b/backend/db/8_project_tags.sql @@ -0,0 +1,14 @@ +-- public.project_tags definition + +-- Drop table + +-- DROP TABLE public.project_tags; + +CREATE TABLE public.project_tags ( + project_id int4 NOT NULL, + programming_languages_id int4 NULL, + softwares_id int4 NULL, + CONSTRAINT project_tags_project_fk FOREIGN KEY (project_id) REFERENCES public.project(id), + CONSTRAINT project_tags_programming_languages_fk FOREIGN KEY (programming_languages_id) REFERENCES public.programming_languages(id), + CONSTRAINT project_tags_softwares_fk FOREIGN KEY (softwares_id) REFERENCES public.softwares(id) +); \ No newline at end of file diff --git a/backend/src/db.rs b/backend/src/db.rs index a4a0b36..1b43fc4 100644 --- a/backend/src/db.rs +++ b/backend/src/db.rs @@ -67,4 +67,21 @@ pub struct Languages { pub lang: String, pub icon_alpha: String, pub level: String +} + +#[derive(Deserialize, Serialize)] +pub struct Tags { + pub lang: Option, + pub icon: Option, + pub type_icon: Option, + pub color: Option +} + +#[derive(Deserialize, Serialize)] +pub struct AllTags { + pub project_id: Option, + pub lang: Option, + pub icon: Option, + pub type_icon: Option, + pub color: Option } \ No newline at end of file diff --git a/backend/src/main.rs b/backend/src/main.rs index bf1ab42..69f28b0 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -10,7 +10,7 @@ use std::net::SocketAddr; use tower_http::cors::CorsLayer; mod db; -use db::{Education, Experience, Info, Languages, Project, ProgrammingLanguages, Softwares}; +use db::{Education, Experience, Info, Languages, ProgrammingLanguages, Project, Softwares, Tags, AllTags}; #[tokio::main] async fn main() -> Result<()> { @@ -28,6 +28,8 @@ async fn main() -> Result<()> { .route("/education", get(education)) .route("/experience", get(experience)) .route("/skills/:id", get(skills)) + .route("/tags/:info_id/:project_id", get(tags)) + .route("/tags/:id", get(alltags)) .with_state(pool) .layer(CorsLayer::very_permissive()); @@ -44,8 +46,7 @@ macro_rules! _gather_data { }; } -async fn info(State(pool): State) -> Json> -{ +async fn info(State(pool): State) -> Json> { let datas = sqlx::query_as!( Info, "SELECT id, full_name, phone_number, email, softskills, interests, birth_year FROM public.info" @@ -58,18 +59,28 @@ async fn info(State(pool): State) -> Json> async fn education(State(pool): State) -> Json> { let datas = sqlx::query_as!(Education, "SELECT * FROM public.education") .fetch_all(&pool) - .await.unwrap_or(vec![]); + .await + .unwrap_or(vec![]); Json(datas) } async fn experience(State(pool): State) -> Json> { let datas = sqlx::query_as!(Experience, "SELECT * FROM public.experience") .fetch_all(&pool) - .await.unwrap_or(vec![]); + .await + .unwrap_or(vec![]); Json(datas) } -async fn skills(Path(id): Path, State(pool): State) -> Json<(Vec,Vec,Vec,Vec)> { +async fn skills( + Path(id): Path, + State(pool): State, +) -> Json<( + Vec, + Vec, + Vec, + Vec, +)> { let project = sqlx::query_as!( Project, "SELECT date_done, title, description, github_link, picture_name, type_project FROM public.project WHERE project.info_id = $1 ORDER BY date_done DESC", @@ -77,7 +88,7 @@ async fn skills(Path(id): Path, State(pool): State) -> Json<(Vec, State(pool): State) -> Json<(Vec, State(pool): State) -> Json> { + let (info_id, project_id) = ids; + let datas = sqlx::query_as!( + Tags, + "SELECT lang, icon, type_icon, color + FROM public.programming_languages pl + JOIN public.project_tags pt ON pl.id = pt.programming_languages_id + WHERE pt.info_id = $1 and pt.project_id = $2 + + UNION ALL + + SELECT software, icon, type_icon, color + FROM public.softwares s + JOIN public.project_tags pt ON s.id = pt.softwares_id + WHERE pt.info_id = $1 and pt.project_id = $2; + + ", + info_id, + project_id + ) + .fetch_all(&pool) + .await + .unwrap_or(vec![]); + Json(datas) +} + +async fn alltags(Path(info_id): Path, State(pool): State) -> Json> { + let datas = sqlx::query_as!( + AllTags, + "SELECT project_id, lang, icon, type_icon, color + FROM public.programming_languages pl + JOIN public.project_tags pt ON pl.id = pt.programming_languages_id + WHERE pt.info_id = $1 + + UNION ALL + + SELECT project_id, software, icon, type_icon, color + FROM public.softwares s + JOIN public.project_tags pt ON s.id = pt.softwares_id + WHERE pt.info_id = $1; + + ", + info_id + ) + .fetch_all(&pool) + .await + .unwrap_or(vec![]); + Json(datas) }