diff --git a/backend/db/crud.py b/backend/db/crud.py index fd699fca902b8f440954db14ab60b38fdd69e0d0..784e5f20d8374b0fa932739d35d618dee898a436 100644 --- a/backend/db/crud.py +++ b/backend/db/crud.py @@ -13,14 +13,38 @@ from db import models, schemas def get_waiting_time(place: str, db: Session): """ Get the last estimated waiting time for the given place """ - db_record = db.query(models.Records).filter(models.Records.place == place).order_by(models.Records.date.desc()).first() - if db_record.waiting_time is not None: - return db_record.waiting_time - else: - raise Exception + date = datetime.now(tz=pytz.timezone("Europe/Paris")) + weekday, current_time = date.weekday(), date.time() + first_timeslot = get_timeslot(place, weekday, True, db) + if first_timeslot and current_time < first_timeslot[0]: + return first_timeslot[0].hour, first_timeslot[0].minute + elif first_timeslot and current_time <= first_timeslot[1]: + waiting_time = db.query( + models.Records.waiting_time + ).filter( + models.Records.place == place + ).order_by( + models.Records.date.desc() + ).first() + waiting_time_minutes = round(waiting_time[0].total_seconds() / 60) + return waiting_time_minutes, None + second_timeslot = get_timeslot(place, weekday, False, db) + if second_timeslot and current_time < second_timeslot[0]: + return second_timeslot[0].hour, second_timeslot[0].minute + elif second_timeslot and current_time <= second_timeslot[1]: + waiting_time = db.query( + models.Records.waiting_time + ).filter( + models.Records.place == place + ).order_by( + models.Records.date.desc() + ).first() + waiting_time_minutes = round(waiting_time[0].total_seconds() / 60) + return waiting_time_minutes, None + return None, None -def get_stats(place: str, weekday: int, min_time_hour: int, min_time_mn: int, max_time_hour: int, max_time_mn: int, interval: timedelta, db: Session): +def get_stats(place: str, weekday: int, min_time: time, max_time: time, interval: timedelta, db: Session): """ Get the average waiting time for each interval between two time steps """ def shift_time(t: time, delta: timedelta): @@ -54,7 +78,6 @@ def get_stats(place: str, weekday: int, min_time_hour: int, min_time_mn: int, ma name = f'{start_time.hour:02}h{start_time.minute:02}' slots_list.append({'name': name, 'time': average_waiting_time}) - min_time, max_time = time(min_time_hour, min_time_mn), time(max_time_hour, max_time_mn) stats = [] start_time, end_time = min_time, shift_time(min_time, interval) while start_time < max_time: @@ -135,27 +158,34 @@ def delete_news(id: int, db: Session): def get_opening_hours(place: str, db: Session): """ Get the opening hours for the given place """ opening_hours = db.query( - models.OpeningHours - ).filter( - models.OpeningHours.place == place - ).order_by( - models.OpeningHours.day, models.OpeningHours.timeslot.desc() - ).all() + models.OpeningHours.day, + models.OpeningHours.timeslot, + models.OpeningHours.open_time, + models.OpeningHours.close_time, + ).filter( + models.OpeningHours.place == place + ).order_by( + models.OpeningHours.day, models.OpeningHours.timeslot.desc() + ).all() return opening_hours def get_timeslot(place: str, day: int, timeslot: bool, db: Session): """ Get the opening hours for the given place and timeslot""" - opening_hours = db.query(models.OpeningHours).filter( + opening_hours = db.query( + models.OpeningHours.open_time, + models.OpeningHours.close_time, + ).filter( models.OpeningHours.place == place, models.OpeningHours.day == day, - models.OpeningHours.timeslot == timeslot).first() + models.OpeningHours.timeslot == timeslot + ).first() return opening_hours def create_opening_hours(new_opening_hours: schemas.OpeningHoursBase, db: Session): """ Add opening hours to the database """ - db_opening_hours = models.News(**new_opening_hours.dict()) + db_opening_hours = models.OpeningHours(**new_opening_hours.dict()) db.add(db_opening_hours) db.commit() db.refresh(db_opening_hours) diff --git a/backend/db/models.py b/backend/db/models.py index 635abd73968e7c712c97f4760bfb9dfa58441007..2c05825b56d87d9403e37c3872361ad192f99396 100644 --- a/backend/db/models.py +++ b/backend/db/models.py @@ -1,48 +1,7 @@ -<<<<<<< HEAD -""" -Models of the database for magasin app -""" -from sqlalchemy import Column, Integer, DateTime, Float, Interval, String, Text - -from db.database import Base - - -class Records(Base): - """Records sql table model""" - __tablename__ = "records" - - id = Column(Integer, primary_key=True, index=True) - place = Column(String(10)) - date = Column(DateTime) - density = Column(Float) - waiting_time = Column(Interval) - - -class Comments(Base): - """Comments sql table model""" - __tablename__ = "comments" - - id = Column(Integer, primary_key=True, index=True) - content = Column(Text) - published_at = Column(DateTime) - place = Column(String(10)) - - -class News(Base): - """News sql table model""" - __tablename__ = "news" - - id = Column(Integer, primary_key=True, index=True) - title = Column(String(50)) - content = Column(Text) - published_at = Column(DateTime) - end_date = Column(DateTime) - place = Column(String(10)) -======= """ Models of the database for magasin app """ -from sqlalchemy import Column, Integer, DateTime, Float, Interval, String, Text, Boolean +from sqlalchemy import Column, Integer, DateTime, Float, Interval, String, Text, Boolean, Time from db.database import Base @@ -63,8 +22,8 @@ class Comments(Base): __tablename__ = "comments" id = Column(Integer, primary_key=True, index=True) - comment = Column(Text) - date = Column(DateTime) + content = Column(Text) + published_at = Column(DateTime) place = Column(String(10)) @@ -86,10 +45,7 @@ class OpeningHours(Base): id = Column(Integer, primary_key=True, index=True) place = Column(String(10)) - day= Column(Integer) + day = Column(Integer) timeslot = Column(Boolean) - open_hour = Column(Integer) - open_minute = Column(Integer) - close_hour = Column(Integer) - close_minute = Column(Integer) ->>>>>>> db and back routes for restaurants opening hours + open_time = Column(Time) + close_time = Column(Time) \ No newline at end of file diff --git a/backend/db/schemas.py b/backend/db/schemas.py index a915d3035b72aff4f72558b3ec5f5d37ce5d66d1..e6107329523baece2bc414e6a1c9bbace35912f2 100644 --- a/backend/db/schemas.py +++ b/backend/db/schemas.py @@ -1,64 +1,8 @@ -<<<<<<< HEAD -""" -Pydantic schemas for the magasin app -""" -from typing import Optional -from datetime import datetime, timedelta -from pydantic import BaseModel, Field - - -class RecordBase(BaseModel): - """Records base schema""" - place: str = Field(..., title="Name of the RU corresponding the given record") - date: datetime = Field(..., title="Date of the record") - density: float = Field(..., title="Estimated density of people") - waiting_time: Optional[timedelta] = Field(title="Estimated waiting time for people coming at this date") - - -class Record(RecordBase): - """Database records base schema""" - id: int - - class Config: - orm_mode = True - - -class CommentBase(BaseModel): - """Comments base schema""" - content: str = Field(..., title="Content of the comment posted") - - -class Comment(CommentBase): - """Database comments base schema""" - id: int - published_at: datetime = Field(..., title="Publication date of the comment") - place: str = Field(..., title="Name of the RU corresponding the comment") - - class Config: - orm_mode = True - - -class NewsBase(BaseModel): - """News sql table model""" - title: str = Field(..., title="Title of the news") - content: str = Field(..., title="Content of the news") - end_date: datetime = Field(..., title="End date to display the news") - place: str = Field(..., title="Name of the RU corresponding the news") - - -class News(NewsBase): - """Database news base schema""" - id: int - published_at: datetime = Field(..., title="Publication date of the news") - - class Config: - orm_mode = True -======= """ Pydantic schemas for the magasin app """ from typing import Optional -from datetime import datetime, timedelta +from datetime import datetime, timedelta, time from pydantic import BaseModel, Field @@ -80,13 +24,13 @@ class Record(RecordBase): class CommentBase(BaseModel): """Comments base schema""" - comment: str = Field(..., title="Content of the comment posted") + content: str = Field(..., title="Content of the comment posted") class Comment(CommentBase): """Database comments base schema""" id: int - date: datetime = Field(..., title="Publication date of the comment") + published_at: datetime = Field(..., title="Publication date of the comment") place: str = Field(..., title="Name of the RU corresponding the comment") class Config: @@ -115,10 +59,8 @@ class OpeningHoursBase(BaseModel): place: str = Field(..., title="Name of the RU corresponding the given record") day: int = Field(..., title="Day of the week") timeslot: bool = Field(..., title="Service slot (True for midday, False for evening)") - open_hour: int = Field(..., title="Hour of the opening time") - open_minute: int = Field(..., title="Minute of the opening time") - close_hour: int = Field(..., title="Hour of the closing time") - close_minute: int = Field(..., title="Minute of the closing time") + open_time: time = Field(..., title="Opening time") + close_time: time = Field(..., title="Closing time") class OpeningHours(OpeningHoursBase): @@ -127,4 +69,3 @@ class OpeningHours(OpeningHoursBase): class Config: orm_mode = True ->>>>>>> db and back routes for restaurants opening hours diff --git a/backend/routers/opening_hours.py b/backend/routers/opening_hours.py index 6362d1c83205ff25908fc8edbf96ad4b3d90fb0d..230d0dff744bcab1b174b61f858c8e1eaee09739 100644 --- a/backend/routers/opening_hours.py +++ b/backend/routers/opening_hours.py @@ -19,9 +19,9 @@ async def get_timeslot(place: str, day: int, timeslot: bool, db: Session = Depen return crud.get_timeslot(place, day, timeslot, db) -@router.post('/{place}/opening_hours', response_model=schemas.OpeningHours) -async def create_opening_hours(place: str, opening_hours: schemas.OpeningHoursBase, db: Session = Depends(get_db)): - return crud.create_opening_hours(place, opening_hours, db) +@router.post('/opening_hours', response_model=schemas.OpeningHours) +async def create_opening_hours(opening_hours: schemas.OpeningHoursBase, db: Session = Depends(get_db)): + return crud.create_opening_hours(opening_hours, db) @router.delete('/opening_hours/{id}', response_model=None) diff --git a/backend/routers/stats.py b/backend/routers/stats.py index 092a062ed141b9841051e9d7d5b28e81e96a375a..b3887930fc4b9bbcd6676176d30edda71724c8d7 100644 --- a/backend/routers/stats.py +++ b/backend/routers/stats.py @@ -1,7 +1,6 @@ from fastapi import APIRouter, Depends from sqlalchemy.orm import Session -from datetime import timedelta -from typing import List +from datetime import timedelta, time from db import crud from db.database import get_db @@ -10,12 +9,11 @@ from db.database import get_db router = APIRouter(prefix="/api", tags=["stats"]) -@router.get('/{place}/waiting_time', response_model=timedelta) +@router.get('/{place}/waiting_time', response_model=tuple) async def waiting_time(place: str, db: Session = Depends(get_db)): return crud.get_waiting_time(place, db) -@router.get('/{place}/stats/{day}/{min_time_hour}/{min_time_mn}/{max_time_hour}/{max_time_mn}/{interval}', response_model=list) -async def stats(place: str, day: int, min_time_hour: int, min_time_mn: int, - max_time_hour: int, max_time_mn: int, interval: timedelta, db: Session = Depends(get_db)): - return crud.get_stats(place, day, min_time_hour, min_time_mn, max_time_hour, max_time_mn, interval, db) +@router.get('/{place}/stats/{day}/{min_time}/{max_time}/{interval}', response_model=list) +async def stats(place: str, day: int, min_time: time, max_time: time, interval: timedelta, db: Session = Depends(get_db)): + return crud.get_stats(place, day, min_time, max_time, interval, db)