From b1b081fe307a306d1328da26c5c507df195f7290 Mon Sep 17 00:00:00 2001 From: j-weissen Date: Tue, 4 Oct 2022 10:14:28 +0200 Subject: [PATCH 1/2] Added StrapiNoteRepository, .env not working, authHeader is mocked --- frontend/svelte/src/models/NoteRepository.ts | 10 +++ .../svelte/src/models/StrapiNoteRepository.ts | 74 +++++++++++++++++++ .../svelte/src/routes/editor/+page.svelte | 27 ++++--- 3 files changed, 101 insertions(+), 10 deletions(-) create mode 100644 frontend/svelte/src/models/NoteRepository.ts create mode 100644 frontend/svelte/src/models/StrapiNoteRepository.ts diff --git a/frontend/svelte/src/models/NoteRepository.ts b/frontend/svelte/src/models/NoteRepository.ts new file mode 100644 index 0000000..421ffce --- /dev/null +++ b/frontend/svelte/src/models/NoteRepository.ts @@ -0,0 +1,10 @@ +import type {Note} from "../types"; + +export interface NoteRepository { + getNotes(): Promise; + getNote(id: number): Promise; + getCurrentNote(): Promise; + updateNote(id: number, note: Note): Promise; + deleteNote(id: number): void; + createNote(note: Note): Promise; +} \ No newline at end of file diff --git a/frontend/svelte/src/models/StrapiNoteRepository.ts b/frontend/svelte/src/models/StrapiNoteRepository.ts new file mode 100644 index 0000000..65c02ce --- /dev/null +++ b/frontend/svelte/src/models/StrapiNoteRepository.ts @@ -0,0 +1,74 @@ +import type {Note} from "../types"; +import {parseCookies} from "nookies"; +import type {NoteRepository} from "./NoteRepository"; + +type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' + +export class StrapiNoteRepository implements NoteRepository { + private static instance: StrapiNoteRepository; + public static getInstance(): StrapiNoteRepository { + if (this.instance === undefined || this.instance === null) { + this.instance = new StrapiNoteRepository(); + } + return this.instance; + } + + private constructor() { + } + + private currentNoteId: number | undefined; + private static apiNoteEndpoint: string = "http://localhost:1337/api/notes" + + public async getNotes(): Promise{ + const response = await StrapiNoteRepository.fetchStrapi("/", 'GET'); + return await response.json(); + } + + public async getNote(id: number): Promise{ + const response = await StrapiNoteRepository.fetchStrapi("/" + id, 'GET'); + return await response.json(); + } + + public async getCurrentNote(): Promise { + if (this.currentNoteId === null || this.currentNoteId === undefined) { + return; + } + return await this.getNote(this.currentNoteId); + } + + public async updateNote(id: number, note: Note): Promise { + const response = await StrapiNoteRepository.fetchStrapi("/" + id, 'PUT', note); + return await response.json(); + } + + public async createNote(note: Note): Promise { + const response = await StrapiNoteRepository.fetchStrapi("/", 'POST', note); + return await response.json(); + } + + public async deleteNote(id: number): Promise { + await StrapiNoteRepository.fetchStrapi("/" + id, 'DELETE'); + } + + private static async fetchStrapi(path: string, method: HttpMethod, body: Note | null = null): Promise { + let requestInit: RequestInit = { + method: method, + headers: { + authorization: StrapiNoteRepository.mockedGetAuthorizationHeader() + } + }; + if (body) { + requestInit["body"] = JSON.stringify({data: body}); + } + return await fetch(StrapiNoteRepository.apiNoteEndpoint + path, requestInit); + } + + private static mockedGetAuthorizationHeader() { + return "bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNjY0MzU2NzIxLCJleHAiOjE2NjY5NDg3MjF9.SljSS-c4X0Z8SPArwaRg8FiOz15YuAH0Ora4y1you9o" + } + + private static getAuthorizationHeader() { + const jwt = parseCookies().jwt; + return `bearer ${jwt}` + } +} \ No newline at end of file diff --git a/frontend/svelte/src/routes/editor/+page.svelte b/frontend/svelte/src/routes/editor/+page.svelte index eeab65c..3df1f70 100644 --- a/frontend/svelte/src/routes/editor/+page.svelte +++ b/frontend/svelte/src/routes/editor/+page.svelte @@ -1,24 +1,31 @@ - {"Pomelonote | Edit " + currNote.title} + {"Pomelonote | Edit "} -
- {currNote.content} +
+
+ +
- + \ No newline at end of file From fe005863a86b339ba2b7a2de69b3809e5746c244 Mon Sep 17 00:00:00 2001 From: j-weissen Date: Tue, 4 Oct 2022 10:25:32 +0200 Subject: [PATCH 2/2] cleanup --- frontend/svelte/src/models/StrapiNoteRepository.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/frontend/svelte/src/models/StrapiNoteRepository.ts b/frontend/svelte/src/models/StrapiNoteRepository.ts index 65c02ce..86fb1a7 100644 --- a/frontend/svelte/src/models/StrapiNoteRepository.ts +++ b/frontend/svelte/src/models/StrapiNoteRepository.ts @@ -13,8 +13,7 @@ export class StrapiNoteRepository implements NoteRepository { return this.instance; } - private constructor() { - } + private constructor() {} private currentNoteId: number | undefined; private static apiNoteEndpoint: string = "http://localhost:1337/api/notes" @@ -64,7 +63,7 @@ export class StrapiNoteRepository implements NoteRepository { } private static mockedGetAuthorizationHeader() { - return "bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNjY0MzU2NzIxLCJleHAiOjE2NjY5NDg3MjF9.SljSS-c4X0Z8SPArwaRg8FiOz15YuAH0Ora4y1you9o" + return "bearer TOKEN" } private static getAuthorizationHeader() {