-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
abb1449
commit 7bdac6a
Showing
6 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from dataclasses import dataclass | ||
from typing import NewType | ||
from uuid import UUID | ||
|
||
|
||
ParameterId = NewType("ParameterId", UUID) | ||
FormId = NewType("FormId", UUID) | ||
FormPopulationId = NewType("FormPopulationId", UUID) | ||
UserId = NewType("UserId", str) | ||
|
||
|
||
@dataclass | ||
class _BaseParameter: | ||
field: str | ||
answer: str | ||
|
||
@dataclass | ||
class UnidentificatedParameter(_BaseParameter): | ||
... | ||
|
||
@dataclass | ||
class IdentificatedParameter(_BaseParameter): | ||
id: ParameterId | ||
|
||
type Parameter = UnidentificatedParameter | IdentificatedParameter | ||
|
||
|
||
@dataclass | ||
class _BaseFormPopulation: | ||
user_id: UserId | ||
form_id: FormId | ||
parameters: list[Parameter] | ||
|
||
@dataclass | ||
class UnidentificatedFormPopulation(_BaseFormPopulation): | ||
... | ||
|
||
@dataclass | ||
class IdentificatedFormPopulation(_BaseFormPopulation): | ||
id: FormPopulationId | ||
|
||
type FormPopulation = UnidentificatedFormPopulation | IdentificatedFormPopulation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import typing | ||
|
||
from .models import ( | ||
FormPopulation, | ||
UnidentificatedFormPopulation, | ||
Parameter, | ||
UnidentificatedParameter, | ||
UserId, | ||
FormId, | ||
) | ||
|
||
|
||
_sentinel: typing.Any = object() | ||
|
||
|
||
class FormPopulationService: | ||
@staticmethod | ||
def create( | ||
user_id: UserId, form_id: FormId, parameters: list[Parameter] = [] | ||
) -> FormPopulation: | ||
return UnidentificatedFormPopulation( | ||
user_id=user_id, | ||
form_id=form_id, | ||
parameters=parameters, | ||
) | ||
|
||
@staticmethod | ||
def add_parameter(form_population: FormPopulation, parameter: Parameter) -> None: | ||
form_population.parameters.append(parameter) | ||
|
||
|
||
class ParameterService: | ||
@staticmethod | ||
def create(field: str, answer: str) -> Parameter: | ||
return UnidentificatedParameter(field=field, answer=answer) | ||
|
||
@staticmethod | ||
def update( | ||
parameter: Parameter, | ||
new_field: str = _sentinel, | ||
new_answer: str = _sentinel, | ||
) -> None: | ||
if new_field is not _sentinel: | ||
parameter.field = new_field | ||
|
||
if new_answer is not _sentinel: | ||
parameter.answer = new_answer |