from fastapi import FastAPI, HTTPException from pydantic import BaseModel from datetime import datetime from typing import Optional import sqlite3 import os app = FastAPI(title="LandveX Bounties") DB_PATH = os.path.join(os.path.dirname(__file__), "..", "landvex.db") def get_db(): conn = sqlite3.connect(DB_PATH) conn.row_factory = sqlite3.Row return conn def init_db(): conn = get_db() conn.execute( """ CREATE TABLE IF NOT EXISTS bounties ( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, description TEXT, reward REAL, status TEXT DEFAULT 'open', created_at TEXT DEFAULT CURRENT_TIMESTAMP, submitted_at TEXT ) """ ) conn.commit() conn.close() class BountyCreate(BaseModel): title: str description: str = "" reward: float = 0.0 class BountyOut(BaseModel): id: int title: str description: str reward: float status: str created_at: str submitted_at: Optional[str] class Config: from_attributes = True @app.on_event("startup") def startup(): init_db() @app.post("/v0/bounties", response_model=BountyOut) def create_bounty(bounty: BountyCreate): conn = get_db() cursor = conn.execute( "INSERT INTO bounties (title, description, reward) VALUES (?, ?, ?)", (bounty.title, bounty.description, bounty.reward), ) bounty_id = cursor.lastrowid conn.commit() row = conn.execute("SELECT * FROM bounties WHERE id = ?", (bounty_id,)).fetchone() conn.close() return dict(row) @app.get("/v0/bounties/{bounty_id}", response_model=BountyOut) def get_bounty(bounty_id: int): conn = get_db() row = conn.execute("SELECT * FROM bounties WHERE id = ?", (bounty_id,)).fetchone() conn.close() if not row: raise HTTPException(status_code=404, detail="Bounty not found") return dict(row) @app.post("/v0/bounties/{bounty_id}/submit", response_model=BountyOut) def submit_bounty(bounty_id: int): conn = get_db() row = conn.execute("SELECT * FROM bounties WHERE id = ?", (bounty_id,)).fetchone() if not row: conn.close() raise HTTPException(status_code=404, detail="Bounty not found") conn.execute( "UPDATE bounties SET status = 'submitted', submitted_at = ? WHERE id = ?", (datetime.utcnow().isoformat(), bounty_id), ) conn.commit() row = conn.execute("SELECT * FROM bounties WHERE id = ?", (bounty_id,)).fetchone() conn.close() return dict(row)