Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- main.py +72 -0
- requirements.txt +5 -0
main.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
load_dotenv()
|
4 |
+
from sqlalchemy import create_engine, text
|
5 |
+
from fastapi import FastAPI, Depends
|
6 |
+
|
7 |
+
DATABASE_URL = os.getenv('DATABASE_URL')
|
8 |
+
|
9 |
+
def get_engine():
|
10 |
+
if not DATABASE_URL:
|
11 |
+
raise ValueError("L'URL de la base de données (PG_URL) n'est pas définie dans l'environnement.")
|
12 |
+
engine = create_engine(DATABASE_URL)
|
13 |
+
return engine
|
14 |
+
|
15 |
+
def get_db_connection(engine=Depends(get_engine)):
|
16 |
+
with engine.connect() as connection:
|
17 |
+
yield connection
|
18 |
+
|
19 |
+
def load_job_offer(connection):
|
20 |
+
query_offre = text('SELECT entreprise, ville, poste, contrat, description_poste, publication, lien, id FROM "jobs_offers"')
|
21 |
+
resultats = connection.execute(query_offre).fetchall()
|
22 |
+
offres = []
|
23 |
+
for row in resultats:
|
24 |
+
entreprise, ville, poste, contrat, description_poste, publication, lien, id = row
|
25 |
+
offres.append({
|
26 |
+
"entreprise": entreprise,
|
27 |
+
"ville": ville,
|
28 |
+
"poste": poste,
|
29 |
+
"contrat": contrat,
|
30 |
+
"description_poste": description_poste,
|
31 |
+
"publication": publication,
|
32 |
+
"lien": lien,
|
33 |
+
"id": id
|
34 |
+
})
|
35 |
+
return offres
|
36 |
+
|
37 |
+
def get_job_offer_by_id(connection, offre_id: str):
|
38 |
+
query = text('SELECT entreprise, ville, poste, contrat, description_poste, publication, lien, id FROM "jobs_offers" WHERE id = :id')
|
39 |
+
result = connection.execute(query, {"id": offre_id}).fetchone()
|
40 |
+
if result:
|
41 |
+
entreprise, ville, poste, contrat, description_poste, publication, lien, id = result
|
42 |
+
return {
|
43 |
+
"entreprise": entreprise,
|
44 |
+
"ville": ville,
|
45 |
+
"poste": poste,
|
46 |
+
"contrat": contrat,
|
47 |
+
"description_poste": description_poste,
|
48 |
+
"publication": publication,
|
49 |
+
"lien": lien,
|
50 |
+
"id": id
|
51 |
+
}
|
52 |
+
return None
|
53 |
+
|
54 |
+
app = FastAPI()
|
55 |
+
|
56 |
+
@app.get("/offre-emploi")
|
57 |
+
async def read_job_offer(connection = Depends(get_db_connection)):
|
58 |
+
offres = load_job_offer(connection)
|
59 |
+
if offres:
|
60 |
+
return offres
|
61 |
+
return {"message": "Aucune offre d'emploi trouvée."}
|
62 |
+
|
63 |
+
@app.get("/offre-emploi/{offre_id}")
|
64 |
+
async def read_job_offer_by_id(offre_id: str, connection = Depends(get_db_connection)):
|
65 |
+
offre = get_job_offer_by_id(connection, offre_id)
|
66 |
+
if offre:
|
67 |
+
return offre
|
68 |
+
return {"message": f"Aucune offre trouvée avec l'id {offre_id}"}
|
69 |
+
|
70 |
+
if __name__ == "__main__":
|
71 |
+
import uvicorn
|
72 |
+
uvicorn.run(app, host="0.0.0.0", port=8010)
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn[standard]
|
3 |
+
python-dotenv
|
4 |
+
sqlalchemy
|
5 |
+
psycopg2-binary
|