54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
from fastapi import APIRouter, HTTPException
|
|
import MySQLdb
|
|
from database import get_db_connection
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/{project_id}")
|
|
def read_long_project(project_id: int):
|
|
conn = get_db_connection()
|
|
if not conn:
|
|
raise HTTPException(status_code=503, detail="Service de base de données indisponible.")
|
|
|
|
try:
|
|
with conn.cursor(MySQLdb.cursors.DictCursor) as cursor:
|
|
sql = """
|
|
SELECT
|
|
p.id,
|
|
p.title,
|
|
p.long_description,
|
|
p.short_description,
|
|
p.school,
|
|
p.repo_link,
|
|
p.nb_image,
|
|
p.image_name,
|
|
p.beginning_year,
|
|
p.end_year,
|
|
GROUP_CONCAT(DISTINCT s.name ORDER BY s.name SEPARATOR ', ') AS skills
|
|
FROM
|
|
projects p
|
|
LEFT JOIN
|
|
project_skills ps ON p.id = ps.project_id
|
|
LEFT JOIN
|
|
skills s ON ps.skill_id = s.id
|
|
WHERE p.id = %s
|
|
GROUP BY p.id;
|
|
"""
|
|
cursor.execute(sql, (project_id,))
|
|
result = cursor.fetchone()
|
|
|
|
if not result:
|
|
raise HTTPException(status_code=404, detail="Projet non trouvé.")
|
|
|
|
if result['skills']:
|
|
result['skills'] = result['skills'].split(', ')
|
|
else:
|
|
result['skills'] = []
|
|
|
|
return result
|
|
|
|
except MySQLdb.Error as e:
|
|
raise HTTPException(status_code=500, detail=f"Erreur lors de la requête: {e}")
|
|
finally:
|
|
conn.close()
|