-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
154 lines (120 loc) · 3.82 KB
/
Copy pathapp.py
File metadata and controls
154 lines (120 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""
FastAPI Starter Template with sqlite-worker
"""
from fastapi import FastAPI, HTTPException
from database import get_worker, init_database
from models import Item, ItemCreate, ItemUpdate
# Initialize FastAPI app
app = FastAPI(
title="SQLite-Worker API",
description="FastAPI starter template with sqlite-worker",
version="1.0.0"
)
# Initialize database
init_database()
worker = get_worker()
@app.on_event("shutdown")
def shutdown():
"""Cleanup on shutdown"""
worker.close()
@app.get("/")
def read_root():
"""Welcome endpoint"""
return {
"message": "Welcome to SQLite-Worker FastAPI Starter",
"docs": "/docs",
"health": "/health"
}
@app.get("/health")
def health_check():
"""Health check endpoint"""
return {"status": "healthy", "database": "connected"}
@app.get("/items")
def list_items(limit: int = 100):
"""List all items"""
token = worker.select("items", limit=limit, order_by="created_at DESC")
items_data = worker.fetch_results(token)
return [
{
"id": item[0],
"name": item[1],
"description": item[2],
"value": item[3],
"created_at": item[4]
}
for item in items_data
]
@app.post("/items", status_code=201)
def create_item(item: ItemCreate):
"""Create a new item"""
token = worker.insert("items", {
"name": item.name,
"description": item.description,
"value": item.value
})
worker.fetch_results(token)
# Get the created item
token = worker.execute(
"SELECT * FROM items WHERE rowid = last_insert_rowid()"
)
result = worker.fetch_results(token)
if result:
item_data = result[0]
return {
"id": item_data[0],
"name": item_data[1],
"description": item_data[2],
"value": item_data[3],
"created_at": item_data[4]
}
raise HTTPException(status_code=500, detail="Failed to create item")
@app.get("/items/{item_id}")
def get_item(item_id: int):
"""Get a specific item"""
token = worker.select("items", conditions={"id": item_id})
items = worker.fetch_results(token)
if not items:
raise HTTPException(status_code=404, detail="Item not found")
item = items[0]
return {
"id": item[0],
"name": item[1],
"description": item[2],
"value": item[3],
"created_at": item[4]
}
@app.put("/items/{item_id}")
def update_item(item_id: int, item_update: ItemUpdate):
"""Update an item"""
# Check if item exists
token = worker.select("items", conditions={"id": item_id})
items = worker.fetch_results(token)
if not items:
raise HTTPException(status_code=404, detail="Item not found")
# Build update data
update_data = {}
if item_update.name is not None:
update_data["name"] = item_update.name
if item_update.description is not None:
update_data["description"] = item_update.description
if item_update.value is not None:
update_data["value"] = item_update.value
if update_data:
token = worker.update("items", update_data, {"id": item_id})
worker.fetch_results(token)
# Return updated item
return get_item(item_id)
@app.delete("/items/{item_id}")
def delete_item(item_id: int):
"""Delete an item"""
# Check if item exists
token = worker.select("items", conditions={"id": item_id})
items = worker.fetch_results(token)
if not items:
raise HTTPException(status_code=404, detail="Item not found")
token = worker.delete("items", {"id": item_id})
worker.fetch_results(token)
return {"message": "Item deleted successfully"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)