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
|
from __future__ import annotations
from typing import TYPE_CHECKING
from fastapi import Depends
from sqlalchemy import ForeignKey, Integer, String
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.utils.db import Base, get_async_session
if TYPE_CHECKING:
from .user import (
User,
)
class Profile(Base):
__tablename__ = "profiles"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
user_id: Mapped[int] = mapped_column(
ForeignKey("users.id"), unique=True, nullable=False
)
avatar_file: Mapped[str | None] = mapped_column(String(255), nullable=True)
banner_file: Mapped[str | None] = mapped_column(String(255), nullable=True)
description: Mapped[str | None] = mapped_column(String(500), nullable=True)
publications_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
collections_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
subscriptions_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
followers_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
following_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
user: Mapped["User"] = relationship(
"User", back_populates="profile", uselist=False, lazy="selectin"
)
@classmethod
async def get_by_user_id(
cls, user_id: int, session: AsyncSession = Depends(get_async_session)
):
result = await session.execute(select(cls).where(cls.user_id == user_id))
return result.scalars().first()
@classmethod
async def get_by_id(
cls, profile_id: int, session: AsyncSession = Depends(get_async_session)
):
result = await session.execute(select(cls).where(cls.id == profile_id))
return result.scalars().first()
|