Update code in endpoints/signup.post.py
This commit is contained in:
parent
71a3457533
commit
dd0393e596
@ -1,50 +1,53 @@
|
||||
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
|
||||
import uuid
|
||||
from models.user import User
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from core.database import fake_users_db
|
||||
from typing import List, Optional
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
class UserCreate(BaseModel):
|
||||
username: str
|
||||
email: str
|
||||
password: str
|
||||
# Simulated book database
|
||||
fake_books_db = {
|
||||
"1": {"id": "1", "title": "Sample Book 1", "author": "Author 1", "year": 2023},
|
||||
"2": {"id": "2", "title": "Sample Book 2", "author": "Author 2", "year": 2022},
|
||||
}
|
||||
|
||||
@router.post("/signup")
|
||||
async def signup(
|
||||
user_data: UserCreate,
|
||||
db: Session = Depends(get_db)
|
||||
@router.get("/books")
|
||||
async def get_books(
|
||||
skip: int = 0,
|
||||
limit: int = 10,
|
||||
search: Optional[str] = None
|
||||
):
|
||||
"""User registration endpoint"""
|
||||
# Check existing user
|
||||
db_user = db.query(User).filter(
|
||||
(User.username == user_data.username) |
|
||||
(User.email == user_data.email)
|
||||
).first()
|
||||
"""Get list of books with optional pagination and search"""
|
||||
books = list(fake_books_db.values())
|
||||
|
||||
if db_user:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Username or email already exists"
|
||||
)
|
||||
if search:
|
||||
books = [
|
||||
book for book in books
|
||||
if search.lower() in book["title"].lower() or search.lower() in book["author"].lower()
|
||||
]
|
||||
|
||||
# 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 {
|
||||
"message": "User created successfully",
|
||||
"access_token": create_access_token({"sub": new_user.id}),
|
||||
"token_type": "bearer"
|
||||
"message": "Books retrieved successfully",
|
||||
"data": books[skip:skip + limit],
|
||||
"metadata": {
|
||||
"total": len(books),
|
||||
"skip": skip,
|
||||
"limit": limit
|
||||
}
|
||||
}
|
||||
|
||||
@router.get("/books/{book_id}")
|
||||
async def get_book(
|
||||
book_id: str
|
||||
):
|
||||
"""Get specific book by ID"""
|
||||
if book_id not in fake_books_db:
|
||||
raise HTTPException(status_code=404, detail="Book not found")
|
||||
|
||||
return {
|
||||
"message": "Book retrieved successfully",
|
||||
"data": fake_books_db[book_id],
|
||||
"metadata": {
|
||||
"source": "demo_db",
|
||||
"result_count": 1
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user