|
|
|
@ -4,6 +4,7 @@ import { environment } from 'src/environments/environment';
|
|
|
|
|
|
|
|
|
|
import { Answer } from '../enums/answer.enum'; |
|
|
|
|
import { Poll } from '../models/poll.model'; |
|
|
|
|
import { HttpHeaders } from '@angular/common/http'; |
|
|
|
|
|
|
|
|
|
@Injectable({ |
|
|
|
|
providedIn: 'root', |
|
|
|
@ -32,7 +33,7 @@ export class ApiService {
|
|
|
|
|
try { |
|
|
|
|
return await this.axiosInstance.post(`${this.pollsEndpoint}`, poll); |
|
|
|
|
} catch (error) { |
|
|
|
|
this.handleError(error); |
|
|
|
|
ApiService.handleError(error); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -49,7 +50,7 @@ export class ApiService {
|
|
|
|
|
response, |
|
|
|
|
}); |
|
|
|
|
} catch (error) { |
|
|
|
|
this.handleError(error); |
|
|
|
|
ApiService.handleError(error); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -57,7 +58,7 @@ export class ApiService {
|
|
|
|
|
try { |
|
|
|
|
return await this.axiosInstance.post(`${this.pollsEndpoint}/${slug}${this.commentsEndpoint}`, comment); |
|
|
|
|
} catch (error) { |
|
|
|
|
this.handleError(error); |
|
|
|
|
ApiService.handleError(error); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -70,7 +71,7 @@ export class ApiService {
|
|
|
|
|
const response: AxiosResponse<Poll[]> = await this.axiosInstance.get<Poll[]>(`${this.pollsEndpoint}`); |
|
|
|
|
return response?.data; |
|
|
|
|
} catch (error) { |
|
|
|
|
this.handleError(error); |
|
|
|
|
ApiService.handleError(error); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -101,7 +102,7 @@ export class ApiService {
|
|
|
|
|
if (error.response?.status === 404) { |
|
|
|
|
return undefined; |
|
|
|
|
} else { |
|
|
|
|
this.handleError(error); |
|
|
|
|
ApiService.handleError(error); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -119,7 +120,7 @@ export class ApiService {
|
|
|
|
|
if (error.response?.status === 404) { |
|
|
|
|
return true; |
|
|
|
|
} else { |
|
|
|
|
this.handleError(error); |
|
|
|
|
ApiService.handleError(error); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -131,7 +132,7 @@ export class ApiService {
|
|
|
|
|
`${this.usersEndpoint}/${email}${this.usersPollsEndpoint}${this.usersPollsSendEmailEndpoint}` |
|
|
|
|
); |
|
|
|
|
} catch (error) { |
|
|
|
|
this.handleError(error); |
|
|
|
|
ApiService.handleError(error); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -145,7 +146,7 @@ export class ApiService {
|
|
|
|
|
); |
|
|
|
|
return response?.data; |
|
|
|
|
} catch (error) { |
|
|
|
|
this.handleError(error); |
|
|
|
|
ApiService.handleError(error); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -160,7 +161,7 @@ export class ApiService {
|
|
|
|
|
answer, |
|
|
|
|
}); |
|
|
|
|
} catch (error) { |
|
|
|
|
this.handleError(error); |
|
|
|
|
ApiService.handleError(error); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -172,7 +173,7 @@ export class ApiService {
|
|
|
|
|
const response: AxiosResponse = await this.axiosInstance.delete(`${this.pollsEndpoint}/${slug}`); |
|
|
|
|
return response?.status === 204; |
|
|
|
|
} catch (error) { |
|
|
|
|
this.handleError(error); |
|
|
|
|
ApiService.handleError(error); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -183,7 +184,7 @@ export class ApiService {
|
|
|
|
|
); |
|
|
|
|
return response?.status === 204; |
|
|
|
|
} catch (error) { |
|
|
|
|
this.handleError(error); |
|
|
|
|
ApiService.handleError(error); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -194,14 +195,35 @@ export class ApiService {
|
|
|
|
|
); |
|
|
|
|
return response?.status === 204; |
|
|
|
|
} catch (error) { |
|
|
|
|
this.handleError(error); |
|
|
|
|
ApiService.handleError(error); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/////////////////////
|
|
|
|
|
// PRIVATE METHODS //
|
|
|
|
|
/////////////////////
|
|
|
|
|
private handleError(error): void { |
|
|
|
|
/** |
|
|
|
|
* prepare headers like the charset and json type for any call to the backend |
|
|
|
|
* @param bodyContent? |
|
|
|
|
*/ |
|
|
|
|
private makeHeaders(bodyContent?: any) { |
|
|
|
|
const headerDict = { |
|
|
|
|
Charset: 'UTF-8', |
|
|
|
|
'Content-Type': 'application/json', |
|
|
|
|
Accept: 'application/json', |
|
|
|
|
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', |
|
|
|
|
'Access-Control-Allow-Origin': '*', |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const requestOptions = { |
|
|
|
|
headers: new HttpHeaders(headerDict), |
|
|
|
|
body: bodyContent, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
return requestOptions; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static handleError(error): void { |
|
|
|
|
if (error.response) { |
|
|
|
|
// The request was made and the server responded with a status code
|
|
|
|
|
// that falls out of the range of 2xx
|
|
|
|
|