-
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.
- Loading branch information
1 parent
43fb993
commit 7ca130d
Showing
5 changed files
with
127 additions
and
0 deletions.
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,42 @@ | ||
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.admin_model import AdminModel | ||
|
||
|
||
class AdminDAO: | ||
"""Class for accessing admins table.""" | ||
|
||
def __init__(self, session: AsyncSession = Depends(get_db_session)): | ||
self.session = session | ||
|
||
async def create_admin_model( | ||
self, | ||
admin_id: str, | ||
email: str, | ||
) -> None: | ||
"""Add single admin to session.""" | ||
admin_model = AdminModel( | ||
id=admin_id, | ||
email=email, | ||
) | ||
|
||
self.session.add(admin_model) | ||
|
||
async def get_all_admins(self, limit: int, offset: int) -> List[AdminModel]: | ||
""" | ||
Get all admins models with limit/offset pagination. | ||
:param limit: limit of admins. | ||
:param offset: offset of admins. | ||
:return: stream of admins. | ||
""" | ||
raw_admins = await self.session.execute( | ||
select(AdminModel).limit(limit).offset(offset), | ||
) | ||
|
||
return list(raw_admins.scalars().fetchall()) |
31 changes: 31 additions & 0 deletions
31
identity_socializer/db/migrations/versions/2023-10-19-14-45_22ac5d68869b.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,31 @@ | ||
"""create admin table | ||
Revision ID: 22ac5d68869b | ||
Revises: 847eee4ea866 | ||
Create Date: 2023-10-19 14:45:52.775871 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "22ac5d68869b" | ||
down_revision = "847eee4ea866" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
length = 200 | ||
|
||
op.create_table( | ||
"admins", | ||
sa.Column("id", sa.String(length), nullable=False), | ||
sa.Column("email", sa.String(length), nullable=False), | ||
sa.Column("created_at", sa.DateTime(), nullable=False), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
|
||
|
||
def downgrade() -> None: | ||
op.drop_table("admins") |
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,21 @@ | ||
import datetime | ||
|
||
from sqlalchemy.orm import Mapped, mapped_column | ||
from sqlalchemy.sql.sqltypes import DateTime, String | ||
|
||
from identity_socializer.db.base import Base | ||
|
||
|
||
class AdminModel(Base): | ||
"""Model for Admin.""" | ||
|
||
__tablename__ = "admins" | ||
|
||
length = 200 | ||
|
||
id: Mapped[str] = mapped_column(primary_key=True) | ||
email: Mapped[str] = mapped_column(String(length)) | ||
created_at: Mapped[DateTime] = mapped_column( | ||
DateTime, | ||
default=datetime.datetime.utcnow, | ||
) |
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
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