45 lines
1.3 KiB
Python
45 lines
1.3 KiB
Python
from sqlalchemy.orm import Session
|
|
|
|
from src.api.models.subscription import Subscription
|
|
from src.api.schemas.subscription import FlightData
|
|
from src.api.schemas.subscription import Subscription as SubscriptionPydantic
|
|
|
|
|
|
def get_subscriptions(db: Session, user_id: int):
|
|
return db.query(Subscription).filter(Subscription.user_id == user_id).all()
|
|
|
|
|
|
def get_subscription(db: Session, user_id: int, flight_id: int):
|
|
return (
|
|
db.query(Subscription)
|
|
.filter(Subscription.user_id == user_id, Subscription.flight_id == flight_id)
|
|
.first()
|
|
)
|
|
|
|
|
|
def create_subscription(db: Session, subscription: SubscriptionPydantic):
|
|
if get_subscription(
|
|
db, user_id=subscription.user_id, flight_id=subscription.flight_id
|
|
):
|
|
raise ValueError
|
|
|
|
db_subscription = Subscription(
|
|
user_id=subscription.user_id,
|
|
flight_id=subscription.flight_id,
|
|
)
|
|
db.add(db_subscription)
|
|
db.commit()
|
|
db.refresh(db_subscription)
|
|
return db_subscription
|
|
|
|
|
|
def remove_subscription(db: Session, user_id: int, flight_id: int):
|
|
db.query(Subscription).filter(
|
|
Subscription.user_id == user_id, Subscription.flight_id == flight_id
|
|
).delete()
|
|
db.commit()
|
|
|
|
|
|
def send_subscriptions(db: Session, flight: FlightData):
|
|
return db.query(Subscription).filter(Subscription.flight_id == flight.id).all()
|