feat(main): initial commit
This commit is contained in:
38
api/routers/library.py
Normal file
38
api/routers/library.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from ninja import Router, Schema
|
||||
from typing import List, Optional
|
||||
from core.models import Library, AppUser
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
router = Router(tags=["library"])
|
||||
|
||||
class LibrarySchema(Schema):
|
||||
id: int
|
||||
name: str
|
||||
description: Optional[str] = None
|
||||
visibility: str
|
||||
owner_user_id: int
|
||||
|
||||
class LibraryCreateSchema(Schema):
|
||||
name: str
|
||||
description: Optional[str] = None
|
||||
visibility: Optional[str] = 'private'
|
||||
owner_user_id: int # In a real app with auth, this would come from request.user
|
||||
|
||||
@router.get("/", response=List[LibrarySchema])
|
||||
def list_libraries(request):
|
||||
return Library.objects.all()
|
||||
|
||||
@router.get("/{library_id}", response=LibrarySchema)
|
||||
def get_library(request, library_id: int):
|
||||
return get_object_or_404(Library, id=library_id)
|
||||
|
||||
@router.post("/", response={201: LibrarySchema})
|
||||
def create_library(request, payload: LibraryCreateSchema):
|
||||
owner = get_object_or_404(AppUser, id=payload.owner_user_id)
|
||||
library = Library.objects.create(
|
||||
owner_user=owner,
|
||||
name=payload.name,
|
||||
description=payload.description,
|
||||
visibility=payload.visibility
|
||||
)
|
||||
return 201, library
|
||||
Reference in New Issue
Block a user