From 57aed9863b1bd4581e1dffdcc08db5867b2c9d45 Mon Sep 17 00:00:00 2001 From: Rameez Baig Date: Sat, 14 Nov 2026 14:36:46 -0700 Subject: [PATCH 1/2] changed API endpoint to /api/v1 in the frontend & backend; removed uv from requirements as it wasn't needed to run the backend; created the docker file, docker ignore and docker compose files; updated backend imports to relative imports --- .dockerignore | 36 +++++++++++++++ Dockerfile | 42 ++++++++++++++++++ backend/app/api/routes.py | 44 +++++++++++-------- backend/app/core/dependencies.py | 4 +- backend/app/main.py | 28 ++++++++++-- .../app/repositories/restaurant_repository.py | 2 +- backend/app/schemas/menu.py | 2 +- backend/app/schemas/menu_item.py | 2 +- backend/app/schemas/menu_section.py | 2 +- backend/app/schemas/offer.py | 2 +- backend/app/schemas/restaurant.py | 2 +- backend/app/services/restaurant_service.py | 16 +++---- backend/requirements.txt | 1 - compose.yaml | 6 +++ frontend/src/api/request.ts | 4 +- frontend/vite.config.ts | 1 - 16 files changed, 153 insertions(+), 41 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 compose.yaml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..cc0c9dd --- /dev/null +++ b/.dockerignore @@ -0,0 +1,36 @@ +# Backend +**/__pycache__/ +**/*.pyc + +# Frontend +frontend/logs +frontend/*.log +frontend/npm-debug.log* +frontend/yarn-debug.log* +frontend/yarn-error.log* +frontend/pnpm-debug.log* +frontend/lerna-debug.log* +frontend/node_modules +frontend/dist +frontend/dist-ssr +frontend/*.local + +# Environment variables and secrets +**/.env +**/.env.* +**/*.pem +**/*.key + +# Git +.git/ + +# Editor directories and files +.vscode/* +!.vscode/extensions.json +.idea +.DS_Store +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f388b28 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,42 @@ +# Stage 1: Build the frontend + +FROM node:22-alpine AS frontend-build + +WORKDIR /frontend + +# Copy the files required for installing packages +COPY frontend/package.json frontend/package-lock.json ./ + +# Install packages +RUN npm ci + +# Copy the source code +COPY frontend/ ./ + +# Compile the frontend +RUN npm run build + + +# Stage 2: Prepare the backend + docker container + +FROM python:3.12-slim + +WORKDIR /app + +# Copy the requirements file +COPY backend/requirements.txt ./ + +# Install the dependencies +RUN pip install --no-cache-dir -r requirements.txt + +# Copy the source code +COPY backend/app/ ./backend/ + +# Copy the compiled frontend files +COPY --from=frontend-build /frontend/dist ./frontend/ + +# Port of the webserver +EXPOSE 8000 + +# Start the FastAPI application +CMD ["uvicorn", "backend.main:main", "--host", "0.0.0.0", "--port", "8000"] \ No newline at end of file diff --git a/backend/app/api/routes.py b/backend/app/api/routes.py index 8e63561..e1d6257 100644 --- a/backend/app/api/routes.py +++ b/backend/app/api/routes.py @@ -1,21 +1,21 @@ -from fastapi import FastAPI, HTTPException, Depends +from fastapi import FastAPI, HTTPException, Depends, APIRouter from json import JSONDecodeError -from backend.app.core.dependencies import get_restaurant_service -from backend.app.schemas.restaurant import Restaurant -from backend.app.schemas.restaurant_query import RestaurantQuery -from backend.app.services.restaurant_service import RestaurantService -from backend.app.schemas.menu_item import MenuItem -from backend.app.schemas.menu_item_request import MenuItemCreate, MenuItemUpdate -from backend.app.schemas.restaurant_request import RestaurantCreate, RestaurantUpdate -from backend.app.core.exceptions import DuplicateRestaurantError, RestaurantNotFoundError - -def create_routes(app: FastAPI): - @app.get("/health") +from ..core.dependencies import get_restaurant_service +from ..schemas.restaurant import Restaurant +from ..schemas.restaurant_query import RestaurantQuery +from ..services.restaurant_service import RestaurantService +from ..schemas.menu_item import MenuItem +from ..schemas.menu_item_request import MenuItemCreate, MenuItemUpdate +from ..schemas.restaurant_request import RestaurantCreate, RestaurantUpdate +from ..core.exceptions import DuplicateRestaurantError, RestaurantNotFoundError + +def create_routes(api: FastAPI | APIRouter): + @api.get("/health") async def health(): return {"status": "ok"} - @app.get("/restaurants", response_model=list[Restaurant]) + @api.get("/restaurants", response_model=list[Restaurant]) def list_restaurants(params: RestaurantQuery = Depends()): restaurant_service = get_restaurant_service() @@ -34,7 +34,7 @@ def list_restaurants(params: RestaurantQuery = Depends()): raise HTTPException(status_code=400, detail=str(e)) raise HTTPException(status_code=500, detail="Restaurant data failed to follow correct list[Restaurant] schema") - @app.post("/restaurants/{restaurant_id}/menu-items", response_model=MenuItem, status_code=201) + @api.post("/restaurants/{restaurant_id}/menu-items", response_model=MenuItem, status_code=201) def create_menu_item(restaurant_id: str, request: MenuItemCreate): restaurant_service = get_restaurant_service() @@ -44,7 +44,7 @@ def create_menu_item(restaurant_id: str, request: MenuItemCreate): except ValueError as e: raise HTTPException(status_code=404, detail=str(e)) - @app.patch("/restaurants/{restaurant_id}/menu-items/{menu_item_id}", response_model=MenuItem) + @api.patch("/restaurants/{restaurant_id}/menu-items/{menu_item_id}", response_model=MenuItem) def update_menu_item(restaurant_id: str, menu_item_id: str, request: MenuItemUpdate): restaurant_service = get_restaurant_service() @@ -55,7 +55,7 @@ def update_menu_item(restaurant_id: str, menu_item_id: str, request: MenuItemUpd raise HTTPException(status_code=404,detail=str(e)) - @app.delete("/restaurants/{restaurant_id}/menu-items/{menu_item_id}", status_code=204) + @api.delete("/restaurants/{restaurant_id}/menu-items/{menu_item_id}", status_code=204) def delete_menu_item(restaurant_id: str, menu_item_id: str): restaurant_service = get_restaurant_service() @@ -67,7 +67,7 @@ def delete_menu_item(restaurant_id: str, menu_item_id: str): except ValueError as e: raise HTTPException(status_code=404, detail=str(e)) - @app.get("/restaurants/{restaurant_id}", response_model=Restaurant) + @api.get("/restaurants/{restaurant_id}", response_model=Restaurant) def get_restaurant(restaurant_id: str): restaurant_service = get_restaurant_service() @@ -80,7 +80,7 @@ def get_restaurant(restaurant_id: str): raise HTTPException(status_code=500, detail="Invalid JSON data") - @app.post( + @api.post( "/restaurants", response_model=Restaurant, status_code=201, @@ -98,7 +98,7 @@ def create_restaurant(request: RestaurantCreate): raise HTTPException(status_code=409, detail=str(e)) - @app.patch( + @api.patch( "/restaurants/{restaurant_id}", response_model=Restaurant, summary="Update a restaurant's information", @@ -119,3 +119,9 @@ def update_restaurant(restaurant_id: str, request: RestaurantUpdate): except DuplicateRestaurantError as e: raise HTTPException(status_code=409, detail=str(e)) + + + @api.get("/", include_in_schema=False) + @api.get("/{path:path}", include_in_schema=False) + def api_not_found(path: str): + raise HTTPException(status_code=404, detail="Not Found") \ No newline at end of file diff --git a/backend/app/core/dependencies.py b/backend/app/core/dependencies.py index 5b15375..4a8af31 100644 --- a/backend/app/core/dependencies.py +++ b/backend/app/core/dependencies.py @@ -1,7 +1,7 @@ from pathlib import Path -from backend.app.services.restaurant_service import RestaurantService -from backend.app.repositories.restaurant_repository import RestaurantRepository +from ..services.restaurant_service import RestaurantService +from ..repositories.restaurant_repository import RestaurantRepository def get_data_dir() -> Path: data_dir = Path(__file__).resolve().parents[2] / "data" diff --git a/backend/app/main.py b/backend/app/main.py index f5e004e..f2bcb7c 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -1,10 +1,32 @@ -from fastapi import FastAPI +from fastapi import FastAPI, APIRouter +from pathlib import Path from .api.routes import create_routes def main(): - app = FastAPI() + app = FastAPI( + docs_url="/api/v1/docs", + redoc_url=None, + openapi_url="/api/v1/openapi.json", + ) + + # Prepare the API prefix + api = APIRouter(prefix="/api/v1") # Add all the routes in app.api.routes - create_routes(app) + create_routes(api) + + # Add the API routes + app.include_router(api) + + # Configure the frontend + BASE_DIR = Path(__file__).resolve().parent + FRONTEND_DIR = BASE_DIR.parent / "frontend" + + if (FRONTEND_DIR / "index.html").exists(): + app.frontend( + "/", + directory=FRONTEND_DIR, + fallback="index.html", + ) return app diff --git a/backend/app/repositories/restaurant_repository.py b/backend/app/repositories/restaurant_repository.py index 0156b73..7b50f42 100644 --- a/backend/app/repositories/restaurant_repository.py +++ b/backend/app/repositories/restaurant_repository.py @@ -3,7 +3,7 @@ from json import JSONDecodeError from pydantic import TypeAdapter, ValidationError import warnings -from backend.app.schemas.restaurant import Restaurant +from ..schemas.restaurant import Restaurant import os import tempfile diff --git a/backend/app/schemas/menu.py b/backend/app/schemas/menu.py index bb6e7a4..94d8ecf 100644 --- a/backend/app/schemas/menu.py +++ b/backend/app/schemas/menu.py @@ -1,6 +1,6 @@ from pydantic import BaseModel, Field -from backend.app.core.identifiers import generate_id +from ..core.identifiers import generate_id from .menu_section import MenuSection diff --git a/backend/app/schemas/menu_item.py b/backend/app/schemas/menu_item.py index 3f1e50a..cb3214c 100644 --- a/backend/app/schemas/menu_item.py +++ b/backend/app/schemas/menu_item.py @@ -1,6 +1,6 @@ from pydantic import BaseModel, Field -from backend.app.core.identifiers import generate_id +from ..core.identifiers import generate_id from .restricted_diet import RestrictedDiet from .offer import Offer diff --git a/backend/app/schemas/menu_section.py b/backend/app/schemas/menu_section.py index f032f83..e285490 100644 --- a/backend/app/schemas/menu_section.py +++ b/backend/app/schemas/menu_section.py @@ -2,7 +2,7 @@ from pydantic import BaseModel, Field -from backend.app.core.identifiers import generate_id +from ..core.identifiers import generate_id from .menu_item import MenuItem diff --git a/backend/app/schemas/offer.py b/backend/app/schemas/offer.py index c9b9092..053562c 100644 --- a/backend/app/schemas/offer.py +++ b/backend/app/schemas/offer.py @@ -2,7 +2,7 @@ from pydantic import BaseModel, Field -from backend.app.schemas.item_availability import ItemAvailability +from ..schemas.item_availability import ItemAvailability class Offer(BaseModel): type: str = Field("Offer", frozen=True) diff --git a/backend/app/schemas/restaurant.py b/backend/app/schemas/restaurant.py index d5af3dc..4943823 100644 --- a/backend/app/schemas/restaurant.py +++ b/backend/app/schemas/restaurant.py @@ -2,7 +2,7 @@ from pydantic import BaseModel, Field -from backend.app.core.identifiers import generate_id +from ..core.identifiers import generate_id class Restaurant(BaseModel): type: str = Field("Restaurant", frozen=True, ) diff --git a/backend/app/services/restaurant_service.py b/backend/app/services/restaurant_service.py index c7c9726..5132621 100644 --- a/backend/app/services/restaurant_service.py +++ b/backend/app/services/restaurant_service.py @@ -1,11 +1,11 @@ -from backend.app.repositories.restaurant_repository import RestaurantRepository -from backend.app.schemas.restaurant import Restaurant -from backend.app.schemas.menu_section import MenuSection -from backend.app.schemas.menu_item import MenuItem -from backend.app.schemas.menu_item_request import MenuItemCreate, MenuItemUpdate -from backend.app.schemas.menu import Menu -from backend.app.schemas.restaurant_request import RestaurantCreate, RestaurantUpdate -from backend.app.core.exceptions import DuplicateRestaurantError, RestaurantNotFoundError +from ..repositories.restaurant_repository import RestaurantRepository +from ..schemas.restaurant import Restaurant +from ..schemas.menu_section import MenuSection +from ..schemas.menu_item import MenuItem +from ..schemas.menu_item_request import MenuItemCreate, MenuItemUpdate +from ..schemas.menu import Menu +from ..schemas.restaurant_request import RestaurantCreate, RestaurantUpdate +from ..core.exceptions import DuplicateRestaurantError, RestaurantNotFoundError UNCATEGORIZED_MENU_SECTION_ID = "uncategorized" UNCATEGORIZED_MENU_SECTION_NAME = "Uncategorized" diff --git a/backend/requirements.txt b/backend/requirements.txt index 750e874..5f8d2ae 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -45,7 +45,6 @@ typer==0.27.2 typing-inspection==0.4.4 typing_extensions==4.16.0 urllib3==2.8.0 -uv==0.12.17 uvicorn==0.53.0 watchfiles==1.3.0 websockets==17.1 \ No newline at end of file diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..f1ef2c3 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,6 @@ +services: + app: + build: . + container_name: openeats + ports: + - "8080:8000" \ No newline at end of file diff --git a/frontend/src/api/request.ts b/frontend/src/api/request.ts index 61385ec..2682e42 100644 --- a/frontend/src/api/request.ts +++ b/frontend/src/api/request.ts @@ -1,3 +1,5 @@ +const API_VERSION = 1; + function errorMessage(detail: unknown, status: number): string { if (typeof detail === "string") return detail; @@ -16,7 +18,7 @@ function errorMessage(detail: unknown, status: number): string { } export async function request(path: string, init?: RequestInit): Promise { - const response = await fetch(`/api${path}`, init); + const response = await fetch(`/api/v${API_VERSION}${path}`, init); if (!response.ok) { const body = await response.json().catch(() => null); diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts index 1615347..0ce2ad4 100644 --- a/frontend/vite.config.ts +++ b/frontend/vite.config.ts @@ -9,7 +9,6 @@ export default defineConfig({ '/api': { target: 'http://127.0.0.1:8000', changeOrigin: true, - rewrite: (path) => path.replace(/^\/api/, ''), }, }, }, From cb600c04836985b985c97ba89980a39679b4b3b1 Mon Sep 17 00:00:00 2001 From: Rameez Baig Date: Sat, 14 Nov 2026 15:06:16 -0700 Subject: [PATCH 2/2] updated README with new development and production instructions; updated test endpoints; added an optional restart clause to the compose file; mounted data volume in the compose file; changed port to 8000 --- README.md | 96 +++++++++++++------ backend/tests/test_health_endpoint.py | 2 +- backend/tests/test_menu_item_routes.py | 28 +++--- backend/tests/test_restaurant_id_endpoint.py | 8 +- .../test_restaurant_management_endpoints.py | 38 ++++---- backend/tests/test_restaurants_endpoint.py | 10 +- compose.yaml | 6 +- 7 files changed, 117 insertions(+), 71 deletions(-) diff --git a/README.md b/README.md index 1b838c5..ecdf93a 100644 --- a/README.md +++ b/README.md @@ -21,25 +21,27 @@ git clone https://github.com/RealUltra/OpenEats cd OpenEats ``` -## Virtual Environment +## Development + +### Virtual Environment Create a virtual environment: -### Linux / macOS +#### Linux / macOS ```bash python3 -m venv backend/.venv source backend/.venv/bin/activate ``` -### Windows +#### Windows ```powershell python -m venv backend\.venv backend\.venv\Scripts\Activate.ps1 ``` -## Install Dependencies +### Install Dependencies Install the required dependencies: @@ -47,28 +49,28 @@ Install the required dependencies: pip install -r backend/requirements.txt ``` -## Sample Data +### Sample Data (Optional, First Run Only) To use the sample data, rename `backend/sample-data` to `backend/data`. -### Linux / macOS +#### Linux / macOS ```bash mv backend/sample-data backend/data ``` -### Windows +#### Windows ```powershell Rename-Item backend/sample-data backend/data ``` -## Running the API +### Running the API Start the application with: ```bash -uv run fastapi dev --entrypoint backend.app.main:main +fastapi dev --entrypoint backend.app.main:main ``` The application will be available at: @@ -77,13 +79,13 @@ The application will be available at: http://127.0.0.1:8000 ``` -## Running the Web UI +### Running the Web UI -### Prerequisites +#### Prerequisites Install Node.js and npm. -### Start the development server +#### Start the development server From the repository root, go into the frontend directory: @@ -105,33 +107,73 @@ npm run dev Open the URL printed by Vite (normally `http://localhost:5173/`). -### Using the UI +## Production -On the startup screen, choose **Customer** to browse restaurants or **Restaurant** to select an existing restaurant or create a new one. The restaurant view has tabs for general details and menu items. +### Prerequisites + +Install Docker with Docker Compose. + +### Sample Data (Optional, First Run Only) + +To start with the sample restaurants, move `backend/sample-data` to `data` in the repository root before starting the application. + +#### Linux / macOS + +```bash +mv backend/sample-data ./data +``` + +#### Windows + +```powershell +Move-Item backend/sample-data data +``` -### Build +### Start the Application -From the `frontend` directory, create a production build with: +From the repository root, run: ```bash -npm run build +docker compose up --build -d ``` +If there have been no changes since you last built the image, you may run: + +```bash +docker compose up -d +``` + +### Open the Application + +```text +http://127.0.0.1:8000/ +``` + +### Stop the Application + +```bash +docker compose down +``` + +## Using the UI + +On the startup screen, choose **Customer** to browse restaurants or **Restaurant** to select an existing restaurant or create a new one. The restaurant view has tabs for general details and menu items. + ## API Endpoints The API provides the following endpoints: | Method | Path | Description | |---|---|---| -| GET | `/health` | `Checks if the API is running.` | -| GET | `/restaurants` | `Lists all the restaurants.` | -| GET | `/restaurants?{query}` | `Lists all the restaurants that match searching query: name=...&hasMenu=...&sort_by=...&sort_desc=...` | -| GET | `/restaurants/{restaurant_id}` | `Retrieves a specific restaurant by its ID.` | -| POST | `/restaurants` | `Creates a restaurant. Returns 201, 409 if the same name and address already exist, 422 for invalid input.` | -| PATCH | `/restaurants/{restaurant_id}` | `Updates only the fields sent. Returns 200, 404 for an unknown ID, 409 for a duplicate, 422 for invalid input.` | -| POST | `/restaurants/{restaurant_id}/menu-items` | `Creates a new menu item. Returns 201, 404 if the restaurant is not found, 422 for invalid input.` | -| PATCH | `/restaurants/{restaurant_id}/menu-items/{menu_item_id}` | `Updates a menu item's fields. Returns 200, 404 if the item or restaurant is not found, 422 for invalid input.` | -| DELETE | `/restaurants/{restaurant_id}/menu-items/{menu_item_id}` | `Deletes a specific menu item. Returns 204 on success, 404 if not found.` | +| GET | `/api/v1/health` | `Checks if the API is running.` | +| GET | `/api/v1/restaurants` | `Lists all the restaurants.` | +| GET | `/api/v1/restaurants?{query}` | `Lists all the restaurants that match searching query: name=...&hasMenu=...&sort_by=...&sort_desc=...` | +| GET | `/api/v1/restaurants/{restaurant_id}` | `Retrieves a specific restaurant by its ID.` | +| POST | `/api/v1/restaurants` | `Creates a restaurant. Returns 201, 409 if the same name and address already exist, 422 for invalid input.` | +| PATCH | `/api/v1/restaurants/{restaurant_id}` | `Updates only the fields sent. Returns 200, 404 for an unknown ID, 409 for a duplicate, 422 for invalid input.` | +| POST | `/api/v1/restaurants/{restaurant_id}/menu-items` | `Creates a new menu item. Returns 201, 404 if the restaurant is not found, 422 for invalid input.` | +| PATCH | `/api/v1/restaurants/{restaurant_id}/menu-items/{menu_item_id}` | `Updates a menu item's fields. Returns 200, 404 if the item or restaurant is not found, 422 for invalid input.` | +| DELETE | `/api/v1/restaurants/{restaurant_id}/menu-items/{menu_item_id}` | `Deletes a specific menu item. Returns 204 on success, 404 if not found.` | ## API Documentation @@ -139,7 +181,7 @@ The API provides the following endpoints: API documentation is available at: ```text -http://127.0.0.1:8000/docs +http://127.0.0.1:8000/api/v1/docs ``` ## Running Tests diff --git a/backend/tests/test_health_endpoint.py b/backend/tests/test_health_endpoint.py index de3a171..feadf45 100644 --- a/backend/tests/test_health_endpoint.py +++ b/backend/tests/test_health_endpoint.py @@ -1,3 +1,3 @@ def test_health_endpoint_returns_200(client): - response = client.get("/health") + response = client.get("/api/v1/health") assert response.status_code == 200 \ No newline at end of file diff --git a/backend/tests/test_menu_item_routes.py b/backend/tests/test_menu_item_routes.py index 4ed7445..11565b0 100644 --- a/backend/tests/test_menu_item_routes.py +++ b/backend/tests/test_menu_item_routes.py @@ -1,6 +1,6 @@ def test_create_menu_item_endpoint(client): response = client.post( - "/restaurants/NwYJoaOHusWdiqXpM7mQH/menu-items", + "/api/v1/restaurants/NwYJoaOHusWdiqXpM7mQH/menu-items", json={ "name": "Test Mocha", "description": "Chocolate and espresso", @@ -25,7 +25,7 @@ def test_create_menu_item_endpoint(client): def test_create_menu_item_endpoint_invalid_restaurant(client): response = client.post( - "/restaurants/not-a-real-id/menu-items", + "/api/v1/restaurants/not-a-real-id/menu-items", json={ "name": "Test Mocha", "description": "Chocolate and espresso", @@ -48,7 +48,7 @@ def test_create_menu_item_endpoint_invalid_restaurant(client): # def test_create_menu_item_endpoint_invalid_section(client): # response = client.post( -# "/restaurants/NwYJoaOHusWdiqXpM7mQH/menu-items", +# "/api/v1/restaurants/NwYJoaOHusWdiqXpM7mQH/menu-items", # json={ # "name": "Test Mocha", # "description": "Chocolate and espresso", @@ -68,14 +68,14 @@ def test_create_menu_item_endpoint_invalid_restaurant(client): def test_create_menu_item_endpoint_invalid_body(client): response = client.post( - "/restaurants/NwYJoaOHusWdiqXpM7mQH/menu-items", + "/api/v1/restaurants/NwYJoaOHusWdiqXpM7mQH/menu-items", json={"description": "Missing required fields"}) assert response.status_code == 422 def test_update_menu_item_endpoint(client): response = client.patch( - "/restaurants/NwYJoaOHusWdiqXpM7mQH/menu-items/F5hn9s3VD9qzKsLV4z8CW", + "/api/v1/restaurants/NwYJoaOHusWdiqXpM7mQH/menu-items/F5hn9s3VD9qzKsLV4z8CW", json={"description": "Updated through API"}) assert response.status_code == 200 @@ -83,24 +83,24 @@ def test_update_menu_item_endpoint(client): assert data["description"] == "Updated through API" def test_delete_menu_item_endpoint(client): - response = client.delete("/restaurants/NwYJoaOHusWdiqXpM7mQH/menu-items/F5hn9s3VD9qzKsLV4z8CW") + response = client.delete("/api/v1/restaurants/NwYJoaOHusWdiqXpM7mQH/menu-items/F5hn9s3VD9qzKsLV4z8CW") assert response.status_code == 204 def test_delete_menu_item_endpoint_invalid_item(client): - response = client.delete("/restaurants/NwYJoaOHusWdiqXpM7mQH/menu-items/not-a-real-item") + response = client.delete("/api/v1/restaurants/NwYJoaOHusWdiqXpM7mQH/menu-items/not-a-real-item") assert response.status_code == 404 def test_delete_menu_item_endpoint_invalid_restaurant(client): - response = client.delete("/restaurants/not-a-real-restaurant/menu-items/F5hn9s3VD9qzKsLV4z8CW") + response = client.delete("/api/v1/restaurants/not-a-real-restaurant/menu-items/F5hn9s3VD9qzKsLV4z8CW") assert response.status_code == 404 # def test_delete_menu_item_endpoint_invalid_section(client): -# response = client.delete("/restaurants/NwYJoaOHusWdiqXpM7mQH/menu-sections/not-a-real-section/items/F5hn9s3VD9qzKsLV4z8CW") +# response = client.delete("/api/v1/restaurants/NwYJoaOHusWdiqXpM7mQH/menu-sections/not-a-real-section/items/F5hn9s3VD9qzKsLV4z8CW") # assert response.status_code == 404 def test_create_menu_item_rejects_extra_fields(client): - response = client.post("/restaurants/NwYJoaOHusWdiqXpM7mQH/menu-items", + response = client.post("/api/v1/restaurants/NwYJoaOHusWdiqXpM7mQH/menu-items", json={ "name": "Test Lemon Tea", "description": "Test item", @@ -120,7 +120,7 @@ def test_create_menu_item_rejects_extra_fields(client): def test_create_menu_item_strips_whitespace(client): - response = client.post("/restaurants/NwYJoaOHusWdiqXpM7mQH/menu-items", + response = client.post("/api/v1/restaurants/NwYJoaOHusWdiqXpM7mQH/menu-items", json={ "name": " Test Lemon Tea ", "description": " Lemon and Tea ", @@ -144,14 +144,14 @@ def test_create_menu_item_strips_whitespace(client): def test_update_menu_item_rejects_empty_body(client): - response = client.patch("/restaurants/NwYJoaOHusWdiqXpM7mQH/menu-items/F5hn9s3VD9qzKsLV4z8CW", json={}) + response = client.patch("/api/v1/restaurants/NwYJoaOHusWdiqXpM7mQH/menu-items/F5hn9s3VD9qzKsLV4z8CW", json={}) assert response.status_code == 422 def test_create_menu_item_rejects_blank_name(client): response = client.post( - "/restaurants/NwYJoaOHusWdiqXpM7mQH/menu-items", + "/api/v1/restaurants/NwYJoaOHusWdiqXpM7mQH/menu-items", json={ "name": " ", "description": "Test", @@ -170,7 +170,7 @@ def test_create_menu_item_rejects_blank_name(client): def test_create_menu_item_rejects_negative_price(client): response = client.post( - "/restaurants/NwYJoaOHusWdiqXpM7mQH/menu-items", + "/api/v1/restaurants/NwYJoaOHusWdiqXpM7mQH/menu-items", json={ "name": "Test Item", "description": "Test", diff --git a/backend/tests/test_restaurant_id_endpoint.py b/backend/tests/test_restaurant_id_endpoint.py index 5e8ffc2..6716403 100644 --- a/backend/tests/test_restaurant_id_endpoint.py +++ b/backend/tests/test_restaurant_id_endpoint.py @@ -4,7 +4,7 @@ def get_valid_restaurant_id(client): """Helper function to grab a real ID from the database for testing.""" - response = client.get("/restaurants") + response = client.get("/api/v1/restaurants") data = response.json() if response.status_code == 200 and len(data) > 0: return data[0]["id"] @@ -17,7 +17,7 @@ def test_restaurant_id_endpoint_returns_200(client): warnings.warn("This couldn't be tested correctly because there were no restaurants.") return - response = client.get(f"/restaurants/{test_id}") + response = client.get(f"/api/v1/restaurants/{test_id}") assert response.status_code == 200, f"Failed! Server returned: {response.json()}" def test_restaurant_id_endpoint_returns_valid_json(client): @@ -27,7 +27,7 @@ def test_restaurant_id_endpoint_returns_valid_json(client): warnings.warn("This couldn't be tested correctly because there were no restaurants.") return - response = client.get(f"/restaurants/{test_id}") + response = client.get(f"/api/v1/restaurants/{test_id}") assert response.status_code == 200, f"Failed! Server returned: {response.json()}" data = response.json() @@ -37,7 +37,7 @@ def test_restaurant_id_endpoint_returns_valid_json(client): assert restaurant.id == test_id def test_restaurant_id_endpoint_returns_404_for_invalid_id(client): - response = client.get("/restaurants/this-id-does-not-exist-999") + response = client.get("/api/v1/restaurants/this-id-does-not-exist-999") assert response.status_code == 404, f"Expected 404, but got: {response.status_code}" data = response.json() diff --git a/backend/tests/test_restaurant_management_endpoints.py b/backend/tests/test_restaurant_management_endpoints.py index a29460e..70ed1c3 100644 --- a/backend/tests/test_restaurant_management_endpoints.py +++ b/backend/tests/test_restaurant_management_endpoints.py @@ -13,7 +13,7 @@ def new_restaurant(**changes): # POST /restaurants def test_create_restaurant_returns_201_with_new_restaurant(client): - response = client.post("/restaurants", json=new_restaurant()) + response = client.post("/api/v1/restaurants", json=new_restaurant()) assert response.status_code == 201 data = response.json() @@ -22,11 +22,11 @@ def test_create_restaurant_returns_201_with_new_restaurant(client): assert data["hasMenu"] is None def test_created_restaurant_is_saved(client): - created = client.post("/restaurants", json=new_restaurant()).json() + created = client.post("/api/v1/restaurants", json=new_restaurant()).json() # A fresh app reads from the JSON file again, like after a restart restarted_client = TestClient(main()) - response = restarted_client.get(f"/restaurants/{created['id']}") + response = restarted_client.get(f"/api/v1/restaurants/{created['id']}") assert response.status_code == 200 assert response.json()["name"] == "Taco Town" @@ -35,28 +35,28 @@ def test_create_restaurant_with_missing_field_returns_422(client): data = new_restaurant() del data["address"] - response = client.post("/restaurants", json=data) + response = client.post("/api/v1/restaurants", json=data) assert response.status_code == 422 def test_create_restaurant_with_id_returns_422(client): - response = client.post("/restaurants", json=new_restaurant(id="my-own-id")) + response = client.post("/api/v1/restaurants", json=new_restaurant(id="my-own-id")) assert response.status_code == 422 def test_create_duplicate_restaurant_returns_409(client): - existing = client.get("/restaurants").json()[0] + existing = client.get("/api/v1/restaurants").json()[0] - response = client.post("/restaurants", json=new_restaurant(name=existing["name"], address=existing["address"])) + response = client.post("/api/v1/restaurants", json=new_restaurant(name=existing["name"], address=existing["address"])) assert response.status_code == 409 # PATCH /restaurants/{restaurant_id} def test_update_restaurant_changes_only_fields_sent(client): - existing = client.get("/restaurants").json()[0] + existing = client.get("/api/v1/restaurants").json()[0] - response = client.patch(f"/restaurants/{existing['id']}", json={"telephone": "+1-250-555-0199"}) + response = client.patch(f"/api/v1/restaurants/{existing['id']}", json={"telephone": "+1-250-555-0199"}) assert response.status_code == 200 data = response.json() @@ -65,36 +65,36 @@ def test_update_restaurant_changes_only_fields_sent(client): assert data["hasMenu"] == existing["hasMenu"] def test_updated_restaurant_is_saved(client): - existing = client.get("/restaurants").json()[0] - client.patch(f"/restaurants/{existing['id']}", json={"name": "Renamed Cafe"}) + existing = client.get("/api/v1/restaurants").json()[0] + client.patch(f"/api/v1/restaurants/{existing['id']}", json={"name": "Renamed Cafe"}) restarted_client = TestClient(main()) - response = restarted_client.get(f"/restaurants/{existing['id']}") + response = restarted_client.get(f"/api/v1/restaurants/{existing['id']}") assert response.json()["name"] == "Renamed Cafe" def test_update_unknown_restaurant_returns_404(client): - response = client.patch("/restaurants/does-not-exist", json={"name": "New Name"}) + response = client.patch("/api/v1/restaurants/does-not-exist", json={"name": "New Name"}) assert response.status_code == 404 def test_update_with_no_fields_returns_422(client): - existing = client.get("/restaurants").json()[0] + existing = client.get("/api/v1/restaurants").json()[0] - response = client.patch(f"/restaurants/{existing['id']}", json={}) + response = client.patch(f"/api/v1/restaurants/{existing['id']}", json={}) assert response.status_code == 422 def test_update_with_invalid_opening_hours_returns_422(client): - existing = client.get("/restaurants").json()[0] + existing = client.get("/api/v1/restaurants").json()[0] - response = client.patch(f"/restaurants/{existing['id']}", json={"openingHours": "always open"}) + response = client.patch(f"/api/v1/restaurants/{existing['id']}", json={"openingHours": "always open"}) assert response.status_code == 422 def test_update_into_duplicate_returns_409(client): - first, second = client.get("/restaurants").json()[:2] + first, second = client.get("/api/v1/restaurants").json()[:2] - response = client.patch(f"/restaurants/{first['id']}", json={"name": second["name"], "address": second["address"]}) + response = client.patch(f"/api/v1/restaurants/{first['id']}", json={"name": second["name"], "address": second["address"]}) assert response.status_code == 409 diff --git a/backend/tests/test_restaurants_endpoint.py b/backend/tests/test_restaurants_endpoint.py index 4e6023d..5c9aeeb 100644 --- a/backend/tests/test_restaurants_endpoint.py +++ b/backend/tests/test_restaurants_endpoint.py @@ -5,11 +5,11 @@ def test_restaurants_endpoint_returns_200(client): - response = client.get("/restaurants") + response = client.get("/api/v1/restaurants") assert response.status_code == 200, f"Failed! Server returned: {response.json()}" def test_restaurants_endpoint_returns_valid_json(client): - response = client.get("/restaurants") + response = client.get("/api/v1/restaurants") assert response.status_code == 200, f"Failed! Server returned: {response.json()}" data = response.json() @@ -21,7 +21,7 @@ def test_restaurants_endpoint_returns_valid_json(client): warnings.warn("This couldn't be tested correctly because there were no restaurants.") def test_restaurants_endpoint_with_name_query(client): - response = client.get("/restaurants?name=Starbucks") + response = client.get("/api/v1/restaurants?name=Starbucks") assert response.status_code == 200, f"Failed! Server returned: {response.json()}" data = response.json() @@ -33,7 +33,7 @@ def test_restaurants_endpoint_with_name_query(client): warnings.warn("This couldn't be tested correctly because there were no restaurants.") def test_restaurants_endpoint_with_has_menu_query(client): - response = client.get("/restaurants?hasMenu=true") + response = client.get("/api/v1/restaurants?hasMenu=true") assert response.status_code == 200, f"Failed! Server returned: {response.json()}" data = response.json() @@ -45,7 +45,7 @@ def test_restaurants_endpoint_with_has_menu_query(client): warnings.warn("This couldn't be tested correctly because there were no restaurants.") def test_restaurants_endpoint_with_invalid_sort_by_query(client): - response = client.get("/restaurants?sort_by=non_existent_field") + response = client.get("/api/v1/restaurants?sort_by=non_existent_field") assert response.status_code == 400 assert "Invalid sort_by" in response.json()["detail"] diff --git a/compose.yaml b/compose.yaml index f1ef2c3..416c01f 100644 --- a/compose.yaml +++ b/compose.yaml @@ -3,4 +3,8 @@ services: build: . container_name: openeats ports: - - "8080:8000" \ No newline at end of file + - "8000:8000" + volumes: + - ./data:/app/data + # Uncomment this if running it in production + #restart: unless-stopped \ No newline at end of file