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

55 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-05-12 19:16:23 +02:00
import { environment } from 'src/environments/environment';
2020-05-05 18:17:12 +02:00
import { Choice } from './choice.model';
import { Comment } from './comment.model';
2020-10-29 18:43:19 +01:00
import { PollConfiguration } from './configuration.model';
import { User } from './user.model';
export class Poll {
constructor(
2020-05-05 18:17:12 +02:00
public owner: User,
public slug: string,
2020-10-29 18:43:19 +01:00
public title: string,
2020-05-12 19:16:23 +02:00
public description?: string,
2020-10-29 18:43:19 +01:00
public configuration: PollConfiguration = new PollConfiguration(),
2020-05-12 19:16:23 +02:00
public comments: Comment[] = [],
2020-06-12 19:17:39 +02:00
public choices: Choice[] = []
) {}
2020-05-12 19:16:23 +02:00
public getAdministrationUrl(): string {
return `${environment.api.baseHref}/administration/polls/${this.slug}`;
}
public getParticipationUrl(): string {
return `${environment.api.baseHref}/participation/polls/${this.slug}`;
}
public static adaptFromLocalJsonServer(
2020-10-29 18:43:19 +01:00
item: Pick<Poll, 'owner' | 'title' | 'description' | 'slug' | 'configuration' | 'comments' | 'choices'>
2020-05-12 19:16:23 +02:00
): Poll {
const poll = new Poll(
new User(item.owner.pseudo, item.owner.email, undefined),
item.slug,
2020-10-29 18:43:19 +01:00
item.title,
2020-05-12 19:16:23 +02:00
item.description,
item.configuration,
item.comments
.map(
(c: Pick<Comment, 'author' | 'content' | 'dateCreated'>) =>
new Comment(c.author, c.content, new Date(c.dateCreated))
)
.sort(Comment.sortChronologically),
2020-06-12 19:17:39 +02:00
item.choices.map((c: Pick<Choice, 'label' | 'imageUrl' | 'participants' | 'counts'>) => {
2020-05-12 19:16:23 +02:00
const choice = new Choice(c.label, c.imageUrl, new Map(c.participants));
choice.participants.forEach((value, key) => {
choice.participants.set(key, new Set(value));
});
choice.updateCounts();
return choice;
})
);
return poll;
}
}