from fastapi import FastAPI
app = FastAPI()
users = {}
notes = {}
# создать пользователя
@app.post("/register")
def register(username: str, password: str):
if username in users:
return {"msg": "user exists"}
users[username] = password
notes[username] = []
return {"msg": "ok"}
# логин
@app.post("/login")
def login(username: str, password: str):
if username in users:
if users[username] == password:
return {"msg": "login ok"}
return {"msg": "error"}
# получить заметки
@app.get("/notes")
def get_notes(username: str):
if username in notes:
return notes[username]
return {"msg": "no user"}
# добавить заметку
@app.post("/add_note")
def add_note(username: str, text: str):
if username in notes:
notes[username].append(text)
return {"msg": "added"}
return {"msg": "no user"}
# удалить заметку
@app.post("/delete_note")
def delete_note(username: str, index: int):
if username in notes:
if index < len(notes[username]):
notes[username].pop(index)
return {"msg": "deleted"}
return {"msg": "error"}
# изменить заметку
@app.post("/edit_note")
def edit_note(username: str, index: int, text: str):
if username in notes:
if index < len(notes[username]):
notes[username][index] = text
return {"msg": "edited"}
return {"msg": "error"}