-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
107 lines (92 loc) · 3.13 KB
/
script.py
File metadata and controls
107 lines (92 loc) · 3.13 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
from fastapi import FastAPI
from pydantic import BaseModel
from fastapi.responses import HTMLResponse, FileResponse
from json import dump
import os
from json import load
class Item(BaseModel):
name: str
description: str
app = FastAPI()
next_id = 0
items = {}
if os.path.exists("notes.json") and os.path.getsize("notes.json") > 0:
with open("notes.json", "r") as f:
data = load(f)
items = {int(k): Item(**v) for k, v in data.items()}
if items:
next_id = max(items.keys()) + 1
# the items page actually stores stuff everything else is ui
@app.get("/", response_class=HTMLResponse)
def read_root():
with open("index.html", "r") as f:
return f.read()
@app.get("/favicon.ico", include_in_schema=False)
def icon():
return 'icon.png'
@app.get("/home", response_class=HTMLResponse)
def read_home():
try:
with open("home.html", "r") as f:
return f.read()
except FileNotFoundError:
return HTMLResponse(content="home.html not found", status_code=404)
@app.get("/home/{page}", response_class=HTMLResponse)
def read_sum(page: str):
try:
with open("home/"+page+".html") as f:
return f.read()
except FileNotFoundError:
return HTMLResponse(content="not found", status_code=404)
@app.get("/notes.json", response_class=FileResponse)
def ret_file():
return FileResponse("notes.json")
@app.get("/items")
def read_items():
return items
# check if item exists before creating
@app.post("/items")
def create_item(item: Item):
global next_id
# Check if item with the same name exists
for existing_item in items.values():
if existing_item.name == item.name:
return {"error": "Item with this name already exists"}, 400
items[next_id] = item
next_id += 1
with open("notes.json", "w") as f:
dump({k: v.dict() for k, v in items.items()}, f)
return {"message": "Item created successfully", "item_id": next_id - 1}
# check if item exists before deleting DONE
@app.delete("/items/{item_name}")
def delete_item(item_name: str):
item_id=-1
for key,val in items.items():
if val.name == item_name:
item_id=key
break
if item_id==-1:
return {"error": "File not found"}, 404
del items[item_id]
with open("notes.json", "w") as f:
dump({k: v.dict() for k, v in items.items()}, f)
return {"message": "Item deleted successfully"}
# implement update also
@app.put("/items/{item_name}")
# you have to pass old_name(in url); in json: new name(now leave as none or old_name), new description
def update_item(item_name: str, item: Item):
item_id = -1
for key,val in items.items():
if(val.name == item_name):
item_id=key
break
if(item_id==-1):
return {"error": "Item not found"}, 404
if len(item.description)==0:
items[item_id].name = item.name
else:
items[item_id] = item
with open("notes.json", "w") as f:
dump({k: v.dict() for k, v in items.items()}, f)
return {"message": "Item updated successfully"}
# check if item exists before updating