funky-framadate-front/src/app/shared/services/api.service.ts

196 lines
6.8 KiB
TypeScript

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<void> {
try {
await axios.post(`${environment.api.baseHref}${environment.api.endpoints.poll.root}`, {
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 axios.post(
`${environment.api.baseHref}${environment.api.endpoints.poll.root}/${poll.id}${environment.api.endpoints.vote}`,
{ 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 axios.post(
`${environment.api.baseHref}${environment.api.endpoints.poll.root}/${poll.id}${environment.api.endpoints.comment}`,
{ params: { comment } }
);
} catch (error) {
this.handleError(error);
}
}
//////////
// READ //
//////////
public async isSlugAvailable(slug: string): Promise<boolean> {
try {
// TODO: what is the return of the API ? could it be changed to an object { isAvailable: true } ?
const response: AxiosResponse = await axios.get(
`${environment.api.baseHref}${environment.api.endpoints.slug.isAvailable}/${slug}`
);
if (response) {
const { isAvailable } = response.data;
return isAvailable;
}
} catch (error) {
this.handleError(error);
}
}
public async sendUserPollsByEmail(email: string): Promise<Poll[]> {
// If user is not authenticated: the list of polls is send to user's email by the backend.
try {
const response: AxiosResponse<Poll[]> = await axios.get<Poll[]>(
`${environment.api.baseHref}${environment.api.endpoints.poll.byEmail.sendEmail}/${email}`
);
return response ? response.data : [];
} catch (error) {
this.handleError(error);
}
}
public async getUserPollsByEmail(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 axios.get<Poll[]>(
`${environment.api.baseHref}${environment.api.endpoints.poll.byEmail.retrieve}/${user.email}`
);
return response ? response.data : [];
} catch (error) {
this.handleError(error);
}
}
public async getPollsByUrl(url: string): Promise<Poll[]> {
try {
const response: AxiosResponse<Poll[]> = await axios.get<Poll[]>(
`${environment.api.baseHref}${environment.api.endpoints.poll.byUrl}/${url}`
);
return response ? response.data : [];
} catch (error) {
this.handleError(error);
}
}
public async getPollById(id: string, password?: string): Promise<Poll[]> {
try {
const response: AxiosResponse<Poll[]> = await axios.get<Poll[]>(
`${environment.api.baseHref}${environment.api.endpoints.poll.root}/${id}`,
password ? { params: { password } } : {}
);
return response ? response.data : [];
} catch (error) {
this.handleError(error);
}
}
////////////
// UPDATE //
////////////
public async updatePoll(poll: Poll): Promise<void> {
try {
// TODO: implement the params when entities are finalized.
await axios.put(`${environment.api.baseHref}${environment.api.endpoints.poll.root}/${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 axios.patch(`${environment.api.baseHref}${environment.api.endpoints.vote.root}/${voteStack.id}`, {
params: { voteStack: {}, token: '' },
});
} catch (error) {
this.handleError(error);
}
}
////////////
// DELETE //
////////////
public async deletePoll(poll: Poll): Promise<void> {
try {
await axios.delete(`${environment.api.baseHref}${environment.api.endpoints.poll.root}${poll.id}`, {});
} catch (error) {
this.handleError(error);
}
}
public async deletePollVotes(poll: Poll): Promise<void> {
try {
// TODO: update endpoint in Backend
await axios.delete(
`${environment.api.baseHref}${environment.api.endpoints.poll.root}${poll.id}${environment.api.endpoints.vote.root}`
);
} catch (error) {
this.handleError(error);
}
}
public async deletePollComments(poll: Poll): Promise<void> {
try {
// TODO: modify endpoint in Backend
await axios.delete(
`${environment.api.baseHref}${environment.api.endpoints.poll.root}${poll.id}${environment.api.endpoints.comment.root}`
);
} 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);
}
}