Initial working version
This commit is contained in:
0
api/init.py
Normal file
0
api/init.py
Normal file
20
api/models.py
Normal file
20
api/models.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
class ColorUpdate(BaseModel):
|
||||
barId: str
|
||||
color: str
|
||||
|
||||
class PositionUpdate(BaseModel):
|
||||
barId: str
|
||||
x: int
|
||||
y: int
|
||||
|
||||
class CreateBar(BaseModel):
|
||||
barId: str
|
||||
url: str
|
||||
color: str
|
||||
x: int
|
||||
y: int
|
||||
|
||||
class DeleteBar(BaseModel):
|
||||
barId: str
|
||||
57
api/routes.py
Normal file
57
api/routes.py
Normal file
@@ -0,0 +1,57 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from .models import ColorUpdate, PositionUpdate, CreateBar, DeleteBar
|
||||
from .settings_manager import SettingsManager
|
||||
|
||||
router = APIRouter(prefix="/api")
|
||||
settings_manager = SettingsManager()
|
||||
|
||||
@router.get("/settings")
|
||||
async def get_settings():
|
||||
try:
|
||||
return settings_manager.load_settings()
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Error reading settings: {str(e)}")
|
||||
|
||||
@router.post("/settings/color")
|
||||
async def update_color(color_update: ColorUpdate):
|
||||
try:
|
||||
settings_manager.update_color(color_update.barId, color_update.color)
|
||||
return {"success": True, "message": f"Color updated for {color_update.barId}"}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Error updating settings: {str(e)}")
|
||||
|
||||
@router.post("/settings/position")
|
||||
async def update_position(position_update: PositionUpdate):
|
||||
try:
|
||||
settings_manager.update_position(position_update.barId, position_update.x, position_update.y)
|
||||
return {"success": True, "message": f"Position updated for {position_update.barId}"}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Error updating settings: {str(e)}")
|
||||
|
||||
@router.post("/settings/create")
|
||||
async def create_bar(create_bar: CreateBar):
|
||||
try:
|
||||
if settings_manager.bar_exists(create_bar.barId):
|
||||
raise HTTPException(status_code=400, detail=f"Bar {create_bar.barId} already exists")
|
||||
|
||||
settings_manager.create_bar(
|
||||
create_bar.barId,
|
||||
create_bar.url,
|
||||
create_bar.color,
|
||||
create_bar.x,
|
||||
create_bar.y
|
||||
)
|
||||
return {"success": True, "message": f"Bar {create_bar.barId} created"}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Error creating bar: {str(e)}")
|
||||
|
||||
@router.delete("/settings/delete")
|
||||
async def delete_bar(delete_bar: DeleteBar):
|
||||
try:
|
||||
if not settings_manager.bar_exists(delete_bar.barId):
|
||||
raise HTTPException(status_code=404, detail=f"Bar {delete_bar.barId} not found")
|
||||
|
||||
settings_manager.delete_bar(delete_bar.barId)
|
||||
return {"success": True, "message": f"Bar {delete_bar.barId} deleted"}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Error deleting bar: {str(e)}")
|
||||
51
api/settings_manager.py
Normal file
51
api/settings_manager.py
Normal file
@@ -0,0 +1,51 @@
|
||||
import json
|
||||
import os
|
||||
from typing import Dict, Any
|
||||
|
||||
class SettingsManager:
|
||||
def __init__(self, settings_file: str = "settings.json"):
|
||||
self.settings_file = settings_file
|
||||
|
||||
def load_settings(self) -> Dict[str, Any]:
|
||||
try:
|
||||
with open(self.settings_file, "r") as f:
|
||||
return json.load(f)
|
||||
except FileNotFoundError:
|
||||
return {}
|
||||
|
||||
def save_settings(self, settings: Dict[str, Any]) -> None:
|
||||
with open(self.settings_file, "w") as f:
|
||||
json.dump(settings, f, indent=2)
|
||||
|
||||
def update_color(self, bar_id: str, color: str) -> None:
|
||||
settings = self.load_settings()
|
||||
if bar_id in settings:
|
||||
settings[bar_id]["color"] = color
|
||||
self.save_settings(settings)
|
||||
|
||||
def update_position(self, bar_id: str, x: int, y: int) -> None:
|
||||
settings = self.load_settings()
|
||||
if bar_id in settings:
|
||||
settings[bar_id]["x"] = x
|
||||
settings[bar_id]["y"] = y
|
||||
self.save_settings(settings)
|
||||
|
||||
def create_bar(self, bar_id: str, url: str, color: str, x: int, y: int) -> None:
|
||||
settings = self.load_settings()
|
||||
settings[bar_id] = {
|
||||
"url": url,
|
||||
"color": color,
|
||||
"x": x,
|
||||
"y": y
|
||||
}
|
||||
self.save_settings(settings)
|
||||
|
||||
def delete_bar(self, bar_id: str) -> None:
|
||||
settings = self.load_settings()
|
||||
if bar_id in settings:
|
||||
del settings[bar_id]
|
||||
self.save_settings(settings)
|
||||
|
||||
def bar_exists(self, bar_id: str) -> bool:
|
||||
settings = self.load_settings()
|
||||
return bar_id in settings
|
||||
Reference in New Issue
Block a user