-
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.
created relationships table, schema and dao
- Loading branch information
1 parent
43fb993
commit 54b4e1d
Showing
5 changed files
with
158 additions
and
1 deletion.
There are no files selected for viewing
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,46 @@ | ||
from typing import List | ||
|
||
from fastapi import Depends | ||
from sqlalchemy import select | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
|
||
from identity_socializer.db.dependencies import get_db_session | ||
from identity_socializer.db.models.relationship_model import RelationshipModel | ||
|
||
|
||
class RelationshipDAO: | ||
"""Class for accessing relationships table.""" | ||
|
||
def __init__(self, session: AsyncSession = Depends(get_db_session)): | ||
self.session = session | ||
|
||
async def create_relationship_model( | ||
self, | ||
follower_id: str, | ||
following_id: str, | ||
) -> None: | ||
"""Add single relationship to session.""" | ||
relationship_model = RelationshipModel( | ||
follower_id=follower_id, | ||
following_id=following_id, | ||
) | ||
|
||
self.session.add(relationship_model) | ||
|
||
async def get_following_by_id(self, user_id: str) -> List[RelationshipModel]: | ||
"""Get following users by id.""" | ||
query = select(RelationshipModel).where( | ||
RelationshipModel.follower_id == user_id, | ||
) | ||
rows = await self.session.execute(query) | ||
|
||
return list(rows.scalars().fetchall()) | ||
|
||
async def get_followers_by_id(self, user_id: str) -> List[RelationshipModel]: | ||
"""Get followers users by id.""" | ||
query = select(RelationshipModel).where( | ||
RelationshipModel.following_id == user_id, | ||
) | ||
rows = await self.session.execute(query) | ||
|
||
return list(rows.scalars().fetchall()) |
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
40 changes: 40 additions & 0 deletions
40
identity_socializer/db/migrations/versions/2023-10-15-23-56_6a777ccf275f.py
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,40 @@ | ||
"""create relationship table | ||
Revision ID: 6a777ccf275f | ||
Revises: 847eee4ea866 | ||
Create Date: 2023-10-15 23:56:33.161484 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "6a777ccf275f" | ||
down_revision = "847eee4ea866" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
op.create_table( | ||
"relationships", | ||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), | ||
sa.Column("follower_id", sa.String(), nullable=False), | ||
sa.Column("following_id", sa.String(), nullable=False), | ||
sa.Column("created_at", sa.DateTime(), nullable=False), | ||
sa.ForeignKeyConstraint( | ||
["following_id"], | ||
["users.id"], | ||
name=op.f("fk_relationships_following_id_users"), | ||
), | ||
sa.ForeignKeyConstraint( | ||
["follower_id"], | ||
["users.id"], | ||
name=op.f("fk_relationships_follower_id_users"), | ||
), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
|
||
|
||
def downgrade() -> None: | ||
op.drop_table("relationships") |
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,26 @@ | ||
import datetime | ||
|
||
from sqlalchemy import DATETIME, ForeignKey | ||
from sqlalchemy.orm import Mapped, mapped_column, relationship | ||
from sqlalchemy.sql.sqltypes import String | ||
|
||
from identity_socializer.db.base import Base | ||
|
||
|
||
class RelationshipModel(Base): | ||
"""Model for Relationship.""" | ||
|
||
__tablename__ = "relationships" | ||
|
||
length = 200 | ||
|
||
id: Mapped[int] = mapped_column(primary_key=True) | ||
follower_id: Mapped[str] = mapped_column(String(length), ForeignKey("users.id")) | ||
following_id: Mapped[str] = mapped_column(String(length), ForeignKey("users.id")) | ||
created_at: Mapped[DATETIME] = mapped_column( | ||
DATETIME, | ||
default=datetime.datetime.utcnow(), | ||
) | ||
|
||
follower = relationship("UserModel", foreign_keys=[follower_id]) | ||
following = relationship("UserModel", foreign_keys=[following_id]) |
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