Skip to content

Commit

Permalink
created relationships table, schema and dao
Browse files Browse the repository at this point in the history
  • Loading branch information
noxethiems committed Oct 16, 2023
1 parent 43fb993 commit 54b4e1d
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 1 deletion.
46 changes: 46 additions & 0 deletions identity_socializer/db/dao/relationship_dao.py
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())
6 changes: 6 additions & 0 deletions identity_socializer/db/dao/user_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,9 @@ async def filter(
query = query.where(UserModel.first_name == first_name)
rows = await self.session.execute(query)
return list(rows.scalars().fetchall())

async def get_user_by_id(self, user_id: str) -> Optional[UserModel]:
"""Get specific user model."""
query = select(UserModel).where(UserModel.id == user_id)
rows = await self.session.execute(query)
return rows.scalars().first()
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")
26 changes: 26 additions & 0 deletions identity_socializer/db/models/relationship_model.py
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])
41 changes: 40 additions & 1 deletion identity_socializer/web/api/auth/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from typing import List
from typing import List, Optional

from fastapi import APIRouter, Depends, HTTPException
from firebase_admin import auth

from identity_socializer.db.dao.relationship_dao import RelationshipDAO
from identity_socializer.db.dao.user_dao import UserDAO
from identity_socializer.db.models.relationship_model import RelationshipModel
from identity_socializer.db.models.user_model import UserModel
from identity_socializer.web.api.auth.schema import (
SecurityToken,
Expand Down Expand Up @@ -64,3 +66,40 @@ async def get_user_models(
:return: list of users objects from database.
"""
return await user_dao.get_all_users(limit=limit, offset=offset)


@router.get("/users/{user_id}", response_model=None)
async def get_user_model(
user_id: str,
user_dao: UserDAO = Depends(),
) -> Optional[UserModel]:
"""Retrieve a user object from the database."""
return await user_dao.get_user_by_id(user_id)


@router.get("/follow_user/{user_id}/{followed_user_id}", response_model=None)
async def follow_user(
user_id: str,
followed_user_id: str,
relationship_dao: RelationshipDAO = Depends(),
) -> None:
"""Follow a user."""
await relationship_dao.create_relationship_model(user_id, followed_user_id)


@router.get("/get_following/{user_id}", response_model=None)
async def get_following(
user_id: str,
relationship_dao: RelationshipDAO = Depends(),
) -> List[RelationshipModel]:
"""Get following users."""
return await relationship_dao.get_following_by_id(user_id)


@router.get("/get_followers/{user_id}", response_model=None)
async def get_followers(
user_id: str,
relationship_dao: RelationshipDAO = Depends(),
) -> List[RelationshipModel]:
"""Get followers users."""
return await relationship_dao.get_followers_by_id(user_id)

0 comments on commit 54b4e1d

Please sign in to comment.