Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Commit

Permalink
fix: /meals/tomorrow may give weird 404 message (#35)
Browse files Browse the repository at this point in the history
* fix: /meals/tomorrow may give weird 404 message

* chore: bump version to 1.6.1
  • Loading branch information
Diego Alloza González authored Feb 8, 2022
1 parent 54c3d44 commit b49f1a0
Show file tree
Hide file tree
Showing 6 changed files with 424 additions and 366 deletions.
14 changes: 8 additions & 6 deletions app/crud/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Base CRUD operations."""

from typing import Any, Generic, List, Optional, Type, TypeVar
from datetime import date
from typing import Generic, List, Optional, Type, TypeVar

from fastapi.exceptions import HTTPException
from pydantic import BaseModel
Expand All @@ -12,18 +13,19 @@
CreateSchemaType = TypeVar("CreateSchemaType", bound=BaseModel)
UpdateSchemaType = TypeVar("UpdateSchemaType", bound=BaseModel)

_Id = date
# pylint: disable=redefined-builtin


class CRUDBase(Generic[ModelType, CreateSchemaType, UpdateSchemaType]):
"""Base CRUD class."""

def raise_not_found_error(self, *, id: Any):
def raise_not_found_error(self, *, id: _Id):
"""Raise 404 NOT FOUND."""
detail = f"{self.model.__name__} with id={id} does not exist"
raise HTTPException(404, detail)

def raise_conflict_error(self, id: Any):
def raise_conflict_error(self, id: _Id):
"""Raise 409 CONFLICT."""
detail = f"{self.model.__name__} with id={id} already exists"
raise HTTPException(409, detail)
Expand All @@ -37,11 +39,11 @@ def __init__(self, model: Type[ModelType]):
"""
self.model = model

def get(self, db: Session, id: Any) -> Optional[ModelType]:
def get(self, db: Session, id: _Id) -> Optional[ModelType]:
"""Get an object using its id."""
return db.query(self.model).filter(self.model.id == id).first()

def get_or_404(self, db: Session, id: Any) -> ModelType:
def get_or_404(self, db: Session, id: _Id) -> ModelType:
"""Get an object or return 404."""
obj = self.get(db, id=id)
if obj is not None:
Expand Down Expand Up @@ -89,7 +91,7 @@ def update(
db.refresh(db_obj)
return db_obj

def remove(self, db: Session, *, id: Any) -> None:
def remove(self, db: Session, *, id: _Id) -> None:
"""Remove an object."""
obj = self.get_or_404(db, id=id)
db.delete(obj)
Expand Down
2 changes: 1 addition & 1 deletion app/crud/crud_meal.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def get_tomorrow_or_404(self, db: Session):
"""Get tomorrow's menu or return 404."""
meal_db = self.get_tomorrow(db)
if not meal_db:
self.raise_not_found_error(id=self.get_tomorrow_date)
self.raise_not_found_error(id=self.get_tomorrow_date())
return meal_db

@staticmethod
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3.8"

services:
api:
app:
build: .
env_file:
- .env
Expand All @@ -11,6 +11,7 @@ services:
- MYSQL_HOST=database
- MYSQL_PORT=3306
- MYSQL_DATABASE=meal-planner
- WAIT_FOR_IT_ADDRESS=database:3306

# If you don't have this environment variables in your
# terminal, put them in the .env file
Expand Down
Loading

0 comments on commit b49f1a0

Please sign in to comment.