ITRadio/server/proj/audio/views.py

25 lines
929 B
Python

from django.shortcuts import render
from rest_framework.viewsets import ViewSet
from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework import status
from django.shortcuts import get_object_or_404
from .schemas import SongSchema
from .models import Song
from .serializers import SongSerializer
class SongViewSet(ViewSet):
def list(self, request):
queryset = Song.objects.all()
serializer = SongSerializer(queryset, many=True)
return Response(serializer.data)
@action(detail=False, methods=['post'], schema=SongSchema())
def create_song(self, request):
serializer = SongSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
track_id = serializer.data['id']
track = get_object_or_404(Song, pk=track_id)
return Response(f"{track}", status=status.HTTP_201_CREATED)