diff --git a/.gitignore b/.gitignore index cc7b141..d19bef8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ # See https://docs.github.com/get-started/getting-started-with-git/ignoring-files for more about ignoring files. +package-lock.json + # Compiled output /dist /tmp diff --git a/configuration.json b/configuration.json new file mode 100644 index 0000000..aba0111 --- /dev/null +++ b/configuration.json @@ -0,0 +1,3 @@ +{ + "apiURL": "http://localhost:8080/api" +} diff --git a/src/app/configuration.service.ts b/src/app/configuration.service.ts new file mode 100644 index 0000000..006ac23 --- /dev/null +++ b/src/app/configuration.service.ts @@ -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 + } +} diff --git a/src/app/services/author.service.ts b/src/app/services/author.service.ts index 8291e88..b152a32 100644 --- a/src/app/services/author.service.ts +++ b/src/app/services/author.service.ts @@ -3,26 +3,30 @@ import {HttpClient, HttpHeaders} from '@angular/common/http'; import {Observable} from 'rxjs'; import {Author} from '../models/author'; import {Post} from '../models/post'; +import {ConfigurationService} from '../configuration.service'; @Injectable({ providedIn: 'root' }) 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 { - return this.httpClient.get(this.url); + return this.httpClient.get(this.apiUrl); } login(name: string, password: string) { - return this.httpClient.post(`${this.url}/login`, {name: name, password: password}) + return this.httpClient.post(`${this.apiUrl}/login`, {name: name, password: password}) } me(token: string): Observable { const headers = new HttpHeaders().set("Authorization", "Bearer " + token); - return this.httpClient.get(`${this.url}/me`, {'headers': headers}); + return this.httpClient.get(`${this.apiUrl}/me`, {'headers': headers}); } attributePost(authorId: string | undefined, postId: bigint, token: string) { @@ -31,7 +35,7 @@ export class AuthorService { 'Authorization': `Bearer ${token}` }) }; - return this.httpClient.put(`${this.url}/${authorId}/posts`, postId, httpOptions); + return this.httpClient.put(`${this.apiUrl}/${authorId}/posts`, postId, httpOptions); } updateAuthor(authorId: string, token: string, username: string, password: string): Observable { @@ -40,7 +44,7 @@ export class AuthorService { 'Authorization': `Bearer ${token}` }) } - return this.httpClient.put(`${this.url}/${authorId}`, {name: username, password: password}, httpOptions); + return this.httpClient.put(`${this.apiUrl}/${authorId}`, {name: username, password: password}, httpOptions); } changeAvatar(id: string | undefined, image: File | undefined, token: string) { @@ -52,7 +56,7 @@ export class AuthorService { 'Authorization': `Bearer ${token}` }) }; - return this.httpClient.put(`${this.url}/${id}/avatar`, formData, httpOptions); + return this.httpClient.put(`${this.apiUrl}/${id}/avatar`, formData, httpOptions); } else { throw new Error('Image doesn\'t exist'); } @@ -60,7 +64,7 @@ export class AuthorService { getAuthor(id: string | null): Observable { if (id) { - return this.httpClient.get(`${this.url}/${id}`); + return this.httpClient.get(`${this.apiUrl}/${id}`); } else { throw new Error("Not Found"); } @@ -72,14 +76,14 @@ export class AuthorService { 'Authorization': `Bearer ${token}` }) }; - return this.httpClient.get(`${this.url}/${id}/posts`, httpOptions); + return this.httpClient.get(`${this.apiUrl}/${id}/posts`, httpOptions); } createAccount(username: string, password: string): Observable { - return this.httpClient.post(`${this.url}/register`, {name: username, password: password}) + return this.httpClient.post(`${this.apiUrl}/register`, {name: username, password: password}) } createAccountAdmin(username: string, password: string, role: string): Observable { - return this.httpClient.post(`${this.url}/register`, {name: username, password: password, role: role}) + return this.httpClient.post(`${this.apiUrl}/register`, {name: username, password: password, role: role}) } } diff --git a/src/app/services/comment.service.ts b/src/app/services/comment.service.ts index df0366c..4a1e365 100644 --- a/src/app/services/comment.service.ts +++ b/src/app/services/comment.service.ts @@ -2,16 +2,20 @@ import { Injectable } from '@angular/core'; import {HttpClient, HttpHeaders} from '@angular/common/http'; import {Observable} from 'rxjs'; import {Comment} from '../models/comment'; +import {ConfigurationService} from '../configuration.service'; @Injectable({ providedIn: 'root' }) export class CommentService { - url: string = 'http://localhost:8080/api/comments'; - constructor(private httpClient: HttpClient) {} + apiUrl: string = ''; + constructor(private httpClient: HttpClient, + private configurationService: ConfigurationService) { + this.apiUrl = `${this.configurationService.getApiUrl()}/comments` + } list(postId: bigint) { - return this.httpClient.get(`${this.url}/posts/${postId}`); + return this.httpClient.get(`${this.apiUrl}/posts/${postId}`); } create(content: string, postId: bigint, authorId: string, token: string): Observable { @@ -20,7 +24,7 @@ export class CommentService { 'Authorization': `Bearer ${token}` }) }; - return this.httpClient.post(`${this.url}/posts/${postId}`, {postId: postId, authorId: authorId, content: content}, httpOptions); + return this.httpClient.post(`${this.apiUrl}/posts/${postId}`, {postId: postId, authorId: authorId, content: content}, httpOptions); } delete(commentId: bigint, token: string) { @@ -29,6 +33,6 @@ export class CommentService { 'Authorization': `Bearer ${token}` }) }; - return this.httpClient.delete(`${this.url}/${commentId}`, httpOptions); + return this.httpClient.delete(`${this.apiUrl}/${commentId}`, httpOptions); } } diff --git a/src/app/services/post.service.ts b/src/app/services/post.service.ts index cf40876..a519adf 100644 --- a/src/app/services/post.service.ts +++ b/src/app/services/post.service.ts @@ -3,24 +3,28 @@ import {Observable} from 'rxjs'; import {Post} from '../models/post'; import {HttpClient, HttpHeaders} from '@angular/common/http'; import {AuthorWithPost} from '../models/author-with-post'; +import {ConfigurationService} from '../configuration.service'; @Injectable({ providedIn: 'root' }) export class PostService { - url: string = 'http://localhost:8080/api/posts'; - constructor(private httpClient: HttpClient) {} + apiUrl: string = ''; + constructor(private httpClient: HttpClient, + private configurationService: ConfigurationService) { + this.apiUrl = `${this.configurationService.getApiUrl()}/posts`; + } list(): Observable { - return this.httpClient.get(this.url); + return this.httpClient.get(this.apiUrl); } listWithAuthors(): Observable { - return this.httpClient.get(`${this.url}/authors`); + return this.httpClient.get(`${this.apiUrl}/authors`); } getPost(id: bigint) { - return this.httpClient.get(`${this.url}/${id}`); + return this.httpClient.get(`${this.apiUrl}/${id}`); } deletePost(id: bigint, token: string) { @@ -29,7 +33,7 @@ export class PostService { '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) { @@ -38,7 +42,7 @@ export class PostService { '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}` }) }; - return this.httpClient.post(this.url, post, httpOptions); + return this.httpClient.post(this.apiUrl, post, httpOptions); } changeIllustration(id: bigint | undefined, image: File | undefined, token: string): Observable { @@ -61,7 +65,7 @@ export class PostService { }) }; console.log(image); - return this.httpClient.put(`${this.url}/${id}/illustration`, formData, httpOptions); + return this.httpClient.put(`${this.apiUrl}/${id}/illustration`, formData, httpOptions); } else { throw new Error('Image doesn\'t exist'); }