-
Notifications
You must be signed in to change notification settings - Fork 0
/
comment.ts
70 lines (61 loc) · 1.84 KB
/
comment.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
import {LengthValidator, RegExpValidator, RequiredValidator, Sdkzer} from "sdkzer";
/**
* Perform CRUD operations (Create, Read, Update and Delete) to deal with Comments.
* Comment is mapped to "http://localhost:8000/api/v1/comments" endpoint
*/
export interface CommentFields {
id: number | null;
user_id?: number | null;
post_id?: number | null;
date?: string;
comment?: string;
}
export default class Event extends Sdkzer<CommentFields> {
public baseEndpoint() {
return "http://localhost:8000/api/v1/comments";
}
private dateRegExp = /^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(\\.[0-9]+)?(Z)?$/;
protected validationRules = {
'user_id': [new RequiredValidator()],
'post_id': [new RequiredValidator()],
'name': [
new RequiredValidator(),
new LengthValidator({ min: 2, max: 20 })
],
'date': [
new RequiredValidator(),
new RegExpValidator({
// validates a date like 2023-12-15T08:30:30Z
rule: this.dateRegExp
})
]
}
public defaults() {
return {
post_id: null,
date: "",
comment: ""
};
}
/*
* Your instance methods hold your business logic using the state of
* the record instance
*/
public isToday() {
return (this.attr("date") === new Date().toISOString().slice(0, 10));
}
/*
* Your static methods to retrieve collections
*/
public static fetchIndexByUser(userId: number) {
// This query will merged on top of the default Comment.fetchIndex() HttpQuery
const indexByUserIdHttpQuery = {
qsParams: {
'user_id': userId
}
};
// Always use the default fetchIndex() passing your custom HttpQuery
// to retrieve collections. if you need to override it do it
return Event.fetchIndex(indexByUserIdHttpQuery);
}
}