Added validators for users and recipes (#1052)

* Added validators for users and recipes

provide a simple get api, allowing to test for existence of
- user by username
- recipe by slug
- group by name (not tested yet)

* updated formatting

* Use group_id+slug for recipes, use ValidationRespone

* Fixed Flake8 errors and warnings

* add missing field for TestUser init
This commit is contained in:
Matthias Bilger
2022-03-15 23:28:42 +01:00
committed by GitHub
parent c8c02036a3
commit e109391e9a
12 changed files with 129 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
from random import randint
from typing import Any
from typing import Any, Optional
from uuid import UUID
from sqlalchemy import and_, func
@@ -143,3 +143,13 @@ class RepositoryRecipes(RepositoryGeneric[Recipe, RecipeModel]):
.order_by(func.random()) # Postgres and SQLite specific
.limit(limit)
]
def get_by_slug(self, group_id: UUID, slug: str, limit=1) -> Optional[Recipe]:
dbrecipe = (
self.session.query(RecipeModel)
.filter(RecipeModel.group_id == group_id, RecipeModel.slug == slug)
.one_or_none()
)
if dbrecipe is None:
return None
return self.schema.from_orm(dbrecipe)