add/delete notes

This commit is contained in:
Michael Dausmann
2023-04-14 00:24:22 +10:00
parent 6a7e7ec9ac
commit 8b2348f6a3
5 changed files with 55 additions and 6 deletions

View File

@@ -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] Read only Notes Dashboard
- [x] SSR Rendered (SEO Optimised) [Note](/pages/notes/[note_id].vue) Display - [x] SSR Rendered (SEO Optimised) [Note](/pages/notes/[note_id].vue) Display
- [x] Max Notes limit property on Plan - [x] Max Notes limit property on Plan
- [ ] Max Notes enforced - [x] Max Notes enforced
- [ ] Optional public SSR Rendered public notes index page - [x] Add, Delete notes on Dashboard
- [ ] Add, Delete, edit notes on Dashboard
### Mobile App ### Mobile App
- [ ] Flutter App Demo integrating with API endpoints, Auth etc - [ ] Flutter App Demo integrating with API endpoints, Auth etc

View File

@@ -14,6 +14,15 @@ export default class NotesService {
} }
async createNote( account_id: number, note_text: string ) { 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 }}); return prisma_client.note.create({ data: { account_id, note_text }});
} }

View File

@@ -7,6 +7,12 @@
const notesStore = useNotesStore(); const notesStore = useNotesStore();
const { notes } = storeToRefs(notesStore); // ensure the notes list is reactive 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 () => { onMounted(async () => {
await notesStore.fetchNotesForCurrentUser(); await notesStore.fetchNotesForCurrentUser();
@@ -15,6 +21,7 @@
<template> <template>
<div> <div>
<h3>Notes Dashboard</h3> <h3>Notes Dashboard</h3>
<p v-for="note in notes">{{ note.note_text }}</p> <p v-for="note in notes">{{ note.note_text }} <button @click.prevent="notesStore.deleteNote(note.id)">-</button></p>
<p><input v-model="newNoteText"/><button @click.prevent="addNote()">Add</button></p>
</div> </div>
</template> </template>

View File

@@ -11,7 +11,7 @@ export const notesRouter = router({
notes, notes,
} }
}), }),
getById: publicProcedure getById: publicProcedure
.input(z.object({ note_id: z.number() })) .input(z.object({ note_id: z.number() }))
.query(async ({ ctx, input }) => { .query(async ({ ctx, input }) => {
const notesService = new NotesService(); const notesService = new NotesService();
@@ -20,4 +20,22 @@ export const notesRouter = router({
note, 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,
}
}),
}) })

View File

@@ -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) // if the active account changes, fetch notes again (i.e dynamic.. probabl overkill)
watch(activeAccountId, async (val, oldVal)=> { watch(activeAccountId, async (val, oldVal)=> {
await fetchNotesForCurrentUser() await fetchNotesForCurrentUser()
}); });
return { notes: _notes, fetchNotesForCurrentUser } return { notes: _notes, fetchNotesForCurrentUser, createNote, deleteNote }
}); });