59 lines
2.0 KiB
Python
59 lines
2.0 KiB
Python
from fastapi import APIRouter, Header, HTTPException, status
|
|
from pydantic import BaseModel
|
|
|
|
from backend.app.services.link_service import create_link, list_public_links, update_link
|
|
from backend.app.services.plugin_manager import plugin_manager
|
|
from backend.app.services.token_service import validate_token
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
class LinkCreate(BaseModel):
|
|
title: str
|
|
url: str
|
|
comment: str = ''
|
|
timestamp: str | None = None
|
|
|
|
|
|
class LinkUpdate(BaseModel):
|
|
title: str
|
|
url: str
|
|
comment: str = ''
|
|
|
|
|
|
@router.post('/links', status_code=status.HTTP_201_CREATED)
|
|
def create_link_endpoint(payload: LinkCreate, authorization: str | None = Header(default=None)):
|
|
if not authorization or not authorization.startswith('Bearer '):
|
|
raise HTTPException(status_code=401, detail='Missing or invalid Authorization header')
|
|
token = authorization.replace('Bearer ', '', 1)
|
|
info = validate_token(token)
|
|
if info is None:
|
|
raise HTTPException(status_code=401, detail='Token expired or invalid')
|
|
|
|
record = create_link(info['user_id'], payload.title, payload.url, payload.comment, payload.timestamp)
|
|
plugin_manager.dispatch({'type': 'link_created', **record})
|
|
return record
|
|
|
|
|
|
@router.put('/links/{link_id}')
|
|
def update_link_endpoint(
|
|
link_id: str,
|
|
payload: LinkUpdate,
|
|
authorization: str | None = Header(default=None),
|
|
):
|
|
if not authorization or not authorization.startswith('Bearer '):
|
|
raise HTTPException(status_code=401, detail='Missing or invalid Authorization header')
|
|
info = validate_token(authorization.replace('Bearer ', '', 1))
|
|
if info is None:
|
|
raise HTTPException(status_code=401, detail='Token expired or invalid')
|
|
|
|
record = update_link(link_id, info['user_id'], payload.title, payload.url, payload.comment)
|
|
if record is None:
|
|
raise HTTPException(status_code=404, detail='Link not found or not owned by user')
|
|
return record
|
|
|
|
|
|
@router.get('/links')
|
|
def list_links():
|
|
return list_public_links()
|