-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from SE-UUlm/feat/125-feature-add-basic-api-c…
…ontroller-structure Added basic api controller structure
- Loading branch information
Showing
20 changed files
with
1,213 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
; Prefix client code env variables with 'PUBLIC_' (https://svelte.dev/tutorial/kit/env-static-public) | ||
PUBLIC_API_BASE_URL=http://localhost:8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { Author, AuthorSpec } from "$lib/model/backend"; | ||
import type { IAuthorController } from "../backend-api"; | ||
import { HttpClient } from "./http-client"; | ||
|
||
export class AuthorController implements IAuthorController { | ||
private readonly client: HttpClient; | ||
|
||
constructor(authorId: number) { | ||
this.client = new HttpClient(`authors/${authorId}`); | ||
} | ||
|
||
async get(): Promise<Author> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async update(newSpec: AuthorSpec): Promise<Author> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
async remove(): Promise<void> { | ||
throw new Error("Method not implemented."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import type { | ||
IAuthorController, | ||
IBackendController, | ||
IPaperController, | ||
IProjectController, | ||
IUserController, | ||
} from "../backend-api"; | ||
import type { | ||
User, | ||
Project, | ||
ProjectSpec, | ||
UserSpec, | ||
Author, | ||
AuthorSpec, | ||
Paper, | ||
PaperSpec, | ||
} from "../model/backend"; | ||
import { AuthorController } from "./author-controller"; | ||
import { HttpClient } from "./http-client"; | ||
import { PaperController } from "./paper-controller"; | ||
import { ProjectController } from "./project-controller"; | ||
import { UserController } from "./user-controller"; | ||
|
||
export class BackendController implements IBackendController { | ||
private static readonly instance: BackendController = new BackendController(); | ||
private readonly client: HttpClient; | ||
|
||
private constructor() { | ||
this.client = new HttpClient(); | ||
} | ||
|
||
static getInstance() { | ||
return BackendController.instance; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async signIn(email: string, password: string): Promise<User> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
async signOut(): Promise<void> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async requestEmailForgottenPassword(email: string): Promise<void> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
async getProjects(): Promise<Project[]> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async createProject(spec: ProjectSpec): Promise<Project> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
project(projectId: number): IProjectController { | ||
return new ProjectController(projectId); | ||
} | ||
|
||
async getUsers(): Promise<User[]> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async createUser(personalInfo: UserSpec, password: string): Promise<User> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
user(userId: number): IUserController { | ||
return new UserController(userId); | ||
} | ||
|
||
thisUser(): IUserController { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
async getAuthors(): Promise<Author[]> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async createAuthor(spec: AuthorSpec): Promise<Author> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
author(authorId: number): IAuthorController { | ||
return new AuthorController(authorId); | ||
} | ||
|
||
async getPapers(): Promise<Paper[]> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async createPaper(spec: PaperSpec): Promise<Paper> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
paper(paperId: number): IPaperController { | ||
return new PaperController(paperId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { Criterion, CriterionSpec } from "$lib/model/backend"; | ||
import type { ICriterionController } from "../backend-api"; | ||
import { HttpClient } from "./http-client"; | ||
|
||
export class CriterionController implements ICriterionController { | ||
private readonly client: HttpClient; | ||
|
||
constructor(basePath: string, criterionId: number) { | ||
this.client = new HttpClient(`${basePath}/criteria/${criterionId}`); | ||
} | ||
|
||
async get(): Promise<Criterion> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
async remove(): Promise<void> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async update(spec: CriterionSpec): Promise<Criterion> { | ||
throw new Error("Method not implemented."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { PUBLIC_API_BASE_URL } from "$env/static/public"; | ||
|
||
/** | ||
* A simple HTTP client that can make requests to the backend API. | ||
* | ||
* This class is used to make requests to the backend API. It is a simple wrapper around the `fetch` API. | ||
* It uses the `PUBLIC_API_BASE_URL` environment variable to determine the base URL of the API. | ||
* | ||
* @example | ||
* const client = new HttpClient("api/v1/example"); | ||
* await client.get(); | ||
* await client.post({ data: "example" }); | ||
* await client.put({ data: "example" }); | ||
* await client.patch({ data: "example" }); | ||
* await client.delete(); | ||
*/ | ||
export class HttpClient { | ||
private readonly basePath: string; | ||
|
||
/** | ||
* Creates a new HTTP client. | ||
* | ||
* @param path The path to the API endpoint. This path will be appended to the `PUBLIC_API_BASE_URL` environment variable. | ||
* | ||
* @example | ||
* const client = new HttpClient("api/v1/example"); | ||
* // When the `PUBLIC_API_BASE_URL` environment variable is `http://localhost:3000` | ||
* // the base URL of the API will be `http://localhost:3000/api/v1/example` | ||
*/ | ||
constructor(path: string = "") { | ||
this.basePath = `${PUBLIC_API_BASE_URL}${path ? `/${path}` : ""}`; | ||
} | ||
|
||
private async fetch<T>( | ||
path: string = "", | ||
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE", | ||
body?: T, | ||
headers?: HeadersInit, | ||
): Promise<Response> { | ||
return fetch(`${this.basePath}/${path}`, { | ||
method, | ||
headers: { | ||
"Content-Type": "application/json", | ||
...headers, | ||
}, | ||
body: body ? JSON.stringify(body) : undefined, | ||
}); | ||
} | ||
|
||
/** | ||
* Makes a GET request to the API. | ||
* | ||
* @param path The path to the API endpoint. This path will be appended to the base URL of the API. | ||
* @param headers The headers to include in the request. | ||
* @returns A promise that resolves to the response from the API. | ||
*/ | ||
async get(path: string = "", headers?: HeadersInit): Promise<Response> { | ||
return this.fetch(path, "GET", undefined, headers); | ||
} | ||
|
||
/** | ||
* Makes a POST request to the API. | ||
* | ||
* @param path The path to the API endpoint. This path will be appended to the base URL of the API. | ||
* @param body The body of the request. | ||
* @param headers The headers to include in the request. | ||
* @returns A promise that resolves to the response from the API. | ||
*/ | ||
async post<T>(path: string = "", body?: T, headers?: HeadersInit): Promise<Response> { | ||
return this.fetch(path, "POST", body, headers); | ||
} | ||
|
||
/** | ||
* Makes a PUT request to the API. | ||
* | ||
* @param path The path to the API endpoint. This path will be appended to the base URL of the API. | ||
* @param body The body of the request. | ||
* @param headers The headers to include in the request. | ||
* @returns A promise that resolves to the response from the API. | ||
*/ | ||
async put<T>(path: string = "", body?: T, headers?: HeadersInit): Promise<Response> { | ||
return this.fetch(path, "PUT", body, headers); | ||
} | ||
|
||
/** | ||
* Makes a PATCH request to the API. | ||
* | ||
* @param path The path to the API endpoint. This path will be appended to the base URL of the API. | ||
* @param body The body of the request. | ||
* @param headers The headers to include in the request. | ||
* @returns A promise that resolves to the response from the API. | ||
*/ | ||
async patch<T>(path: string = "", body?: T, headers?: HeadersInit): Promise<Response> { | ||
return this.fetch(path, "PATCH", body, headers); | ||
} | ||
|
||
/** | ||
* Makes a DELETE request to the API. | ||
* | ||
* @param path The path to the API endpoint. This path will be appended to the base URL of the API. | ||
* @param headers The headers to include in the request. | ||
* @returns A promise that resolves to the response from the API. | ||
*/ | ||
async delete(path: string = "", headers?: HeadersInit): Promise<Response> { | ||
return this.fetch(path, "DELETE", undefined, headers); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { Paper, PaperSpec } from "$lib/model/backend"; | ||
import type { IPaperController } from "../backend-api"; | ||
import { HttpClient } from "./http-client"; | ||
|
||
export class PaperController implements IPaperController { | ||
private readonly client: HttpClient; | ||
|
||
constructor(paperId: number) { | ||
this.client = new HttpClient(`papers/${paperId}`); | ||
} | ||
|
||
async get(): Promise<Paper> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async update(newSpec: PaperSpec): Promise<Paper> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
async getForwardReferencedPapers(): Promise<Paper[]> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
async getBackwardReferencedPapers(): Promise<Paper[]> { | ||
throw new Error("Method not implemented."); | ||
} | ||
} |
Oops, something went wrong.