Skip to content

Commit

Permalink
feat: init strawberry logic and architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
Angel-Dijoux committed Jan 21, 2024
1 parent 99608ef commit 67c0d23
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 43 deletions.
13 changes: 13 additions & 0 deletions src/api/favoris/favori_resolver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import strawberry

from src.business_logic.favoris.get_favoris import get_favoris_by_user_id
from src.business_logic.formation.scrap.types import FormationsWithTotal

# https://strawberry.rocks/docs/integrations/flask#options


@strawberry.type
class FavorisResolver:
get_favoris_by_user_id: FormationsWithTotal = strawberry.field(
resolver=get_favoris_by_user_id
)
12 changes: 12 additions & 0 deletions src/api/formation/formation_resolver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from src.business_logic.formation.get_formation_details import (
FormationDetails,
get_formation_by_id,
)
import strawberry


@strawberry.type
class FormationResolver:
get_formation_by_id: FormationDetails = strawberry.field(
resolver=get_formation_by_id
)
40 changes: 17 additions & 23 deletions src/api/schema.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
import typing
from src.business_logic.formation.get_formation_details import (
Formation,
get_formation_by_id,
)
from src.models import User
from src.api.favoris.favori_resolver import FavorisResolver
from src.api.formation.formation_resolver import FormationResolver
import strawberry

from src import db


@strawberry.type
class Book:
title: str
author: str

from strawberry.tools import merge_types
from strawberry.extensions import QueryDepthLimiter
from strawberry.extensions import MaxTokensLimiter
from strawberry.extensions import MaxAliasesLimiter

def get_users():
return db.session.query(User).all()
types: tuple = (FormationResolver, FavorisResolver)

Queries = merge_types("Queries", types)

@strawberry.type
class Query:
users: typing.List[User] = strawberry.field(resolver=get_users)
formation: Formation = strawberry.field(resolver=get_formation_by_id)


schema = strawberry.Schema(Query)
schema = strawberry.Schema(
query=Queries,
extensions=[
QueryDepthLimiter(max_depth=10),
MaxTokensLimiter(max_token_count=1000),
MaxAliasesLimiter(max_alias_count=15),
],
)
# https://strawberry.rocks/docs/guides/tools
3 changes: 1 addition & 2 deletions src/business_logic/favoris/get_favoris.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from src import db
from src.business_logic.formation.scrap.types import (
FormationIsFavorite,
FormationsWithTotal,
)

from src import db
from src.models.formation import Formation
from src.models.user_favori import UserFavori

Expand Down
8 changes: 4 additions & 4 deletions src/business_logic/formation/get_formation_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

@dataclass
@strawberry.type
class Formation:
class FormationDetails:
id: str
exceptions: Optional[ParcourSupExpectations]
duree: str
Expand All @@ -49,7 +49,7 @@ def _read_json_formation(for_id: str) -> Optional[dict[str, Any]]:
return result if len(result) > 0 else None


def _process_formation(for_id: str) -> Formation:
def _process_formation(for_id: str) -> FormationDetails:
formation = _read_json_formation(for_id)

if formation:
Expand All @@ -74,7 +74,7 @@ def _process_formation(for_id: str) -> Formation:
updated_at = datetime.strptime(
formation["modification_date"], DATE_FORMAT
).date()
return Formation(
return FormationDetails(
id=identifiant,
exceptions=exceptions,
duree=duree,
Expand All @@ -89,7 +89,7 @@ def _process_formation(for_id: str) -> Formation:
)


def get_formation_by_id(for_id: str) -> Formation:
def get_formation_by_id(for_id: str) -> FormationDetails:
try:
return _process_formation(for_id)
except Exception as e:
Expand Down
5 changes: 5 additions & 0 deletions src/business_logic/formation/scrap/types.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
from dataclasses import dataclass

import strawberry

from src.models.formation import Formation


@dataclass
@strawberry.type
class Facet:
key: str
doc_count: int


@dataclass
@strawberry.type
class FormationIsFavorite:
formation: Formation
is_favorite: bool


@dataclass
@strawberry.type
class FormationsWithTotal:
total: int
formations: list[Formation | FormationIsFavorite]
11 changes: 7 additions & 4 deletions src/models/base_model.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from datetime import date

import strawberry
from sqlalchemy import Column, DateTime, func
from sqlalchemy.ext.declarative import declarative_base


Model = declarative_base(name="Model")


@strawberry.type
class BaseModel(Model):
__abstract__: bool = True
created_at = Column(DateTime, nullable=False, default=func.now())
updated_at = Column(
__abstract__ = True
created_at: date = Column(DateTime, nullable=False, default=func.now())
updated_at: date = Column(
DateTime, nullable=False, default=func.now(), onupdate=func.now()
)

Expand Down
21 changes: 11 additions & 10 deletions src/models/formation.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import uuid

import strawberry
from sqlalchemy import Column, Integer, String, Text

from src.models.base_model import BaseModel
from src.models.helpers.UUIDType import UUIDType
import strawberry


def default_uuid5():
Expand All @@ -16,16 +17,16 @@ def default_uuid5():
class Formation(BaseModel):
__tablename__ = "formation"

id = Column(
id: uuid.UUID = Column(
UUIDType,
default=default_uuid5,
primary_key=True,
)
code_nsf = Column(Integer, nullable=False)
type = Column(String(255), nullable=False)
libelle = Column(String(255), nullable=False)
tutelle = Column(String(255), nullable=False)
url = Column(String(255), nullable=False, unique=True)
domain = Column(Text, nullable=False)
niveau_de_sortie = Column(String(255), nullable=False)
duree = Column(String(255), nullable=False)
code_nsf: int = Column(Integer, nullable=False)
type: str = Column(String(255), nullable=False)
libelle: str = Column(String(255), nullable=False)
tutelle: str = Column(String(255), nullable=False)
url: str = Column(String(255), nullable=False, unique=True)
domain: str = Column(Text, nullable=False)
niveau_de_sortie: str = Column(String(255), nullable=False)
duree: str = Column(String(255), nullable=False)

0 comments on commit 67c0d23

Please sign in to comment.