front end version 2 de framadate, un gestionnaire de sondage
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

196 lines
5.8 KiB

import { Injectable } from '@angular/core';
import axios, { AxiosInstance, AxiosResponse } from 'axios';
import { environment } from 'src/environments/environment';
import { Poll } from '../models/poll.model';
import { User } from '../models/user.model';
@Injectable({
providedIn: 'root',
})
export class ApiService {
private axiosInstance: AxiosInstance;
private readonly pollsEndpoint = environment.api.endpoints.polls.name;
private readonly commentsEndpoint = environment.api.endpoints.polls.comments.name;
private readonly votesEndpoint = environment.api.endpoints.polls.votes.name;
private readonly slugsEndpoint = environment.api.endpoints.polls.slugs.name;
private readonly votesStacksEndpoint = environment.api.endpoints.voteStack.name;
private readonly usersEndpoint = environment.api.endpoints.users.name;
private readonly usersPollsEndpoint = environment.api.endpoints.users.polls.name;
private readonly usersPollsSendEmailEndpoint = environment.api.endpoints.users.polls.sendEmail.name;
constructor() {
this.axiosInstance = axios.create({ baseURL: environment.api.baseHref });
}
////////////
// CREATE //
////////////
public async savePoll(poll: Poll): Promise<void> {
try {
await this.axiosInstance.post(`${this.pollsEndpoint}`, { params: { config: poll.config } });
} catch (error) {
this.handleError(error);
}
}
public async saveVote(poll: Poll): Promise<void> {
try {
// TODO: add the votestack in the params
await this.axiosInstance.post(`${this.pollsEndpoint}/${poll.id}${this.votesEndpoint}`, {
params: { voteStack: {} },
});
} catch (error) {
this.handleError(error);
}
}
public async saveComment(poll: Poll, comment: string): Promise<void> {
try {
// TODO: add the comment in the params
await this.axiosInstance.post(`${this.pollsEndpoint}/${poll.id}${this.commentsEndpoint}`, {
params: { comment },
});
} catch (error) {
this.handleError(error);
}
}
//////////
// READ //
//////////
public async isSlugAvailable(slug: string): Promise<boolean> {
try {
// TODO: scenario should be : if we can get this slug, it exists. if not, it doesn't. It's just a GET.
const response: AxiosResponse = await this.axiosInstance.get(
`${this.pollsEndpoint}${this.slugsEndpoint}/${slug}`
);
if (response?.status !== 404) {
return false;
}
} catch (error) {
if (error.response?.status === 404) {
return true;
} else {
this.handleError(error);
}
}
}
public async sendEmailToUserOfItsPollsList(user: User): Promise<void> {
// If user is not authenticated: the list of polls is send to user's email by the backend.
try {
await this.axiosInstance.get<Poll[]>(
`${this.usersEndpoint}/${user.email}${this.usersPollsEndpoint}${this.usersPollsSendEmailEndpoint}`
);
} catch (error) {
this.handleError(error);
}
}
public async getPollsByUserEmail(user: User): Promise<Poll[]> {
// If user is authenticated : retrieve polls & display directly in frontend.
// TODO: Backend should handle this case. Actually the endpoint doesn't exist in backend.
try {
const response: AxiosResponse<Poll[]> = await this.axiosInstance.get<Poll[]>(
`${this.usersEndpoint}/${user.email}${this.usersPollsEndpoint}`
);
return response?.data;
} catch (error) {
this.handleError(error);
}
}
public async getPollByIdentifier(identifier: string): Promise<Poll | undefined> {
// TODO: identifier should be decided according to backend : Id || Slug ?
try {
const response: AxiosResponse<Poll> = await this.axiosInstance.get<Poll>(
`${this.pollsEndpoint}/${identifier}`
);
return response?.data;
} catch (error) {
if (error.response?.status === 404) {
return undefined;
} else {
this.handleError(error);
}
}
}
////////////
// UPDATE //
////////////
public async updatePoll(poll: Poll): Promise<void> {
try {
// TODO: implement the params when entities are finalized.
await this.axiosInstance.put(`${this.pollsEndpoint}/${poll.id}`, {
params: { voteStack: {}, token: '' },
});
} catch (error) {
this.handleError(error);
}
}
public async updateVote(voteStack: any): Promise<void> {
try {
// TODO: implement the params when entities are finalized.
await this.axiosInstance.patch(`${this.votesStacksEndpoint}/${voteStack.id}`, {
params: { voteStack: {}, token: '' },
});
} catch (error) {
this.handleError(error);
}
}
////////////
// DELETE //
////////////
public async deletePoll(poll: Poll): Promise<void> {
try {
await this.axiosInstance.delete(`${this.pollsEndpoint}${poll.id}`, {});
} catch (error) {
this.handleError(error);
}
}
public async deletePollVotes(poll: Poll): Promise<void> {
try {
// TODO: update endpoint in Backend
await this.axiosInstance.delete(`${this.pollsEndpoint}${poll.id}${this.votesEndpoint}`);
} catch (error) {
this.handleError(error);
}
}
public async deletePollComments(poll: Poll): Promise<void> {
try {
// TODO: modify endpoint in Backend
await this.axiosInstance.delete(`${this.pollsEndpoint}${poll.id}${this.commentsEndpoint}`);
} catch (error) {
this.handleError(error);
}
}
/////////////////////
// PRIVATE METHODS //
/////////////////////
private 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
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.log(error.config);
}
}