Update code in endpoints/signup.post.py
This commit is contained in:
parent
4a368f0674
commit
5ed400dbc5
@ -1,50 +1,71 @@
|
||||
from fastapi import APIRouter, HTTPException, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
from pydantic import BaseModel
|
||||
from core.database import get_db
|
||||
from core.auth import get_password_hash, create_access_token
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from core.database import fake_users_db
|
||||
import uuid
|
||||
from models.user import User
|
||||
from typing import List, Optional
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
class UserCreate(BaseModel):
|
||||
username: str
|
||||
email: str
|
||||
password: str
|
||||
|
||||
@router.post("/signup")
|
||||
async def signup(
|
||||
user_data: UserCreate,
|
||||
db: Session = Depends(get_db)
|
||||
@router.get("/cats")
|
||||
async def get_cats(
|
||||
limit: Optional[int] = 10,
|
||||
offset: Optional[int] = 0
|
||||
):
|
||||
"""User registration endpoint"""
|
||||
# Check existing user
|
||||
db_user = db.query(User).filter(
|
||||
(User.username == user_data.username) |
|
||||
(User.email == user_data.email)
|
||||
).first()
|
||||
|
||||
if db_user:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Username or email already exists"
|
||||
)
|
||||
|
||||
# Create new user
|
||||
new_user = User(
|
||||
id=str(uuid.uuid4()),
|
||||
username=user_data.username,
|
||||
email=user_data.email,
|
||||
hashed_password=get_password_hash(user_data.password)
|
||||
)
|
||||
|
||||
db.add(new_user)
|
||||
db.commit()
|
||||
|
||||
# Return token directly after registration
|
||||
"""Get list of cats"""
|
||||
cats = list(fake_users_db.get("cats", {}).values())
|
||||
return {
|
||||
"message": "User created successfully",
|
||||
"access_token": create_access_token({"sub": new_user.id}),
|
||||
"token_type": "bearer"
|
||||
"message": "Cats retrieved successfully",
|
||||
"data": cats[offset:offset + limit],
|
||||
"metadata": {
|
||||
"total": len(cats),
|
||||
"limit": limit,
|
||||
"offset": offset
|
||||
}
|
||||
}
|
||||
|
||||
@router.get("/cats/{cat_id}")
|
||||
async def get_cat(
|
||||
cat_id: str
|
||||
):
|
||||
"""Get cat by ID"""
|
||||
cat = fake_users_db.get("cats", {}).get(cat_id)
|
||||
if not cat:
|
||||
raise HTTPException(status_code=404, detail="Cat not found")
|
||||
|
||||
return {
|
||||
"message": "Cat retrieved successfully",
|
||||
"data": cat,
|
||||
"metadata": {
|
||||
"source": "demo_db",
|
||||
"result_count": 1
|
||||
}
|
||||
}
|
||||
|
||||
@router.post("/cats")
|
||||
async def create_cat(
|
||||
name: str,
|
||||
breed: str,
|
||||
age: int
|
||||
):
|
||||
"""Create new cat"""
|
||||
cat_id = str(uuid.uuid4())
|
||||
|
||||
if "cats" not in fake_users_db:
|
||||
fake_users_db["cats"] = {}
|
||||
|
||||
new_cat = {
|
||||
"id": cat_id,
|
||||
"name": name,
|
||||
"breed": breed,
|
||||
"age": age
|
||||
}
|
||||
|
||||
fake_users_db["cats"][cat_id] = new_cat
|
||||
|
||||
return {
|
||||
"message": "Cat created successfully",
|
||||
"data": new_cat,
|
||||
"next_steps": [
|
||||
"Add cat photo",
|
||||
"Set feeding schedule"
|
||||
]
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user