Skip to content
Snippets Groups Projects
Commit ba57de47 authored by Aymeric Chaumont's avatar Aymeric Chaumont
Browse files

added /api/restaurants route

parent ab5dd302
No related branches found
No related tags found
2 merge requests!30Restaurants route,!28improve front
Pipeline #43939 passed with warnings
......@@ -275,3 +275,54 @@ def delete_opening_hours(id: int, db: Session):
else:
db.query(models.OpeningHours).filter(models.OpeningHours.id == id).delete()
db.commit()
# Restaurants information
def get_restaurants(db: Session):
current_date = datetime.now(tz=pytz.timezone("Europe/Paris"))
weekday, current_time = current_date.weekday(), current_date.time()
restaurant_names = [r.place for r in db.query(models.OpeningHours.place.distinct).distinct()]
restaurants = []
for name in restaurant_names:
restaurant = {}
restaurant["name"] = name
first_timeslot = get_timeslot(name, weekday, True, db)
second_timeslot = get_timeslot(name, weekday, False, db)
if (first_timeslot and first_timeslot[0] <= current_time < first_timeslot[1]) or (
second_timeslot and second_timeslot[0] <= current_time < second_timeslot[1]):
restaurant["status"] = True
else:
restaurant["status"] = False
if first_timeslot and second_timeslot:
restaurant["timetable"] = (
f"{first_timeslot[0].hour:{'0'}{2}}h{first_timeslot[0].minute:{'0'}{2}}-"
f"{first_timeslot[1].hour:{'0'}{2}}h{first_timeslot[1].minute:{'0'}{2}} / "
f"{second_timeslot[0].hour:{'0'}{2}}h{second_timeslot[0].minute:{'0'}{2}}-"
f"{second_timeslot[1].hour:{'0'}{2}}h{second_timeslot[1].minute:{'0'}{2}}")
elif first_timeslot:
restaurant["timetable"] = (
f"{first_timeslot[0].hour:{'0'}{2}}h{first_timeslot[0].minute:{'0'}{2}}-"
f"{first_timeslot[1].hour:{'0'}{2}}h{first_timeslot[1].minute:{'0'}{2}}")
else:
restaurant["timeslot"] = "-"
if restaurant["status"]:
waiting_time = db.query(
models.Records.waiting_time
).filter(
models.Records.place == name
).order_by(
models.Records.date.desc()
).first()
waiting_time_minutes = round(waiting_time[0].total_seconds() / 60)
restaurant["waiting_time"] = waiting_time_minutes
else:
restaurant["waiting_time"] = None
restaurants.append(restaurant)
return restaurants
......@@ -11,7 +11,7 @@ class Records(Base):
__tablename__ = "records"
id = Column(Integer, primary_key=True, index=True)
place = Column(String(10))
place = Column(String(30))
date = Column(DateTime)
density = Column(Float)
waiting_time = Column(Interval)
......@@ -24,7 +24,7 @@ class Comments(Base):
id = Column(Integer, primary_key=True, index=True)
content = Column(Text)
published_at = Column(DateTime)
place = Column(String(10))
place = Column(String(30))
class News(Base):
......@@ -36,7 +36,7 @@ class News(Base):
content = Column(Text)
published_at = Column(DateTime)
end_date = Column(DateTime)
place = Column(String(10))
place = Column(String(30))
class OpeningHours(Base):
......@@ -44,7 +44,7 @@ class OpeningHours(Base):
__tablename__ = "opening_hours"
id = Column(Integer, primary_key=True, index=True)
place = Column(String(10))
place = Column(String(30))
day = Column(Integer)
timeslot = Column(Boolean)
open_time = Column(Time)
......
......@@ -4,7 +4,7 @@ from dotenv import load_dotenv
import os
from db import database, models
from routers import stats, comments, news, opening_hours
from routers import stats, comments, news, opening_hours, restaurants
app = FastAPI(docs_url="/api/docs", openapi_url="/api/openapi.json")
......@@ -35,6 +35,7 @@ app.include_router(stats.router)
app.include_router(comments.router)
app.include_router(news.router)
app.include_router(opening_hours.router)
app.include_router(restaurants.router)
"""
......
......@@ -10,8 +10,8 @@ router = APIRouter(prefix="/api", tags=["opening_hours"])
@router.get('/{place}/opening_hours', response_model=List[schemas.OpeningHours])
async def get_opening_hours(place: str, page: int = 1, db: Session = Depends(get_db)):
return crud.get_opening_hours(place, page, db)
async def get_opening_hours(place: str, db: Session = Depends(get_db)):
return crud.get_opening_hours(place, db)
@router.get('/{place}/opening_hours/{day}/{timeslot}', response_model=List[schemas.OpeningHours])
......
from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
from typing import List
from db import schemas, crud
from db.database import get_db
router = APIRouter(prefix="/api", tags=["restaurants"])
@router.get('/restaurants', response_model=List[dict])
async def get_restaurants(db: Session = Depends(get_db)):
return crud.get_restaurants(db)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment