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

db and back routes for restaurants opening hours

parent 60682715
Branches
No related tags found
2 merge requests!29Time dependence,!28improve front
......@@ -128,3 +128,44 @@ def delete_news(id: int, db: Session):
else:
db.query(models.News).filter(models.News.id == id).delete()
db.commit()
# Define CRUD operation for the opening hours
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()
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(
models.OpeningHours.place == place,
models.OpeningHours.day == day,
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.add(db_opening_hours)
db.commit()
db.refresh(db_opening_hours)
return db_opening_hours
def delete_opening_hours(id: int, db: Session):
""" Delete the opening hours with the matching id """
if id == 0:
db.query(models.OpeningHours).delete()
else:
db.query(models.OpeningHours).filter(models.OpeningHours.id == id).delete()
db.commit()
<<<<<<< HEAD
"""
Models of the database for magasin app
"""
......@@ -37,3 +38,58 @@ class News(Base):
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 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)
comment = Column(Text)
date = 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))
class OpeningHours(Base):
"""OpeningHours sql table model"""
__tablename__ = "opening_hours"
id = Column(Integer, primary_key=True, index=True)
place = Column(String(10))
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
<<<<<<< HEAD
"""
Pydantic schemas for the magasin app
"""
......@@ -52,3 +53,78 @@ class News(NewsBase):
class Config:
orm_mode = True
=======
"""
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"""
comment: 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")
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
class OpeningHoursBase(BaseModel):
"""Database opening_hours base schema"""
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")
class OpeningHours(OpeningHoursBase):
"""Database opening_hours base schema"""
id: int
class Config:
orm_mode = True
>>>>>>> db and back routes for restaurants opening hours
......@@ -4,7 +4,7 @@ from dotenv import load_dotenv
import os
from db import database, models
from routers import stats, comments, news
from routers import stats, comments, news, opening_hours
app = FastAPI(docs_url="/api/docs", openapi_url="/api/openapi.json")
......@@ -34,6 +34,7 @@ def on_startup():
app.include_router(stats.router)
app.include_router(comments.router)
app.include_router(news.router)
app.include_router(opening_hours.router)
"""
......
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/opening_hours", tags=["opening_hours"])
@router.get('/{place}', 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)
@router.get('/{place}/{day}/{timeslot}', response_model=List[schemas.OpeningHours])
async def get_timeslot(place: str, day: int, timeslot: bool, db: Session = Depends(get_db)):
return crud.get_timeslot(place, day, timeslot, db)
@router.post('/{place}', 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.delete('/{id}', response_model=None)
async def delete_opening_hours(id: int, db: Session = Depends(get_db)):
return crud.delete_opening_hours(id, db)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment