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 fastapi import APIRouter, Depends, HTTPException
|
||||||
from sqlalchemy.orm import Session
|
from core.database import fake_users_db
|
||||||
from pydantic import BaseModel
|
|
||||||
from core.database import get_db
|
|
||||||
from core.auth import get_password_hash, create_access_token
|
|
||||||
import uuid
|
import uuid
|
||||||
from models.user import User
|
from typing import List, Optional
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
class UserCreate(BaseModel):
|
@router.get("/cats")
|
||||||
username: str
|
async def get_cats(
|
||||||
email: str
|
limit: Optional[int] = 10,
|
||||||
password: str
|
offset: Optional[int] = 0
|
||||||
|
|
||||||
@router.post("/signup")
|
|
||||||
async def signup(
|
|
||||||
user_data: UserCreate,
|
|
||||||
db: Session = Depends(get_db)
|
|
||||||
):
|
):
|
||||||
"""User registration endpoint"""
|
"""Get list of cats"""
|
||||||
# Check existing user
|
cats = list(fake_users_db.get("cats", {}).values())
|
||||||
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
|
|
||||||
return {
|
return {
|
||||||
"message": "User created successfully",
|
"message": "Cats retrieved successfully",
|
||||||
"access_token": create_access_token({"sub": new_user.id}),
|
"data": cats[offset:offset + limit],
|
||||||
"token_type": "bearer"
|
"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