api/app/routers/auth.py
Yoga Pangestu 7bbe4e7295 Implement subscription, tenant, and user services with CRUD operations and associated tests
- Added subscription_service.py for managing subscriptions with functions to create, read, update, and delete subscriptions.
- Added tenant_service.py for managing tenants with functions to create, read, update, delete tenants, and assign users to tenants.
- Added user_service.py for managing users with functions to create, read, update, and delete users.
- Created tests for business types, plans, subscriptions, tenants, and users, ensuring proper functionality and error handling.
- Implemented pagination in user listing and included user profiles in responses.
- Established fixtures for database setup and teardown in tests.
2026-07-17 23:36:31 +07:00

47 lines
1.3 KiB
Python

from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from app.database import get_db
from app.models.user import User
from app.models.user_profile import UserProfile
from app.schemas.user import UserCreate, UserResponse
from app.security import hash_password
router = APIRouter(prefix="/auth", tags=["auth"])
@router.post("/register", response_model=UserResponse, status_code=status.HTTP_201_CREATED)
def register(data: UserCreate, db: Session = Depends(get_db)):
existing = db.query(User).filter(
(User.email == data.email) | (User.username == data.username)
).first()
if existing:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail={"message": "Email or username already registered"},
)
user = User(
email=data.email,
username=data.username,
password=hash_password(data.password),
)
db.add(user)
db.flush()
try:
profile = UserProfile(
user_id=user.id,
full_name=data.full_name,
phone=data.phone,
timezone=data.timezone,
)
db.add(profile)
db.commit()
except:
db.rollback()
raise
db.refresh(user)
return user