-
Notifications
You must be signed in to change notification settings - Fork 20
/
voting_user.py
56 lines (43 loc) · 1.63 KB
/
voting_user.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from __future__ import annotations
from dataclasses import dataclass, field
from myapp.application.domain.model.identifier.article_id import ArticleId
from myapp.application.domain.model.identifier.user_id import UserId
from myapp.application.domain.model.karma import Karma
from myapp.application.domain.model.vote import Vote
from myapp.application.domain.model.vote_for_article_result import (
AlreadyVotedResult,
InsufficientKarmaResult,
SuccessfullyVotedResult,
VoteForArticleResult
)
MINIMUM_KARMA_REQUIRED_FOR_VOTING = Karma(5)
@dataclass
class VotingUser:
id: UserId
karma: Karma
votes_for_articles: list[ArticleVote] = field(default_factory=list)
def vote_for_article(
self,
article_id: ArticleId,
vote: Vote
) -> VoteForArticleResult:
if self._user_voted_for_article(article_id):
return AlreadyVotedResult(article_id, self.id)
if not self._karma_enough_for_voting():
return InsufficientKarmaResult(user_id=self.id)
self.votes_for_articles.append(
ArticleVote(article_id, self.id, vote)
)
return SuccessfullyVotedResult(article_id, self.id, vote)
def _karma_enough_for_voting(self):
return self.karma >= MINIMUM_KARMA_REQUIRED_FOR_VOTING
def _user_voted_for_article(self, article_id: ArticleId) -> bool:
article_ids_for_which_user_voted = (
article_vote.article_id for article_vote in self.votes_for_articles
)
return article_id in article_ids_for_which_user_voted
@dataclass
class ArticleVote:
article_id: ArticleId
user_id: UserId
vote: Vote