/
project.ts
78 lines (60 loc) · 1.77 KB
/
project.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { ProjectImages } from './types';
import { ProjectAttributes } from './types';
import { Client } from './client';
import { User } from './user';
export class Project {
id: number;
title: string;
description: string;
instructions: string;
author: User;
image: string;
images: ProjectImages;
createdAt: Date;
modifiedAt: Date;
sharedAt: Date;
views: number;
loves: number;
favorites: number;
comments: number;
remixes: number;
parentId: number | null;
rootId: number | null;
constructor(attributes: ProjectAttributes, private readonly client: Client) {
this.id = attributes.id;
this.title = attributes.title;
this.description = attributes.description;
this.instructions = attributes.instructions;
this.author = new User(attributes.author, client);
this.image = attributes.image;
this.images = attributes.images;
this.createdAt = new Date(attributes.history.created);
this.modifiedAt = new Date(attributes.history.modified);
this.sharedAt = new Date(attributes.history.shared);
this.views = attributes.stats.views;
this.loves = attributes.stats.loves;
this.favorites = attributes.stats.favorites;
this.comments = attributes.stats.comments;
this.remixes = attributes.stats.remixes;
this.parentId = attributes.remix.parent;
this.rootId = attributes.remix.root;
}
get url(): string {
return `https://scratch.mit.edu/projects/${this.id}/`;
}
get isRemix(): boolean {
return !!this.parentId;
}
parent(): Promise<Project> | undefined {
if (!this.parentId) {
return;
}
return this.client.project(this.parentId);
}
root(): Promise<Project> | undefined {
if (!this.rootId) {
return;
}
return this.client.project(this.rootId);
}
}