Backend: Added tags for each project.

This commit is contained in:
Yohan Boujon 2024-01-28 17:54:04 +01:00
parent 44d3af1595
commit 7d829cc22f
3 changed files with 100 additions and 8 deletions

View file

@ -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)
);

View file

@ -68,3 +68,20 @@ pub struct Languages {
pub icon_alpha: String,
pub level: String
}
#[derive(Deserialize, Serialize)]
pub struct Tags {
pub lang: Option<String>,
pub icon: Option<String>,
pub type_icon: Option<String>,
pub color: Option<String>
}
#[derive(Deserialize, Serialize)]
pub struct AllTags {
pub project_id: Option<i32>,
pub lang: Option<String>,
pub icon: Option<String>,
pub type_icon: Option<String>,
pub color: Option<String>
}

View file

@ -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<PgPool>) -> Json<Vec<Info>>
{
async fn info(State(pool): State<PgPool>) -> Json<Vec<Info>> {
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<PgPool>) -> Json<Vec<Info>>
async fn education(State(pool): State<PgPool>) -> Json<Vec<Education>> {
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<PgPool>) -> Json<Vec<Experience>> {
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<i32>, State(pool): State<PgPool>) -> Json<(Vec<Project>,Vec<ProgrammingLanguages>,Vec<Softwares>,Vec<Languages>)> {
async fn skills(
Path(id): Path<i32>,
State(pool): State<PgPool>,
) -> Json<(
Vec<Project>,
Vec<ProgrammingLanguages>,
Vec<Softwares>,
Vec<Languages>,
)> {
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",
@ -102,5 +113,55 @@ async fn skills(Path(id): Path<i32>, State(pool): State<PgPool>) -> Json<(Vec<Pr
.fetch_all(&pool)
.await.unwrap_or(vec![]);
Json((project,programming_languages,softwares,languages))
Json((project, programming_languages, softwares, languages))
}
async fn tags(Path(ids): Path<(i32, i32)>, State(pool): State<PgPool>) -> Json<Vec<Tags>> {
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<i32>, State(pool): State<PgPool>) -> Json<Vec<AllTags>> {
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)
}