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 = new BehaviorSubject(undefined); public readonly poll: Observable = 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 { await this.apiService.savePoll(poll); this.messageService.display(MessageSeverity.SUCCESS, 'Le sondage a été créé.'); } public async saveVote(poll: Poll): Promise { 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 { await this.apiService.saveComment(poll, comment); this.messageService.display(MessageSeverity.SUCCESS, 'Votre commentaire a été enregistré.'); } // GET public async getPollByIdentifier(slug: string): Promise { this.updateCurrentPoll(await this.apiService.getPollByIdentifier(slug)); } // DELETE public async deletePollVotes(poll: Poll): Promise { 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 { await this.apiService.deletePollComments(poll); this.messageService.display(MessageSeverity.SUCCESS, 'Les commentaires de ce sondage ont été supprimés.'); } }