diff --git a/backend/app/__init__.py b/backend/app/__init__.py index 15dd0be..aa73664 100644 --- a/backend/app/__init__.py +++ b/backend/app/__init__.py @@ -4,7 +4,6 @@ from starlette.middleware.cors import CORSMiddleware app = FastAPI() -app.include_router(router) app.add_middleware( CORSMiddleware, allow_origins=['*'], @@ -12,3 +11,4 @@ app.add_middleware( allow_methods=['*'], allow_headers=['*'], ) +app.include_router(router) diff --git a/backend/app/routes.py b/backend/app/routes.py index 6af6311..5e03c2b 100644 --- a/backend/app/routes.py +++ b/backend/app/routes.py @@ -16,9 +16,13 @@ async def get_notes(session: AsyncSession = Depends(get_session)): return result.scalars().all() +class NoteCreate(BaseModel): + title: str + + @router.post("/items/create") -async def create_note(title: str, session: AsyncSession = Depends(get_session)): - note = Note(title=title) +async def create_note(req: NoteCreate, session: AsyncSession = Depends(get_session)): + note = Note(title=req.title) session.add(note) await session.commit() return { @@ -36,26 +40,25 @@ class NoteUpdateReq(BaseModel): @router.put("/items/update") async def update_note(req: NoteUpdateReq, session: AsyncSession = Depends(get_session)): - new_note = Note( - id=req.id, - title=req.title, - checked=req.checked - ) await session.execute( - update(Note).where(Note.id == req.id).values(new_note) + update(Note).where(Note.id == req.id).values(req.__dict__) ) await session.commit() return { - "id": new_note.id, - "title": new_note.title, - "checked": new_note.checked + "id": req.id, + "title": req.title, + "checked": req.checked } +class NoteDeleteReq(BaseModel): + id: int + + @router.delete("/items/delete") -async def delete_note(id: int, session: AsyncSession = Depends(get_session)): +async def delete_note(req: NoteDeleteReq, session: AsyncSession = Depends(get_session)): await session.execute( - delete(Note).where(Note.id == id) + delete(Note).where(Note.id == req.id) ) await session.commit() return 204 diff --git a/frontend/src/App.vue b/frontend/src/App.vue index 41dba3f..ee987fc 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -2,32 +2,50 @@ import {onMounted, ref} from 'vue' import Item from "./components/Item.vue"; import Edit from "./components/Edit.vue"; +import config from "./config"; import axios from 'axios' -let id = 0 - -const notes = ref([{id: id, title: "Hello World", checked: false}]) -const currentNote = ref({id: id += 1, title: "", checked: false}) - onMounted(async () => { - notes.value = await axios.get('http://localhost:8888/items') + const response = await axios.get(`${config.apiEndpoint}/items`) + notes.value = response.data + id = Math.max(...notes.value.map((note: any) => note.id), 0) + currentNote.value.id = id += 1 }) -function processNote(note: any) { + +let id; +const notes = ref([]) +const currentNote = ref({id: id += 1, title: "", checked: false}) + +async function processNote(note: any) { const index = notes.value.findIndex((item) => item.id === note.id) if (index !== -1) { - notes.value[index] = note + await axios.put(`${config.apiEndpoint}/items/update`, note).then((response) => { + notes.value[index] = response.data + }) } else { - notes.value.push(note) + await axios.post(`${config.apiEndpoint}/items/create`, {title: note.title}).then((response) => { + notes.value.push(response.data) + }) } currentNote.value = {id: id += 1, title: "", checked: false} } -function deleteNote(note: any) { + +async function deleteNote(note: any) { + await axios.delete(`${config.apiEndpoint}/items/delete`, {data: {id: note.id}}) notes.value = notes.value.filter((item) => item.id !== note.id) } + +async function updateChecked(id: any, checked: boolean) { + const index = notes.value.findIndex((item) => item.id === id) + if (index !== -1) { + notes.value[index].checked = checked + await axios.put(`${config.apiEndpoint}/items/update`, notes.value[index]) + } +}