diff --git a/.gitignore b/.gitignore index 31072e4..7c4f084 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ node_modules *.xml notification-domain/ TODO.txt +*.sh \ No newline at end of file diff --git a/auth-domain/user-manager/manage.py b/auth-domain/user-manager/manage.py index 7109b91..311468e 100644 --- a/auth-domain/user-manager/manage.py +++ b/auth-domain/user-manager/manage.py @@ -17,6 +17,7 @@ def recreate_db(): @cli.command("seed_db") def seed_db(): db.session.add(User(username="lufthansa", email="info@lufthansa.com", password="password1234", airline=True)) + db.session.add(User(username="ryanair", email="info@ryanair.com", password="password1234", airline=True)) db.session.add(User(username="messi", email="messi@gmail.com", password="password1234")) db.session.commit() diff --git a/flights-domain/docker-compose.yml b/flights-domain/docker-compose.yml index 5722366..393bf2d 100644 --- a/flights-domain/docker-compose.yml +++ b/flights-domain/docker-compose.yml @@ -21,6 +21,7 @@ services: condition: service_healthy networks: - flights + - subscriptions flights-api-db: container_name: fids_flights_db @@ -42,5 +43,8 @@ services: - flights networks: + subscriptions: + name: subscription-domain_subscriptions + external: true flights: driver: bridge \ No newline at end of file diff --git a/flights-domain/flights-information/requirements.txt b/flights-domain/flights-information/requirements.txt index d396de1..eb2fac7 100644 --- a/flights-domain/flights-information/requirements.txt +++ b/flights-domain/flights-information/requirements.txt @@ -3,4 +3,5 @@ fastapi[all]==0.103.2 psycopg2-binary==2.9.5 pyjwt==2.6.0 gunicorn==20.1.0 -sqlalchemy==2.0.22 \ No newline at end of file +sqlalchemy==2.0.22 +asyncreq==0.0.4 \ No newline at end of file diff --git a/flights-domain/flights-information/src/api/config.py b/flights-domain/flights-information/src/api/config.py new file mode 100644 index 0000000..65f9891 --- /dev/null +++ b/flights-domain/flights-information/src/api/config.py @@ -0,0 +1 @@ +API_MESSAGES = "http://fids_subscriptions_api:5000/messages" diff --git a/flights-domain/flights-information/src/api/cruds/flight.py b/flights-domain/flights-information/src/api/cruds/flight.py index afbeb38..130be7c 100644 --- a/flights-domain/flights-information/src/api/cruds/flight.py +++ b/flights-domain/flights-information/src/api/cruds/flight.py @@ -22,6 +22,7 @@ def create_flight(db: Session, flight: FlightPydantic): departure_time=flight.departure_time, arrival_time=flight.arrival_time, gate=flight.gate, + user_id=flight.user_id, ) db.add(db_flight) db.commit() @@ -33,8 +34,10 @@ def update_flight_status(db: Session, status, id): db_flight = db.query(Flight).filter(Flight.id == id).first() if db_flight is None: raise KeyError + if db_flight.user_id != status.user_id: + raise PermissionError - setattr(db_flight, "status", status) + setattr(db_flight, "status", status.status) setattr(db_flight, "last_updated", func.now()) db.commit() db.refresh(db_flight) diff --git a/flights-domain/flights-information/src/api/models/flight.py b/flights-domain/flights-information/src/api/models/flight.py index 4c7d999..6c9d4af 100644 --- a/flights-domain/flights-information/src/api/models/flight.py +++ b/flights-domain/flights-information/src/api/models/flight.py @@ -16,6 +16,7 @@ class Flight(Base): arrival_time = Column(DateTime, nullable=False) gate = Column(String, nullable=True) last_updated = Column(DateTime, default=func.now(), nullable=False) + user_id = Column(Integer, nullable=False) # def get_departure_time(self, format="%Y-%m-%d %I:%M %p"): # return self.departure_time.strftime(format) diff --git a/flights-domain/flights-information/src/api/routes/flights.py b/flights-domain/flights-information/src/api/routes/flights.py index bf7c310..ee32e32 100644 --- a/flights-domain/flights-information/src/api/routes/flights.py +++ b/flights-domain/flights-information/src/api/routes/flights.py @@ -1,12 +1,17 @@ from typing import Optional -from fastapi import APIRouter, Depends, HTTPException +from asyncreq import request +from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException from sqlalchemy.orm import Session +from src.api.config import API_MESSAGES from src.api.cruds import flight as flight_crud from src.api.db import get_db from src.api.schemas.flight import Flight, FlightCreate, FlightStatusUpdate +# from copy import copy + + router = APIRouter() @@ -24,9 +29,25 @@ def create_flight(flight: FlightCreate, db: Session = Depends(get_db)): @router.patch("/{id}", response_model=Flight) -def update_flight(id: int, status: FlightStatusUpdate, db: Session = Depends(get_db)): - db_flight = flight_crud.update_flight_status(db=db, id=id, status=status.status) - # push to queue with BackgroundTasks +async def update_flight( + id: int, + status: FlightStatusUpdate, + background_tasks: BackgroundTasks, + db: Session = Depends(get_db), +): + try: + db_flight = flight_crud.update_flight_status(db=db, id=id, status=status) + except PermissionError: + raise HTTPException(status_code=401, detail="Unauthorized") + except KeyError: + raise HTTPException(status_code=404, detail="Flight not found") + + msg = status.model_dump() + msg["id"] = id + msg["flight_code"] = db_flight.flight_code + msg["origin"] = db_flight.origin + msg["destination"] = db_flight.destination + background_tasks.add_task(request, API_MESSAGES, "POST", json=msg) return db_flight diff --git a/flights-domain/flights-information/src/api/schemas/flight.py b/flights-domain/flights-information/src/api/schemas/flight.py index 10ca2b5..387a0be 100644 --- a/flights-domain/flights-information/src/api/schemas/flight.py +++ b/flights-domain/flights-information/src/api/schemas/flight.py @@ -12,6 +12,7 @@ class Flight(BaseModel): departure_time: str arrival_time: str gate: str = None + user_id: int # last_updated: str # @validator("departure_time", "arrival_time", "last_updated", pre=True, always=True) @@ -30,7 +31,9 @@ class FlightCreate(BaseModel): departure_time: str arrival_time: str gate: str = None + user_id: int class FlightStatusUpdate(BaseModel): status: str + user_id: int diff --git a/gateway/src/api/routes/flights.py b/gateway/src/api/routes/flights.py index 63fb601..7b0b6e6 100644 --- a/gateway/src/api/routes/flights.py +++ b/gateway/src/api/routes/flights.py @@ -22,9 +22,11 @@ async def get_flight_by_id(id: int): async def create_flight( flight: FlightCreate, authorization: Annotated[str | None, Header()] = None ): - await checkAuth(authorization) + auth = await checkAuth(authorization) + flight_data = flight.model_dump() + flight_data["user_id"] = auth["id"] (response, status, _) = await request( - f"{API_FLIGHTS}", "POST", json=flight.model_dump() + f"{API_FLIGHTS}", "POST", json=flight_data ) if status < 200 or status > 204: raise HTTPException(status_code=status, detail=response) @@ -37,17 +39,11 @@ async def update_flight( status_update: FlightStatusUpdate, authorization: Annotated[str | None, Header()] = None, ): - await checkAuth(authorization) + auth = await checkAuth(authorization) + status = status_update.model_dump() + status["user_id"] = auth["id"] (response, status, _) = await request( - f"{API_FLIGHTS}/{id}", "PATCH", json=status_update.model_dump() - ) - if status < 200 or status > 204: - raise HTTPException(status_code=status, detail=response) - # TODO: move to flights-domain - msg = response - msg["id"] = id - (response, status, _) = await request( - f"{API_MESSAGES}", "POST", json=msg + f"{API_FLIGHTS}/{id}", "PATCH", json=status ) if status < 200 or status > 204: raise HTTPException(status_code=status, detail=response) diff --git a/gateway/src/api/routes/notifications.py b/gateway/src/api/routes/notifications.py index 0006580..cb49143 100644 --- a/gateway/src/api/routes/notifications.py +++ b/gateway/src/api/routes/notifications.py @@ -1,7 +1,6 @@ -from fastapi import APIRouter, Header, HTTPException +from fastapi import APIRouter, HTTPException -from src.api.config import (API_FLIGHTS, API_NOTIFICATIONS, API_SUBSCRIPTIONS, - API_USERS) +from src.api.config import API_NOTIFICATIONS from src.api.schemas.notification import Update as Message from src.api.utils.network import request diff --git a/gateway/src/api/routes/subscriptions.py b/gateway/src/api/routes/subscriptions.py index 73a2bc1..0c6322f 100644 --- a/gateway/src/api/routes/subscriptions.py +++ b/gateway/src/api/routes/subscriptions.py @@ -2,8 +2,7 @@ from typing import Annotated from fastapi import APIRouter, Header, HTTPException -from src.api.config import (API_FLIGHTS, API_NOTIFICATIONS, API_SUBSCRIPTIONS, - API_USERS) +from src.api.config import API_SUBSCRIPTIONS from src.api.routes.auth import status as checkAuth from src.api.schemas.subscriptions import Subscription from src.api.utils.network import request