new folder structure
This commit is contained in:
parent
aa65cead2e
commit
07e014cd8f
4 changed files with 2 additions and 2 deletions
10
frontend/svelte/src/models/repos/note/NoteRepository.ts
Normal file
10
frontend/svelte/src/models/repos/note/NoteRepository.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import type {Note} from "../../types";
|
||||
|
||||
export interface NoteRepository {
|
||||
getNotes(): Promise<Note[]>;
|
||||
getNote(id: number): Promise<Note>;
|
||||
getCurrentNote(): Promise<Note | void>;
|
||||
updateNote(id: number, note: Note): Promise<Note>;
|
||||
deleteNote(id: number): void;
|
||||
createNote(note: Note): Promise<Note>;
|
||||
}
|
||||
|
|
@ -0,0 +1,73 @@
|
|||
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<Note[]>{
|
||||
const response = await StrapiNoteRepository.fetchStrapi("/", 'GET');
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
public async getNote(id: number): Promise<Note>{
|
||||
const response = await StrapiNoteRepository.fetchStrapi("/" + id, 'GET');
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
public async getCurrentNote(): Promise<Note | void> {
|
||||
if (this.currentNoteId === null || this.currentNoteId === undefined) {
|
||||
return;
|
||||
}
|
||||
return await this.getNote(this.currentNoteId);
|
||||
}
|
||||
|
||||
public async updateNote(id: number, note: Note): Promise<Note> {
|
||||
const response = await StrapiNoteRepository.fetchStrapi("/" + id, 'PUT', note);
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
public async createNote(note: Note): Promise<Note> {
|
||||
const response = await StrapiNoteRepository.fetchStrapi("/", 'POST', note);
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
public async deleteNote(id: number): Promise<void> {
|
||||
await StrapiNoteRepository.fetchStrapi("/" + id, 'DELETE');
|
||||
}
|
||||
|
||||
private static async fetchStrapi(path: string, method: HttpMethod, body: Note | null = null): Promise<Response> {
|
||||
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 TOKEN"
|
||||
}
|
||||
|
||||
private static getAuthorizationHeader() {
|
||||
const jwt = parseCookies().jwt;
|
||||
return `bearer ${jwt}`
|
||||
}
|
||||
}
|
||||
0
frontend/svelte/src/models/repos/user/StrapiUserRepo.ts
Normal file
0
frontend/svelte/src/models/repos/user/StrapiUserRepo.ts
Normal file
0
frontend/svelte/src/models/repos/user/UserRepository.ts
Normal file
0
frontend/svelte/src/models/repos/user/UserRepository.ts
Normal file
Loading…
Add table
Add a link
Reference in a new issue