From 8b2348f6a39be89a25cada5e97753f5284e2d1c3 Mon Sep 17 00:00:00 2001 From: Michael Dausmann Date: Fri, 14 Apr 2023 00:24:22 +1000 Subject: [PATCH] add/delete notes --- README.md | 5 ++--- lib/services/notes.service.ts | 9 +++++++++ pages/dashboard.vue | 9 ++++++++- server/trpc/routers/notes.router.ts | 20 +++++++++++++++++++- stores/notes.store.ts | 18 +++++++++++++++++- 5 files changed, 55 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 7798933..cc45fd3 100644 --- a/README.md +++ b/README.md @@ -69,9 +69,8 @@ Please don't hitch your wagon to this star just yet... I'm coding this in the op - [x] Read only Notes Dashboard - [x] SSR Rendered (SEO Optimised) [Note](/pages/notes/[note_id].vue) Display - [x] Max Notes limit property on Plan -- [ ] Max Notes enforced -- [ ] Optional public SSR Rendered public notes index page -- [ ] Add, Delete, edit notes on Dashboard +- [x] Max Notes enforced +- [x] Add, Delete notes on Dashboard ### Mobile App - [ ] Flutter App Demo integrating with API endpoints, Auth etc diff --git a/lib/services/notes.service.ts b/lib/services/notes.service.ts index d635dd7..e63ff90 100644 --- a/lib/services/notes.service.ts +++ b/lib/services/notes.service.ts @@ -14,6 +14,15 @@ export default class NotesService { } async createNote( account_id: number, note_text: string ) { + const account = await prisma_client.account.findFirstOrThrow({ + where: { id: account_id}, + include: { notes: true} + }); + + if(account.notes.length>= account.max_notes){ + throw new Error('Note Limit reached, no new notes can be added'); + } + return prisma_client.note.create({ data: { account_id, note_text }}); } diff --git a/pages/dashboard.vue b/pages/dashboard.vue index 0a1af58..757f67e 100644 --- a/pages/dashboard.vue +++ b/pages/dashboard.vue @@ -7,6 +7,12 @@ const notesStore = useNotesStore(); const { notes } = storeToRefs(notesStore); // ensure the notes list is reactive + const newNoteText = ref('') + + async function addNote(){ + await notesStore.createNote(newNoteText.value) + newNoteText.value = ''; + } onMounted(async () => { await notesStore.fetchNotesForCurrentUser(); @@ -15,6 +21,7 @@ diff --git a/server/trpc/routers/notes.router.ts b/server/trpc/routers/notes.router.ts index 0f0f2c8..391dfb5 100644 --- a/server/trpc/routers/notes.router.ts +++ b/server/trpc/routers/notes.router.ts @@ -11,7 +11,7 @@ export const notesRouter = router({ notes, } }), - getById: publicProcedure + getById: publicProcedure .input(z.object({ note_id: z.number() })) .query(async ({ ctx, input }) => { const notesService = new NotesService(); @@ -20,4 +20,22 @@ export const notesRouter = router({ note, } }), + createNote: protectedProcedure + .input(z.object({ note_text: z.string() })) + .mutation(async ({ ctx, input }) => { + const notesService = new NotesService(); + const note = (ctx.activeAccountId)?await notesService.createNote(ctx.activeAccountId, input.note_text):null; + return { + note, + } + }), + deleteNote: protectedProcedure + .input(z.object({ note_id: z.number() })) + .mutation(async ({ ctx, input }) => { + const notesService = new NotesService(); + const note = (ctx.activeAccountId)?await notesService.deleteNote(input.note_id):null; + return { + note, + } + }), }) \ No newline at end of file diff --git a/stores/notes.store.ts b/stores/notes.store.ts index d0b764a..ae0b4f2 100644 --- a/stores/notes.store.ts +++ b/stores/notes.store.ts @@ -26,10 +26,26 @@ export const useNotesStore = defineStore('notes', () => { } } + async function createNote(note_text: string) { + const { $client } = useNuxtApp(); + const { note } = await $client.notes.createNote.mutate({note_text}); + if(note){ + _notes.value.push(note); + } + } + + async function deleteNote(note_id: number) { + const { $client } = useNuxtApp(); + const { note } = await $client.notes.deleteNote.mutate({note_id}); + if(note){ + _notes.value = _notes.value.filter(n => n.id !== note.id); + } + } + // if the active account changes, fetch notes again (i.e dynamic.. probabl overkill) watch(activeAccountId, async (val, oldVal)=> { await fetchNotesForCurrentUser() }); - return { notes: _notes, fetchNotesForCurrentUser } + return { notes: _notes, fetchNotesForCurrentUser, createNote, deleteNote } });