Edit of links and database versioning
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
from fastapi import APIRouter, Header, HTTPException, status
|
||||
from pydantic import BaseModel
|
||||
|
||||
from backend.app.services.link_service import create_link, list_public_links
|
||||
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
|
||||
|
||||
@@ -15,6 +15,12 @@ class LinkCreate(BaseModel):
|
||||
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 '):
|
||||
@@ -29,6 +35,24 @@ def create_link_endpoint(payload: LinkCreate, authorization: str | None = Header
|
||||
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()
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
from fastapi import APIRouter
|
||||
from fastapi import APIRouter, Depends
|
||||
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
||||
|
||||
from backend.app.services.link_service import list_public_links, list_public_users
|
||||
from backend.app.services.token_service import validate_token
|
||||
|
||||
router = APIRouter()
|
||||
optional_bearer = HTTPBearer(auto_error=False)
|
||||
|
||||
|
||||
@router.get('/users')
|
||||
@@ -12,7 +15,15 @@ def public_users():
|
||||
|
||||
@router.get('/feed')
|
||||
@router.get('/feed/{username}')
|
||||
def public_feed(username: str | None = None):
|
||||
def public_feed(
|
||||
username: str | None = None,
|
||||
credentials: HTTPAuthorizationCredentials | None = Depends(optional_bearer),
|
||||
):
|
||||
current_user_id = None
|
||||
if credentials:
|
||||
token_data = validate_token(credentials.credentials)
|
||||
if token_data:
|
||||
current_user_id = token_data['user_id']
|
||||
items = list_public_links(username)
|
||||
return [
|
||||
{
|
||||
@@ -25,6 +36,8 @@ def public_feed(username: str | None = None):
|
||||
'avatar_url': item['avatar_url'],
|
||||
'bio': item['bio'],
|
||||
},
|
||||
'is_owner': item['user_id'] == current_user_id,
|
||||
'can_edit': item['user_id'] == current_user_id,
|
||||
'created_at': item['created_at'],
|
||||
}
|
||||
for item in items
|
||||
|
||||
Reference in New Issue
Block a user