-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost_entity.py
63 lines (48 loc) · 1.95 KB
/
post_entity.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
57
58
59
60
61
'''User accounts for all registered users in the application.'''
from sqlalchemy import Integer, String, ForeignKey, Text
from sqlalchemy.orm import Mapped, mapped_column, relationship
from typing import Self
from .entity_base import EntityBase
#from ..models import User
from ..models import Post
from fastapi import Depends
from .user_entity import UserEntity
from .post_votes_entity import post_votes_table
#from ..services import UserPostService
__authors__ = ['Kris Jordan']
__copyright__ = 'Copyright 2023'
__license__ = 'MIT'
class PostEntity(EntityBase):
__tablename__ = 'posts'
post_id: Mapped[int] = mapped_column(Integer, primary_key=True)
title: Mapped[str] = mapped_column(String(64), nullable = False, default = '')
content: Mapped[str] = mapped_column(Text, nullable=False, default='')
user_id: Mapped[int] = mapped_column(ForeignKey('user.id'))
user: Mapped[UserEntity] = relationship("UserEntity",back_populates='posts')
votes: Mapped[list[UserEntity]] = relationship(secondary=post_votes_table)
timestamp: Mapped[str] = mapped_column(String(64), nullable=False, default='')
@classmethod
def from_model(cls, model: Post, user: UserEntity ) -> Self:
#user_svc: UserPostService = Depends()
return cls(
post_id=model.id,
title = model.title,
content=model.content,
user = user,
votes= [],
timestamp=model.timestamp,
#user_svc.findUser(model.user)
#[user_svc.findUser(vote) for vote in model.votes]
)
def to_model(self) -> Post:
vote_num = [vote.to_model() for vote in self.votes]
return Post(
id=self.post_id,
title = self.title,
content=self.content,
user=self.user.to_model(),
votes=vote_num,
timestamp=self.timestamp,
)
def update(self, model: Post) -> None:
self.content = model.content