175 lines
5.6 KiB
Python
175 lines
5.6 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from app.database import get_db
|
|
from app.models.models import User, Song
|
|
from app.auth import get_current_user
|
|
from pydantic import BaseModel
|
|
import os
|
|
|
|
router = APIRouter(prefix="/api/admin", tags=["admin"])
|
|
|
|
def get_admin_user(current_user: User = Depends(get_current_user)):
|
|
if not current_user.is_admin and not current_user.is_owner:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Доступ запрещен. Требуются права администратора"
|
|
)
|
|
return current_user
|
|
|
|
def get_owner_user(current_user: User = Depends(get_current_user)):
|
|
if not current_user.is_owner:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Доступ запрещен. Требуются права создателя"
|
|
)
|
|
return current_user
|
|
|
|
class UpdateSongRequest(BaseModel):
|
|
title: str
|
|
artist: str
|
|
|
|
class BanUserRequest(BaseModel):
|
|
user_id: int
|
|
is_banned: bool
|
|
|
|
class PromoteUserRequest(BaseModel):
|
|
user_id: int
|
|
is_admin: bool
|
|
|
|
@router.get("/users")
|
|
def get_all_users(
|
|
db: Session = Depends(get_db),
|
|
admin: User = Depends(get_admin_user)
|
|
):
|
|
users = db.query(User).all()
|
|
return [{
|
|
"id": user.id,
|
|
"username": user.username,
|
|
"email": user.email,
|
|
"is_admin": user.is_admin,
|
|
"is_owner": user.is_owner,
|
|
"is_banned": user.is_banned,
|
|
"created_at": user.created_at
|
|
} for user in users]
|
|
|
|
@router.get("/songs")
|
|
def get_all_songs(
|
|
db: Session = Depends(get_db),
|
|
admin: User = Depends(get_admin_user)
|
|
):
|
|
songs = db.query(Song).all()
|
|
return [{
|
|
"id": song.id,
|
|
"title": song.title,
|
|
"artist": song.artist,
|
|
"file_path": song.file_path,
|
|
"cover_path": song.cover_path,
|
|
"owner_id": song.owner_id,
|
|
"owner_username": song.owner.username if song.owner else None,
|
|
"created_at": song.created_at
|
|
} for song in songs]
|
|
|
|
@router.put("/songs/{song_id}")
|
|
def update_song(
|
|
song_id: int,
|
|
request: UpdateSongRequest,
|
|
db: Session = Depends(get_db),
|
|
admin: User = Depends(get_admin_user)
|
|
):
|
|
song = db.query(Song).filter(Song.id == song_id).first()
|
|
if not song:
|
|
raise HTTPException(status_code=404, detail="Песня не найдена")
|
|
|
|
song.title = request.title
|
|
song.artist = request.artist
|
|
db.commit()
|
|
db.refresh(song)
|
|
|
|
return {"message": "Песня обновлена", "song": {
|
|
"id": song.id,
|
|
"title": song.title,
|
|
"artist": song.artist
|
|
}}
|
|
|
|
@router.delete("/songs/{song_id}")
|
|
def delete_song(
|
|
song_id: int,
|
|
db: Session = Depends(get_db),
|
|
admin: User = Depends(get_admin_user)
|
|
):
|
|
song = db.query(Song).filter(Song.id == song_id).first()
|
|
if not song:
|
|
raise HTTPException(status_code=404, detail="Песня не найдена")
|
|
|
|
# Удаляем файлы
|
|
if song.file_path and os.path.exists(song.file_path):
|
|
os.remove(song.file_path)
|
|
if song.cover_path and os.path.exists(song.cover_path):
|
|
os.remove(song.cover_path)
|
|
|
|
db.delete(song)
|
|
db.commit()
|
|
|
|
return {"message": "Песня удалена"}
|
|
|
|
@router.post("/users/ban")
|
|
def ban_user(
|
|
request: BanUserRequest,
|
|
db: Session = Depends(get_db),
|
|
admin: User = Depends(get_admin_user)
|
|
):
|
|
user = db.query(User).filter(User.id == request.user_id).first()
|
|
if not user:
|
|
raise HTTPException(status_code=404, detail="Пользователь не найден")
|
|
|
|
if user.is_admin or user.is_owner:
|
|
raise HTTPException(status_code=400, detail="Нельзя забанить администратора или создателя")
|
|
|
|
user.is_banned = request.is_banned
|
|
db.commit()
|
|
|
|
return {"message": f"Пользователь {'забанен' if request.is_banned else 'разбанен'}"}
|
|
|
|
@router.post("/users/promote")
|
|
def promote_user(
|
|
request: PromoteUserRequest,
|
|
db: Session = Depends(get_db),
|
|
owner: User = Depends(get_owner_user)
|
|
):
|
|
user = db.query(User).filter(User.id == request.user_id).first()
|
|
if not user:
|
|
raise HTTPException(status_code=404, detail="Пользователь не найден")
|
|
|
|
if user.is_owner:
|
|
raise HTTPException(status_code=400, detail="Нельзя изменить права создателя")
|
|
|
|
user.is_admin = request.is_admin
|
|
db.commit()
|
|
|
|
return {"message": f"Пользователь {'повышен до администратора' if request.is_admin else 'понижен до пользователя'}"}
|
|
|
|
@router.delete("/users/{user_id}")
|
|
def delete_user(
|
|
user_id: int,
|
|
db: Session = Depends(get_db),
|
|
admin: User = Depends(get_admin_user)
|
|
):
|
|
user = db.query(User).filter(User.id == user_id).first()
|
|
if not user:
|
|
raise HTTPException(status_code=404, detail="Пользователь не найден")
|
|
|
|
if user.is_admin or user.is_owner:
|
|
raise HTTPException(status_code=400, detail="Нельзя удалить администратора или создателя")
|
|
|
|
# Удаляем все песни пользователя
|
|
for song in user.songs:
|
|
if song.file_path and os.path.exists(song.file_path):
|
|
os.remove(song.file_path)
|
|
if song.cover_path and os.path.exists(song.cover_path):
|
|
os.remove(song.cover_path)
|
|
|
|
db.delete(user)
|
|
db.commit()
|
|
|
|
return {"message": "Пользователь удален"}
|