Update code in endpoints/signup.post.py

This commit is contained in:
Backend IM Bot 2025-03-21 10:19:21 +00:00
parent dd43767135
commit 995f6fb8b2

View File

@ -1,50 +1,32 @@
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, HTTPException
playlists = [
{
"id": "playlist1",
"songs": [
{"id": "song1", "title": "Song One", "artist": "Artist A"},
{"id": "song2", "title": "Song Two", "artist": "Artist B"}
]
}
]
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.post("/playlist/songs")
async def get_playlist_songs(
playlist_id: str = "playlist1"
):
"""User registration endpoint"""
# Check existing user
db_user = db.query(User).filter(
(User.username == user_data.username) |
(User.email == user_data.email)
).first()
"""Get songs from a playlist"""
playlist = next((p for p in playlists if p["id"] == playlist_id), None)
if not playlist:
raise HTTPException(status_code=400, detail="Playlist not found")
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 {
"message": "User created successfully",
"access_token": create_access_token({"sub": new_user.id}),
"token_type": "bearer"
"message": "Songs retrieved successfully",
"playlist_id": playlist_id,
"songs": playlist["songs"],
"features": {
"total_songs": len(playlist["songs"]),
"can_shuffle": True
}
}