Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created admin table #23

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions identity_socializer/db/dao/admin_dao.py
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())
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")
21 changes: 21 additions & 0 deletions identity_socializer/db/models/admin_model.py
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,
)
8 changes: 8 additions & 0 deletions identity_socializer/web/api/auth/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,11 @@ class SimpleUserModelDTO(BaseModel):
name: str
email: str
model_config = ConfigDict(from_attributes=True)


class AdminDTO(BaseModel):
"""Message model for admin register."""

id: str
email: str
model_config = ConfigDict(from_attributes=True)
25 changes: 25 additions & 0 deletions identity_socializer/web/api/auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
from fastapi import APIRouter, Depends, HTTPException
from firebase_admin import auth

from identity_socializer.db.dao.admin_dao import AdminDAO
from identity_socializer.db.dao.relationship_dao import RelationshipDAO
from identity_socializer.db.dao.user_dao import UserDAO
from identity_socializer.db.models.admin_model import AdminModel
from identity_socializer.db.models.user_model import UserModel
from identity_socializer.web.api.auth.schema import (
AdminDTO,
SecurityToken,
SimpleUserModelDTO,
Success,
Expand Down Expand Up @@ -67,6 +70,28 @@ async def get_user_models(
return await user_dao.get_all_users(limit=limit, offset=offset)


@router.post("/create_admin", response_model=None)
async def create_admin(
admin: AdminDTO,
admin_dao: AdminDAO = Depends(),
) -> None:
"""Create an admin."""
await admin_dao.create_admin_model(
admin_id=admin.id,
email=admin.email,
)


@router.get("/admins", response_model=None)
async def get_admin_models(
limit: int = 10,
offset: int = 0,
user_dao: AdminDAO = Depends(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renombrar a admin dao?

) -> List[AdminModel]:
"""Retrieve all admins from the database."""
return await user_dao.get_all_admins(limit=limit, offset=offset)


@router.get("/users/{user_id}", response_model=None)
async def get_user_model(
user_id: str,
Expand Down