Service pour importer une configuration dans un json

This commit is contained in:
Guams 2025-01-26 16:11:39 +01:00
parent 1644ac32c9
commit d6202efbbf
6 changed files with 55 additions and 26 deletions

2
.gitignore vendored
View File

@ -1,5 +1,7 @@
# See https://docs.github.com/get-started/getting-started-with-git/ignoring-files for more about ignoring files. # See https://docs.github.com/get-started/getting-started-with-git/ignoring-files for more about ignoring files.
package-lock.json
# Compiled output # Compiled output
/dist /dist
/tmp /tmp

3
configuration.json Normal file
View File

@ -0,0 +1,3 @@
{
"apiURL": "http://localhost:8080/api"
}

View File

@ -0,0 +1,12 @@
import { Injectable } from '@angular/core';
import * as config from '../../configuration.json'
@Injectable({
providedIn: 'root'
})
export class ConfigurationService {
getApiUrl(): string {
return config.apiURL
}
}

View File

@ -3,26 +3,30 @@ import {HttpClient, HttpHeaders} from '@angular/common/http';
import {Observable} from 'rxjs'; import {Observable} from 'rxjs';
import {Author} from '../models/author'; import {Author} from '../models/author';
import {Post} from '../models/post'; import {Post} from '../models/post';
import {ConfigurationService} from '../configuration.service';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class AuthorService { export class AuthorService {
url: string = "http://localhost:8080/api/authors"; apiUrl: string = '';
constructor(private httpClient: HttpClient) {} constructor(private httpClient: HttpClient,
private configurationService: ConfigurationService) {
this.apiUrl = `${this.configurationService.getApiUrl()}/authors`
}
list(): Observable<Author[]> { list(): Observable<Author[]> {
return this.httpClient.get<Author[]>(this.url); return this.httpClient.get<Author[]>(this.apiUrl);
} }
login(name: string, password: string) { login(name: string, password: string) {
return this.httpClient.post<any>(`${this.url}/login`, {name: name, password: password}) return this.httpClient.post<any>(`${this.apiUrl}/login`, {name: name, password: password})
} }
me(token: string): Observable<Author> { me(token: string): Observable<Author> {
const headers = new HttpHeaders().set("Authorization", "Bearer " + token); const headers = new HttpHeaders().set("Authorization", "Bearer " + token);
return this.httpClient.get<Author>(`${this.url}/me`, {'headers': headers}); return this.httpClient.get<Author>(`${this.apiUrl}/me`, {'headers': headers});
} }
attributePost(authorId: string | undefined, postId: bigint, token: string) { attributePost(authorId: string | undefined, postId: bigint, token: string) {
@ -31,7 +35,7 @@ export class AuthorService {
'Authorization': `Bearer ${token}` 'Authorization': `Bearer ${token}`
}) })
}; };
return this.httpClient.put<any>(`${this.url}/${authorId}/posts`, postId, httpOptions); return this.httpClient.put<any>(`${this.apiUrl}/${authorId}/posts`, postId, httpOptions);
} }
updateAuthor(authorId: string, token: string, username: string, password: string): Observable<Author> { updateAuthor(authorId: string, token: string, username: string, password: string): Observable<Author> {
@ -40,7 +44,7 @@ export class AuthorService {
'Authorization': `Bearer ${token}` 'Authorization': `Bearer ${token}`
}) })
} }
return this.httpClient.put<Author>(`${this.url}/${authorId}`, {name: username, password: password}, httpOptions); return this.httpClient.put<Author>(`${this.apiUrl}/${authorId}`, {name: username, password: password}, httpOptions);
} }
changeAvatar(id: string | undefined, image: File | undefined, token: string) { changeAvatar(id: string | undefined, image: File | undefined, token: string) {
@ -52,7 +56,7 @@ export class AuthorService {
'Authorization': `Bearer ${token}` 'Authorization': `Bearer ${token}`
}) })
}; };
return this.httpClient.put<Author>(`${this.url}/${id}/avatar`, formData, httpOptions); return this.httpClient.put<Author>(`${this.apiUrl}/${id}/avatar`, formData, httpOptions);
} else { } else {
throw new Error('Image doesn\'t exist'); throw new Error('Image doesn\'t exist');
} }
@ -60,7 +64,7 @@ export class AuthorService {
getAuthor(id: string | null): Observable<Author> { getAuthor(id: string | null): Observable<Author> {
if (id) { if (id) {
return this.httpClient.get<Author>(`${this.url}/${id}`); return this.httpClient.get<Author>(`${this.apiUrl}/${id}`);
} else { } else {
throw new Error("Not Found"); throw new Error("Not Found");
} }
@ -72,14 +76,14 @@ export class AuthorService {
'Authorization': `Bearer ${token}` 'Authorization': `Bearer ${token}`
}) })
}; };
return this.httpClient.get<Post[]>(`${this.url}/${id}/posts`, httpOptions); return this.httpClient.get<Post[]>(`${this.apiUrl}/${id}/posts`, httpOptions);
} }
createAccount(username: string, password: string): Observable<Author> { createAccount(username: string, password: string): Observable<Author> {
return this.httpClient.post<Author>(`${this.url}/register`, {name: username, password: password}) return this.httpClient.post<Author>(`${this.apiUrl}/register`, {name: username, password: password})
} }
createAccountAdmin(username: string, password: string, role: string): Observable<Author> { createAccountAdmin(username: string, password: string, role: string): Observable<Author> {
return this.httpClient.post<Author>(`${this.url}/register`, {name: username, password: password, role: role}) return this.httpClient.post<Author>(`${this.apiUrl}/register`, {name: username, password: password, role: role})
} }
} }

