Update code in endpoints/signup.post.py
This commit is contained in:
parent
dd43767135
commit
995f6fb8b2
@ -1,50 +1,32 @@
|
|||||||
from fastapi import APIRouter, HTTPException, Depends
|
from fastapi import APIRouter, HTTPException
|
||||||
from sqlalchemy.orm import Session
|
|
||||||
from pydantic import BaseModel
|
playlists = [
|
||||||
from core.database import get_db
|
{
|
||||||
from core.auth import get_password_hash, create_access_token
|
"id": "playlist1",
|
||||||
import uuid
|
"songs": [
|
||||||
from models.user import User
|
{"id": "song1", "title": "Song One", "artist": "Artist A"},
|
||||||
|
{"id": "song2", "title": "Song Two", "artist": "Artist B"}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
class UserCreate(BaseModel):
|
@router.post("/playlist/songs")
|
||||||
username: str
|
async def get_playlist_songs(
|
||||||
email: str
|
playlist_id: str = "playlist1"
|
||||||
password: str
|
|
||||||
|
|
||||||
@router.post("/signup")
|
|
||||||
async def signup(
|
|
||||||
user_data: UserCreate,
|
|
||||||
db: Session = Depends(get_db)
|
|
||||||
):
|
):
|
||||||
"""User registration endpoint"""
|
"""Get songs from a playlist"""
|
||||||
# Check existing user
|
playlist = next((p for p in playlists if p["id"] == playlist_id), None)
|
||||||
db_user = db.query(User).filter(
|
if not playlist:
|
||||||
(User.username == user_data.username) |
|
raise HTTPException(status_code=400, detail="Playlist not found")
|
||||||
(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": "Songs retrieved successfully",
|
||||||
"access_token": create_access_token({"sub": new_user.id}),
|
"playlist_id": playlist_id,
|
||||||
"token_type": "bearer"
|
"songs": playlist["songs"],
|
||||||
}
|
"features": {
|
||||||
|
"total_songs": len(playlist["songs"]),
|
||||||
|
"can_shuffle": True
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user