Browse Source

Add comment model

pull/1216/head
Hironsan 3 years ago
parent
commit
c1fd662831
1 changed files with 64 additions and 0 deletions
  1. 64
      frontend/models/comment.ts

64
frontend/models/comment.ts

@ -0,0 +1,64 @@
export class CommentItemList {
constructor(public commentItems: CommentItem[]) {}
static valueOf(items: CommentItem[]): CommentItemList {
return new CommentItemList(items)
}
add(item: CommentItem) {
this.commentItems.push(item)
}
update(item: CommentItem) {
const index = this.commentItems.findIndex(comment => comment.id === item.id)
this.commentItems.splice(index, 1, item)
}
delete(item: CommentItem) {
this.commentItems = this.commentItems.filter(comment => comment.id !== item.id)
}
count(): Number {
return this.commentItems.length
}
toArray(): Object[] {
return this.commentItems.map(item => item.toObject())
}
}
export class CommentItem {
constructor(
public id: number,
public user: number,
public username: string,
public document: number,
public documentText: string,
public text: string,
public createdAt: string
) {}
static valueOf(
{ id, user, username, document, document_text, text, created_at }:
{ id: number, user: number, username: string, document: number,
document_text: string, text: string, created_at: string }
): CommentItem {
return new CommentItem(id, user, username, document, document_text, text, created_at)
}
by(userId: number) {
return this.user === userId
}
toObject(): Object {
return {
id: this.id,
user: this.user,
username: this.username,
document: this.document,
document_text: this.documentText,
text: this.text,
created_at: this.createdAt
}
}
}
Loading…
Cancel
Save