View File

@ -2,16 +2,20 @@ import { Injectable } from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http'; import {HttpClient, HttpHeaders} from '@angular/common/http';
import {Observable} from 'rxjs'; import {Observable} from 'rxjs';
import {Comment} from '../models/comment'; import {Comment} from '../models/comment';
import {ConfigurationService} from '../configuration.service';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class CommentService { export class CommentService {
url: string = 'http://localhost:8080/api/comments'; apiUrl: string = '';
constructor(private httpClient: HttpClient) {} constructor(private httpClient: HttpClient,
private configurationService: ConfigurationService) {
this.apiUrl = `${this.configurationService.getApiUrl()}/comments`
}
list(postId: bigint) { list(postId: bigint) {
return this.httpClient.get<Comment[]>(`${this.url}/posts/${postId}`); return this.httpClient.get<Comment[]>(`${this.apiUrl}/posts/${postId}`);
} }
create(content: string, postId: bigint, authorId: string, token: string): Observable<Comment> { create(content: string, postId: bigint, authorId: string, token: string): Observable<Comment> {
@ -20,7 +24,7 @@ export class CommentService {
'Authorization': `Bearer ${token}` 'Authorization': `Bearer ${token}`
}) })
}; };
return this.httpClient.post<Comment>(`${this.url}/posts/${postId}`, {postId: postId, authorId: authorId, content: content}, httpOptions); return this.httpClient.post<Comment>(`${this.apiUrl}/posts/${postId}`, {postId: postId, authorId: authorId, content: content}, httpOptions);
} }
delete(commentId: bigint, token: string) { delete(commentId: bigint, token: string) {
@ -29,6 +33,6 @@ export class CommentService {
'Authorization': `Bearer ${token}` 'Authorization': `Bearer ${token}`
}) })
}; };
return this.httpClient.delete<Comment>(`${this.url}/${commentId}`, httpOptions); return this.httpClient.delete<Comment>(`${this.apiUrl}/${commentId}`, httpOptions);
} }
} }

View File

@ -3,24 +3,28 @@ import {Observable} from 'rxjs';
import {Post} from '../models/post'; import {Post} from '../models/post';
import {HttpClient, HttpHeaders} from '@angular/common/http'; import {HttpClient, HttpHeaders} from '@angular/common/http';
import {AuthorWithPost} from '../models/author-with-post'; import {AuthorWithPost} from '../models/author-with-post';
import {ConfigurationService} from '../configuration.service';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class PostService { export class PostService {
url: string = 'http://localhost:8080/api/posts'; apiUrl: string = '';
constructor(private httpClient: HttpClient) {} constructor(private httpClient: HttpClient,
private configurationService: ConfigurationService) {
this.apiUrl = `${this.configurationService.getApiUrl()}/posts`;
}
list(): Observable<Post[]> { list(): Observable<Post[]> {
return this.httpClient.get<Post[]>(this.url); return this.httpClient.get<Post[]>(this.apiUrl);
} }
listWithAuthors(): Observable<AuthorWithPost[]> { listWithAuthors(): Observable<AuthorWithPost[]> {
return this.httpClient.get<AuthorWithPost[]>(`${this.url}/authors`); return this.httpClient.get<AuthorWithPost[]>(`${this.apiUrl}/authors`);
} }
getPost(id: bigint) { getPost(id: bigint) {
return this.httpClient.get<Post>(`${this.url}/${id}`); return this.httpClient.get<Post>(`${this.apiUrl}/${id}`);
} }
deletePost(id: bigint, token: string) { deletePost(id: bigint, token: string) {
@ -29,7 +33,7 @@ export class PostService {
'Authorization': `Bearer ${token}` 'Authorization': `Bearer ${token}`
}) })
}; };
return this.httpClient.delete(`${this.url}/${id}`, httpOptions); return this.httpClient.delete(`${this.apiUrl}/${id}`, httpOptions);
} }
updatePost(postId: bigint, postData: any, token: string) { updatePost(postId: bigint, postData: any, token: string) {
@ -38,7 +42,7 @@ export class PostService {
'Authorization': `Bearer ${token}` 'Authorization': `Bearer ${token}`
}) })
}; };
return this.httpClient.put(`${this.url}/${postId}`, postData, httpOptions); return this.httpClient.put(`${this.apiUrl}/${postId}`, postData, httpOptions);
} }
@ -48,7 +52,7 @@ export class PostService {
'Authorization': `Bearer ${token}` 'Authorization': `Bearer ${token}`
}) })
}; };
return this.httpClient.post<Post>(this.url, post, httpOptions); return this.httpClient.post<Post>(this.apiUrl, post, httpOptions);
} }
changeIllustration(id: bigint | undefined, image: File | undefined, token: string): Observable<Post> { changeIllustration(id: bigint | undefined, image: File | undefined, token: string): Observable<Post> {
@ -61,7 +65,7 @@ export class PostService {
}) })
}; };
console.log(image); console.log(image);
return this.httpClient.put<Post>(`${this.url}/${id}/illustration`, formData, httpOptions); return this.httpClient.put<Post>(`${this.apiUrl}/${id}/illustration`, formData, httpOptions);
} else { } else {
throw new Error('Image doesn\'t exist'); throw new Error('Image doesn\'t exist');
} }