import { Injectable } from '@angular/core'; import axios, { 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 { //////////// // CREATE // //////////// public async savePoll(poll: Poll): Promise { try { await axios.post(`${environment.api.baseHref}${environment.api.endpoints.poll.name}`, { params: { config: poll.config }, }); } catch (error) { this.handleError(error); } } public async saveVote(poll: Poll): Promise { try { // TODO: add the votestack in the params await axios.post( `${environment.api.baseHref}${environment.api.endpoints.poll.name}/${poll.id}${environment.api.endpoints.poll.vote.name}`, { params: { voteStack: {} } } ); } catch (error) { this.handleError(error); } } public async saveComment(poll: Poll, comment: string): Promise { try { // TODO: add the comment in the params await axios.post( `${environment.api.baseHref}${environment.api.endpoints.poll.name}/${poll.id}${environment.api.endpoints.poll.comment.name}`, { params: { comment } } ); } catch (error) { this.handleError(error); } } ////////// // READ // ////////// public async isSlugAvailable(slug: string): Promise { 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 axios.get( `${environment.api.baseHref}${environment.api.endpoints.poll.slug.name}/${slug}` ); return response && response.status === 404 ? true : false; } catch (error) { this.handleError(error); } } public async sendEmailToUserOfItsPollsList(email: string): Promise { // If user is not authenticated: the list of polls is send to user's email by the backend. try { const response: AxiosResponse = await axios.get( `${environment.api.baseHref}${environment.api.endpoints.user.polls.sendEmail.name}/${email}` ); return response ? response.data : []; } catch (error) { this.handleError(error); } } public async getPollsByUserEmail(user: User): Promise { // 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 = await axios.get( `${environment.api.baseHref}${environment.api.endpoints.user.polls.name}/${user.email}` ); return response ? response.data : []; } catch (error) { this.handleError(error); } } public async getPollsByUrl(url: string): Promise { try { const response: AxiosResponse = await axios.get( `${environment.api.baseHref}${environment.api.endpoints.poll.name}/${url}` ); return response ? response.data : []; } catch (error) { this.handleError(error); } } public async getPollById(id: string, password?: string): Promise { try { const response: AxiosResponse = await axios.get( `${environment.api.baseHref}${environment.api.endpoints.poll.name}/${id}`, password ? { params: { password } } : {} ); return response ? response.data : []; } catch (error) { this.handleError(error); } } //////////// // UPDATE // //////////// public async updatePoll(poll: Poll): Promise { try { // TODO: implement the params when entities are finalized. await axios.put(`${environment.api.baseHref}${environment.api.endpoints.poll.name}/${poll.id}`, { params: { voteStack: {}, token: '' }, }); } catch (error) { this.handleError(error); } } public async updateVote(voteStack: any): Promise { try { // TODO: implement the params when entities are finalized. await axios.patch( `${environment.api.baseHref}${environment.api.endpoints.poll.vote.name}/${voteStack.id}`, { params: { voteStack: {}, token: '' }, } ); } catch (error) { this.handleError(error); } } //////////// // DELETE // //////////// public async deletePoll(poll: Poll): Promise { try { await axios.delete(`${environment.api.baseHref}${environment.api.endpoints.poll.name}${poll.id}`, {}); } catch (error) { this.handleError(error); } } public async deletePollVotes(poll: Poll): Promise { try { // TODO: update endpoint in Backend await axios.delete( `${environment.api.baseHref}${environment.api.endpoints.poll.name}${poll.id}${environment.api.endpoints.poll.vote.name}` ); } catch (error) { this.handleError(error); } } public async deletePollComments(poll: Poll): Promise { try { // TODO: modify endpoint in Backend await axios.delete( `${environment.api.baseHref}${environment.api.endpoints.poll.name}${poll.id}${environment.api.endpoints.poll.comment.name}` ); } 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); } }