funky-framadate-front/src/app/core/models/choice.model.ts

40 lines
1.0 KiB
TypeScript
Raw Normal View History

2020-06-12 19:17:39 +02:00
import { Answer } from '../enums/answer.enum';
import { User } from './user.model';
2020-05-05 18:17:12 +02:00
export class Choice {
2020-05-12 19:16:23 +02:00
constructor(
public label: string,
public imageUrl?: string,
2020-06-12 19:17:39 +02:00
public participants: Map<Answer, Set<User>> = new Map<Answer, Set<User>>([
[Answer.YES, new Set<User>()],
[Answer.NO, new Set<User>()],
[Answer.MAYBE, new Set<User>()],
2020-05-12 19:16:23 +02:00
]),
2020-06-12 19:17:39 +02:00
public counts: Map<Answer, number> = new Map<Answer, number>([
[Answer.YES, 0],
[Answer.NO, 0],
[Answer.MAYBE, 0],
2020-05-12 19:16:23 +02:00
])
) {}
2020-06-12 19:17:39 +02:00
public updateParticipation(user: User, responseType: Answer): void {
this.removeParticipant(user);
this.participants.get(responseType).add(user);
2020-05-12 19:16:23 +02:00
this.updateCounts();
}
2020-06-12 19:17:39 +02:00
public removeParticipant(user: User): void {
for (const responseType of Object.values(Answer)) {
if (this.participants.get(responseType).has(user)) {
this.participants.get(responseType).delete(user);
2020-05-12 19:16:23 +02:00
}
}
}
public updateCounts(): void {
2020-06-12 19:17:39 +02:00
for (const responseType of Object.values(Answer)) {
2020-05-12 19:16:23 +02:00
this.counts.set(responseType, this.participants.get(responseType).size);
}
}
2020-05-05 18:17:12 +02:00
}