48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
from ninja import Router, Schema
|
|
from typing import List, Optional
|
|
from core.models import Channel, AppUser, Library
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
router = Router(tags=["channel"])
|
|
|
|
class ChannelSchema(Schema):
|
|
id: int
|
|
name: str
|
|
slug: str
|
|
channel_number: Optional[int] = None
|
|
description: Optional[str] = None
|
|
scheduling_mode: str
|
|
library_id: int
|
|
owner_user_id: int
|
|
|
|
class ChannelCreateSchema(Schema):
|
|
name: str
|
|
slug: str
|
|
channel_number: Optional[int] = None
|
|
description: Optional[str] = None
|
|
library_id: int
|
|
owner_user_id: int # Mock Auth User
|
|
|
|
@router.get("/", response=List[ChannelSchema])
|
|
def list_channels(request):
|
|
return Channel.objects.all()
|
|
|
|
@router.get("/{channel_id}", response=ChannelSchema)
|
|
def get_channel(request, channel_id: int):
|
|
return get_object_or_404(Channel, id=channel_id)
|
|
|
|
@router.post("/", response={201: ChannelSchema})
|
|
def create_channel(request, payload: ChannelCreateSchema):
|
|
owner = get_object_or_404(AppUser, id=payload.owner_user_id)
|
|
library = get_object_or_404(Library, id=payload.library_id)
|
|
|
|
channel = Channel.objects.create(
|
|
owner_user=owner,
|
|
library=library,
|
|
name=payload.name,
|
|
slug=payload.slug,
|
|
channel_number=payload.channel_number,
|
|
description=payload.description
|
|
)
|
|
return 201, channel
|