Update code in endpoints/login.post.py
This commit is contained in:
parent
4dde81e9fe
commit
9bf6e2b0d2
@ -1,39 +1,44 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from core.database import fake_users_db
|
||||
from fastapi.security import OAuth2PasswordRequestForm
|
||||
from typing import List, Optional
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.post("/login")
|
||||
async def login_handler(
|
||||
form_data: OAuth2PasswordRequestForm = Depends()
|
||||
# Simulated playlist database
|
||||
fake_playlists_db = {
|
||||
"playlist1": {
|
||||
"id": "playlist1",
|
||||
"name": "My Favorites",
|
||||
"songs": [
|
||||
{"id": "song1", "title": "Song 1", "artist": "Artist 1", "duration": "3:45"},
|
||||
{"id": "song2", "title": "Song 2", "artist": "Artist 2", "duration": "4:20"},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@router.post("/playlist/songs")
|
||||
async def get_playlist_songs(
|
||||
playlist_id: str,
|
||||
limit: Optional[int] = 10,
|
||||
offset: Optional[int] = 0
|
||||
):
|
||||
"""Authenticate user and return token"""
|
||||
user = fake_users_db.get(form_data.username)
|
||||
"""Fetch list of songs from a playlist"""
|
||||
if playlist_id not in fake_playlists_db:
|
||||
raise HTTPException(status_code=404, detail="Playlist not found")
|
||||
|
||||
if not user or user["password"] != form_data.password:
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail="Incorrect username or password",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
|
||||
if user.get("disabled"):
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Inactive user"
|
||||
)
|
||||
playlist = fake_playlists_db[playlist_id]
|
||||
songs = playlist["songs"][offset:offset + limit]
|
||||
|
||||
return {
|
||||
"message": "Login successful",
|
||||
"access_token": "dummy_jwt_token_" + form_data.username,
|
||||
"token_type": "bearer",
|
||||
"user": {
|
||||
"username": form_data.username,
|
||||
"email": user["email"]
|
||||
"message": "Songs fetched successfully",
|
||||
"data": {
|
||||
"playlist_name": playlist["name"],
|
||||
"songs": songs,
|
||||
"total_songs": len(playlist["songs"])
|
||||
},
|
||||
"features": {
|
||||
"rate_limit": 100,
|
||||
"expires_in": 3600
|
||||
"metadata": {
|
||||
"limit": limit,
|
||||
"offset": offset,
|
||||
"has_more": (offset + limit) < len(playlist["songs"])
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user