Skip to content

Commit 233c173

Browse files
IkeSalmonsondan5e3s6ares
authored andcommitted
FEAT: Add admin create community function and endpoint, file name changed to project standard and route added to router
1 parent 51d64de commit 233c173

5 files changed

Lines changed: 20 additions & 35 deletions

File tree

app/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from fastapi import FastAPI
55
from slowapi import _rate_limit_exceeded_handler
66

7-
from app.routers.admin.admin import create_community_admin
7+
from app.routers.admin.routes import create_admin
88
from app.routers.router import setup_router as setup_router_v2
99
from app.services.database.database import AsyncSessionLocal, init_db
1010
from app.services.limiter import limiter
@@ -17,7 +17,7 @@ async def lifespan(app: FastAPI):
1717
# add check db file and create if not found
1818
await init_db()
1919
app.db_session_factory = AsyncSessionLocal()
20-
await create_community_admin(app.db_session_factory)
20+
await create_admin(app.db_session_factory)
2121
try:
2222
yield
2323
finally:
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
from app.services import auth
1010
from app.services.database.models import Community as DBCommunity # Precisa?
1111
from app.services.database.orm.community import create_community
12+
from app.services.limiter import limiter
1213

1314
# ADMIN_USER = os.getenv("ADMIN_USER")
1415
# ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD")
1516

1617

17-
async def create_community_admin(session: AsyncSession):
18+
async def create_admin(session: AsyncSession):
1819
ADMIN_USER = os.getenv("ADMIN_USER")
1920
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD")
2021
password = ADMIN_PASSWORD
@@ -25,7 +26,6 @@ async def create_community_admin(session: AsyncSession):
2526
password=hashed_password,
2627
role="admin",
2728
)
28-
session: AsyncSession = session
2929
session.add(community)
3030
await session.commit()
3131
await session.refresh(community)
@@ -37,30 +37,32 @@ class CommunityPostResponse(BaseModel):
3737

3838

3939
def setup():
40-
router = APIRouter(prefix="/admin", tags=["news"])
40+
router = APIRouter(prefix="/admin", tags=["admin"])
4141

4242
@router.post(
43-
"create_community",
43+
"/create_community",
4444
response_model=CommunityPostResponse,
45-
status_code=status.HTTP_200_OK,
45+
status_code=status.HTTP_201_CREATED,
4646
summary="Create Community endpoint",
4747
description="Create Community and returns a confirmation message",
4848
)
49+
@limiter.limit("60/minute")
4950
async def post_create_community(
5051
request: Request,
5152
admin_community: Annotated[
5253
DBCommunity, Depends(get_current_active_community)
5354
],
54-
community: Annotated[DBCommunity],
55+
community: DBCommunity,
5556
):
5657
"""
57-
Server Admin endpoint that creates Community and returns a confirmation message.
58+
Server Admin endpoint that creates Community and returns a confirmation
59+
message.
5860
"""
59-
admin_role = admin_community.get("role")
61+
admin_role = admin_community.role
6062
if admin_role != "admin":
6163
return {"status": "Unauthorized"}
62-
await create_community(
63-
session=request.app.db_session_factory, community=community
64-
)
64+
await create_community(request=request, community=community)
6565

6666
return CommunityPostResponse()
67+
68+
return router

app/routers/authentication.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,6 @@ async def authenticate_community(
7070
return None
7171
return found_community
7272

73-
# Teste
74-
75-
@router.post("/create_commumity")
76-
async def create_community(request: Request):
77-
password = "123Asd!@#"
78-
hashed_password = auth.hash_password(password)
79-
community = DBCommunity(
80-
username="username",
81-
email="username@test.com",
82-
password=hashed_password,
83-
)
84-
session: AsyncSession = request.app.db_session_factory
85-
session.add(community)
86-
await session.commit()
87-
await session.refresh(community)
88-
return {"msg": "succes? "}
89-
90-
# Teste
91-
9273
@router.post("/token", response_model=Token)
9374
@limiter.limit("60/minute")
9475
async def login_for_access_token(

app/routers/router.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from fastapi import APIRouter
22

3+
from app.routers.admin.routes import setup as admin_router_setup
34
from app.routers.authentication import setup as authentication_router_setup
45
from app.routers.healthcheck.routes import setup as healthcheck_router_setup
56
from app.routers.libraries.routes import setup as libraries_router_setup
@@ -12,4 +13,5 @@ def setup_router() -> APIRouter:
1213
router.include_router(news_router_setup(), prefix="")
1314
router.include_router(authentication_router_setup(), prefix="")
1415
router.include_router(libraries_router_setup(), prefix="")
16+
router.include_router(admin_router_setup(), prefix="")
1517
return router

app/services/encryption.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
"ENCRYPTION_KEY não está definida nas variáveis de ambiente."
1212
)
1313

14-
cipher = Fernet(ENCRYPTION_KEY.encode())
14+
cipher = Fernet(ENCRYPTION_KEY)
1515

1616

1717
def encrypt_email(email: str) -> str:
1818
"""Criptografa uma string de e-mail."""
19-
return cipher.encrypt(email.encode()).decode()
19+
return cipher.encrypt(email.encode())
2020

2121

2222
def decrypt_email(encrypted_email: str) -> str:
2323
"""Descriptografa uma string de e-mail."""
24-
return cipher.decrypt(encrypted_email.encode()).decode()
24+
return cipher.decrypt(encrypted_email).decode()

0 commit comments

Comments
 (0)