You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.8 KiB

  1. export class CommentItemList {
  2. constructor(public commentItems: CommentItem[]) {}
  3. static valueOf(items: CommentItem[]): CommentItemList {
  4. return new CommentItemList(items)
  5. }
  6. add(item: CommentItem) {
  7. this.commentItems.push(item)
  8. }
  9. update(item: CommentItem) {
  10. const index = this.commentItems.findIndex(comment => comment.id === item.id)
  11. this.commentItems.splice(index, 1, item)
  12. }
  13. delete(item: CommentItem) {
  14. this.commentItems = this.commentItems.filter(comment => comment.id !== item.id)
  15. }
  16. deleteBulk(items: CommentItemList) {
  17. const ids = items.ids()
  18. this.commentItems = this.commentItems.filter(comment => !ids.includes(comment.id))
  19. }
  20. count(): Number {
  21. return this.commentItems.length
  22. }
  23. ids(): Number[]{
  24. return this.commentItems.map(item => item.id)
  25. }
  26. toArray(): Object[] {
  27. return this.commentItems.map(item => item.toObject())
  28. }
  29. }
  30. export class CommentItem {
  31. constructor(
  32. public id: number,
  33. public user: number,
  34. public username: string,
  35. public document: number,
  36. public documentText: string,
  37. public text: string,
  38. public createdAt: string
  39. ) {}
  40. static valueOf(
  41. { id, user, username, document, document_text, text, created_at }:
  42. { id: number, user: number, username: string, document: number,
  43. document_text: string, text: string, created_at: string }
  44. ): CommentItem {
  45. return new CommentItem(id, user, username, document, document_text, text, created_at)
  46. }
  47. by(userId: number) {
  48. return this.user === userId
  49. }
  50. toObject(): Object {
  51. return {
  52. id: this.id,
  53. user: this.user,
  54. username: this.username,
  55. document: this.document,
  56. document_text: this.documentText,
  57. text: this.text,
  58. created_at: this.createdAt
  59. }
  60. }
  61. }