funky-framadate-front/src/app/core/services/poll.service.ts

60 lines
2.0 KiB
TypeScript

import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { MessageSeverity } from '../enums/message-severity.enum';
import { Poll } from '../models/poll.model';
import { ApiService } from './api.service';
import { MessageDisplayerService } from './message-displayer.service';
@Injectable({
providedIn: 'root',
})
export class PollService {
private _poll: BehaviorSubject<Poll | undefined> = new BehaviorSubject<Poll | undefined>(undefined);
public readonly poll: Observable<Poll | undefined> = this._poll.asObservable();
constructor(private apiService: ApiService, private messageService: MessageDisplayerService) {}
public updateCurrentPoll(poll: Poll): void {
this._poll.next(poll);
}
// SAVE
public async savePoll(poll: Poll): Promise<void> {
await this.apiService.savePoll(poll);
this.messageService.display(MessageSeverity.SUCCESS, 'Le sondage a été créé.');
}
public async saveVote(poll: Poll): Promise<void> {
await this.apiService.saveVote(poll);
this.messageService.display(MessageSeverity.SUCCESS, 'Votre participation au sondage a été enregistrée.');
}
public async saveComment(poll: Poll, comment: string): Promise<void> {
await this.apiService.saveComment(poll, comment);
this.messageService.display(MessageSeverity.SUCCESS, 'Votre commentaire a été enregistré.');
}
// GET
public async getPollByIdentifier(slug: string): Promise<void> {
const poll: Poll | undefined = await this.apiService.getPollByIdentifier(slug);
if (poll) {
this.updateCurrentPoll(poll);
}
}
// DELETE
public async deletePollVotes(poll: Poll): Promise<void> {
await this.apiService.deletePollVotes(poll);
this.messageService.display(
MessageSeverity.SUCCESS,
'Les participations des votants à ce sondage ont été supprimées.'
);
}
public async deletePollComments(poll: Poll): Promise<void> {
await this.apiService.deletePollComments(poll);
this.messageService.display(MessageSeverity.SUCCESS, 'Les commentaires de ce sondage ont été supprimés.');
}
}