Skip to content

Commit

Permalink
Adding api request using z schema Closes #61
Browse files Browse the repository at this point in the history
  • Loading branch information
dejanvasic85 committed Oct 28, 2023
1 parent e563801 commit e57acc7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/lib/services/noteService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export async function updateNote(note: Note): Promise<Note> {
where: { id: note.id },
data: {
...note,
boardId: note.boardId!
boardId: note.boardId!,
updatedAt: new Date()
}
});
}
Expand Down
8 changes: 8 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ export const NoteSchema = EntitySchema.extend({

export type Note = z.infer<typeof NoteSchema>;

export const NotePatchInputSchema = z.object({
text: z.string(),
textPlain: z.string(),
colour: z.string().nullable()
});

export type NotePatchInput = z.infer<typeof NotePatchInputSchema>;

export const BoardSchema = EntitySchema.extend({
userId: z.string(),
notes: z.array(NoteSchema),
Expand Down
9 changes: 8 additions & 1 deletion src/routes/api/notes/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ import { json, type RequestHandler } from '@sveltejs/kit';
import { getUserById, isBoardOwner } from '$lib/services/userService';
import { createNote } from '$lib/services/noteService';
import { updateBoard } from '$lib/services/boardService';
import { NoteCreateInputSchema } from '$lib/types';

export const POST: RequestHandler = async ({ locals, request }) => {
const { id, boardId, text, textPlain, colour = null } = await request.json();
const changes = await request.json();
const parseResult = NoteCreateInputSchema.safeParse(changes);
if (!parseResult.success) {
return json({ message: 'Unable to parse NoteCreateInputSchema' }, { status: 400 });
}

const { id, boardId, text, textPlain, colour } = parseResult.data;
const userId = locals.user.id!;
const user = await getUserById(userId, { boards: true, notes: false });
if (!user) {
Expand Down
11 changes: 8 additions & 3 deletions src/routes/api/notes/[id]/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { json, type RequestHandler } from '@sveltejs/kit';

import { updateBoard } from '$lib/services/boardService';
import { getNoteById, updateNote, deleteNote } from '$lib/services/noteService';
import { NotePatchInputSchema } from '$lib/types';
import { getUserById, isBoardOwner } from '$lib/services/userService';

export const GET: RequestHandler = async ({ locals, params }) => {
Expand Down Expand Up @@ -37,8 +38,13 @@ export const PATCH: RequestHandler = async ({ locals, params, request }) => {
return json(null, { status: 404 });
}

// todo: validate body (using joi)
const changes = await request.json();
const parseResult = NotePatchInputSchema.safeParse(changes);
if (!parseResult.success) {
parseResult.error.errors.forEach((e) => console.error(e));
return json({ message: 'Unable to parse NoteSchema' }, { status: 400 });
}

const user = await getUserById(userId, { boards: true, notes: false });
if (!user) {
return json(null, { status: 404 });
Expand All @@ -50,8 +56,7 @@ export const PATCH: RequestHandler = async ({ locals, params, request }) => {

const updatedNote = await updateNote({
...note,
...changes,
updatedAt: new Date()
...parseResult.data
});

return json(updatedNote);
Expand Down
19 changes: 15 additions & 4 deletions src/routes/my-board/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
import { MaybeType, tryFetch } from '$lib/fetch';
import { generateId } from '$lib/identityGenerator';
import { getOrderedNotes, reorderNotes, updateNote } from '$lib/notes';
import type { BoardPatch, Note, NoteOrdered, User } from '$lib/types';
import type {
BoardPatch,
NoteCreateInput,
NotePatchInput,
Note,
NoteOrdered,
User
} from '$lib/types';
const auth = withAuth();
const { getToken } = auth;
Expand Down Expand Up @@ -42,7 +49,7 @@
async function handleCreate() {
const id = generateId('nid');
const newNote: Note = { id, text: '', textPlain: '', boardId, colour: null };
const newNote: NoteCreateInput = { id, text: '', textPlain: '', boardId, colour: null };
localNotes = [...localNotes, { ...newNote, order: localNotes.length }];
localNoteOrder = [...localNoteOrder, id];
Expand Down Expand Up @@ -74,11 +81,15 @@
}
localNotes = [...updateNote(localNotes, note)];
const { order, boardId, ...restNoteProps } = note;
const notePatch: NotePatchInput = {
colour: note.colour,
text: note.text,
textPlain: note.textPlain
};
const { type } = await tryFetch<Note>(
`/api/notes/${note.id}`,
{ method: 'PATCH', body: JSON.stringify(restNoteProps) },
{ method: 'PATCH', body: JSON.stringify(notePatch) },
{ getBearerToken: getToken }
);
Expand Down

0 comments on commit e57acc7

Please sign in to comment.