Add user and team models with auth routes and admin endpoints

This commit is contained in:
Esa Kataja
2025-05-05 17:54:39 +03:00
parent ed10e7bf80
commit 6706abfc97
5 changed files with 162 additions and 28 deletions
+40 -8
View File
@@ -1,10 +1,9 @@
from fastapi import APIRouter
# from sqlmodel import Session, select
from fastapi import APIRouter, Body, HTTPException
# from models.user import User
# from models.group import Group
# from ..app import get_session
from models.user import CreateUser
from models.team import TeamBase, Team
from lib.db import get_connection
from lib.helpers import hash_password
router = APIRouter(prefix="/admin", tags=["Admin"])
@@ -18,8 +17,41 @@ router = APIRouter(prefix="/admin", tags=["Admin"])
# # Implementation will be added with authentication logic
# return {"is_active": is_active}
# @router.post("/users")
# async def create_user(username: str, password: str, group_id: int, session: Session = Depends(get_session)):
@router.post("/teams")
async def create_team(team: TeamBase):
with get_connection() as conn:
conn.execute("INSERT INTO Team (name) VALUES (?)", (team.name,))
return {"message": "Team created successfully"}
@router.get("/teams", response_model=list[Team])
async def list_teams():
with get_connection() as conn:
teams = conn.execute("SELECT * FROM Team").fetchdf()
return teams.to_dict(orient="records")
@router.post("/users")
async def create_user(user: CreateUser):
with get_connection() as conn:
conn.execute(
"INSERT INTO User (username, hashed_password, team_id, is_admin) VALUES (?, ?, ?, ?)",
(
user.username,
hash_password(user.password),
user.team_id,
user.is_admin,
),
)
return {"message": "User created successfully"}
@router.post("/users/")
async def disable_user(user_id: int = Body(..., embed=True)):
raise HTTPException(status_code=405, detail="Method not yet implemented")
# """
# Add a new user
# Only accessible to admin users