Checkpoint React frontend migration
This commit is contained in:
84
backend/app/routers/websites.py
Normal file
84
backend/app/routers/websites.py
Normal file
@@ -0,0 +1,84 @@
|
||||
from typing import List, Optional
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.future import select
|
||||
from pydantic import BaseModel
|
||||
|
||||
from app.database import get_db
|
||||
from app.models import Website
|
||||
from app.core.auth import AuthContext, get_auth_context, require_website_auth
|
||||
|
||||
router = APIRouter(tags=["websites"])
|
||||
|
||||
class WebsiteBase(BaseModel):
|
||||
name: str
|
||||
domain: str
|
||||
|
||||
class WebsiteResponse(WebsiteBase):
|
||||
id: int
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
@router.get("/websites", response_model=List[WebsiteResponse])
|
||||
async def get_websites(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
auth: AuthContext = Depends(get_auth_context),
|
||||
):
|
||||
require_website_auth(auth, allowed_roles={"admin", "system_admin"})
|
||||
result = await db.execute(select(Website).order_by(Website.id.asc()))
|
||||
websites = result.scalars().all()
|
||||
# Map old columns (site_name, site_url) to new response format
|
||||
return [
|
||||
WebsiteResponse(
|
||||
id=w.id,
|
||||
name=w.site_name,
|
||||
domain=w.site_url
|
||||
) for w in websites
|
||||
]
|
||||
|
||||
@router.post("/websites", response_model=WebsiteResponse)
|
||||
async def create_website(
|
||||
payload: WebsiteBase,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
auth: AuthContext = Depends(get_auth_context),
|
||||
):
|
||||
require_website_auth(auth, allowed_roles={"admin", "system_admin"})
|
||||
website = Website(site_name=payload.name, site_url=payload.domain)
|
||||
db.add(website)
|
||||
await db.commit()
|
||||
await db.refresh(website)
|
||||
return WebsiteResponse(id=website.id, name=website.site_name, domain=website.site_url)
|
||||
|
||||
@router.put("/websites/{website_id}", response_model=WebsiteResponse)
|
||||
async def update_website(
|
||||
website_id: int,
|
||||
payload: WebsiteBase,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
auth: AuthContext = Depends(get_auth_context),
|
||||
):
|
||||
require_website_auth(auth, allowed_roles={"admin", "system_admin"})
|
||||
website = await db.get(Website, website_id)
|
||||
if not website:
|
||||
raise HTTPException(status_code=404, detail="Website not found")
|
||||
|
||||
website.site_name = payload.name
|
||||
website.site_url = payload.domain
|
||||
await db.commit()
|
||||
await db.refresh(website)
|
||||
return WebsiteResponse(id=website.id, name=website.site_name, domain=website.site_url)
|
||||
|
||||
@router.delete("/websites/{website_id}")
|
||||
async def delete_website(
|
||||
website_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
auth: AuthContext = Depends(get_auth_context),
|
||||
):
|
||||
require_website_auth(auth, allowed_roles={"admin", "system_admin"})
|
||||
website = await db.get(Website, website_id)
|
||||
if not website:
|
||||
raise HTTPException(status_code=404, detail="Website not found")
|
||||
|
||||
await db.delete(website)
|
||||
await db.commit()
|
||||
return {"status": "success", "message": "Website deleted"}
|
||||
Reference in New Issue
Block a user