From cc306f33ef39944ad40acb965a0fae7551b4dcc2 Mon Sep 17 00:00:00 2001 From: Mootfrost777 Date: Tue, 3 Jan 2023 19:43:27 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B2=D1=80=D0=BE=D0=B4=D0=B5=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/__init__.py | 2 +- backend/app/routes.py | 29 ++++++++++++----------- frontend/src/App.vue | 40 +++++++++++++++++++++++--------- frontend/src/components/Edit.vue | 6 ++++- frontend/src/components/Item.vue | 8 +++++-- frontend/src/config.ts | 5 ++++ 6 files changed, 62 insertions(+), 28 deletions(-) create mode 100644 frontend/src/config.ts 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]) + } +}