diff --git a/src/app/components/shared/comment-page/comment-page.component.ts b/src/app/components/shared/comment-page/comment-page.component.ts index 1510402b7775bc6192972a243325f6a18e67bc73..33fd31461df20c963d77b8cb3a6a994d90c8027b 100644 --- a/src/app/components/shared/comment-page/comment-page.component.ts +++ b/src/app/components/shared/comment-page/comment-page.component.ts @@ -1,15 +1,12 @@ -import { Component, OnInit, ViewChild } from '@angular/core'; +import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { Comment } from '../../../models/comment'; import { User } from '../../../models/user'; -import { CommentService } from '../../../services/http/comment.service'; import { NotificationService } from '../../../services/util/notification.service'; -import { CommentListComponent } from '../comment-list/comment-list.component'; import { AuthenticationService } from '../../../services/http/authentication.service'; import { MatDialog } from '@angular/material'; import { SubmitCommentComponent } from '../_dialogs/submit-comment/submit-comment.component'; -import { RxStompService } from '@stomp/ng2-stompjs'; -import { CreateComment } from '../../../models/messages/create-comment'; +import { WsCommentServiceService } from '../../../services/websockets/ws-comment-service.service'; @Component({ selector: 'app-comment-page', @@ -20,14 +17,11 @@ export class CommentPageComponent implements OnInit { roomId: string; user: User; - @ViewChild(CommentListComponent) child: CommentListComponent; - constructor(private route: ActivatedRoute, - private commentService: CommentService, private notification: NotificationService, public dialog: MatDialog, - private rxStompService: RxStompService, - private authenticationService: AuthenticationService) { } + private authenticationService: AuthenticationService, + private wsCommentService: WsCommentServiceService) { } ngOnInit(): void { this.roomId = localStorage.getItem('roomId'); @@ -51,27 +45,6 @@ export class CommentPageComponent implements OnInit { } send(comment: Comment): void { - /*this.commentService.addComment({ - id: '', - roomId: comment.roomId, - userId: comment.userId, - subject: comment.subject, - body: comment.body, - creationTimestamp: comment.creationTimestamp, - read: false, - revision: '' - } as Comment).subscribe(() => { - this.child.getComments(); - this.notification.show(`Comment '${comment.subject}' successfully created.`); - });*/ - const message = new CreateComment(comment.roomId, comment.userId, comment.body); - this.rxStompService.publish({ - destination: `/queue/comment.command`, - body: JSON.stringify(message), - headers: { - 'content-type': 'application/json' - } - }); - + this.wsCommentService.add(comment); } } diff --git a/src/app/components/shared/comment/comment.component.ts b/src/app/components/shared/comment/comment.component.ts index 0ee9be9f73c1dc47fd66df382a089f4a794be2c3..09ecfd90845cc996fdc4100bea9d559d042c0c72 100644 --- a/src/app/components/shared/comment/comment.component.ts +++ b/src/app/components/shared/comment/comment.component.ts @@ -7,6 +7,7 @@ import { CommentService } from '../../../services/http/comment.service'; import { NotificationService } from '../../../services/util/notification.service'; import { TranslateService } from '@ngx-translate/core'; import { LanguageService } from '../../../services/util/language.service'; +import { WsCommentServiceService } from '../../../services/websockets/ws-comment-service.service'; @Component({ selector: 'app-comment', @@ -24,7 +25,8 @@ export class CommentComponent implements OnInit { private commentService: CommentService, private notification: NotificationService, private translateService: TranslateService, - protected langService: LanguageService) { + protected langService: LanguageService, + private wsCommentService: WsCommentServiceService) { langService.langEmitter.subscribe(lang => translateService.use(lang)); } ngOnInit() { @@ -35,18 +37,18 @@ export class CommentComponent implements OnInit { } setRead(comment: Comment): void { - comment.read = !comment.read; - this.commentService.updateComment(comment).subscribe(); + this.comment = this.wsCommentService.toggleRead(comment); + // this.commentService.updateComment(comment).subscribe(); } setCorrect(comment: Comment): void { - comment.correct = !comment.correct; - this.commentService.updateComment(comment).subscribe(); + this.comment = this.wsCommentService.toggleCorrect(comment); + // this.commentService.updateComment(comment).subscribe(); } setFavorite(comment: Comment): void { - comment.favorite = !comment.favorite; - this.commentService.updateComment(comment).subscribe(); + this.comment = this.wsCommentService.toggleFavorite(comment); + // this.commentService.updateComment(comment).subscribe(); } delete(comment: Comment): void { diff --git a/src/app/models/messages/patch-comment.ts b/src/app/models/messages/patch-comment.ts index eefd649d18fa9a4327da8dce73fd06736934d833..2c76e2ab1d39ba87cc0dcf3198a95ed3e36562c5 100644 --- a/src/app/models/messages/patch-comment.ts +++ b/src/app/models/messages/patch-comment.ts @@ -1,17 +1,15 @@ export class PatchComment { type: string; payload: { - roomId: string; - creatorId: string; - body: string; + commentId; + changes: Map<string, any>; }; - constructor(roomId: string, creatorId: string, body: string) { + constructor(commentId: string, changes: Map<string, any>) { this.type = 'PatchComment'; this.payload = { - roomId: roomId, - creatorId: creatorId, - body: body + commentId: commentId, + changes: changes }; } } diff --git a/src/app/services/http/comment.service.ts b/src/app/services/http/comment.service.ts index d8673aa279dd3247b6afc5c2c36c2057201a47df..a25a5c07f83ed9ee6289b0b4746ba3f5543449d2 100644 --- a/src/app/services/http/comment.service.ts +++ b/src/app/services/http/comment.service.ts @@ -66,4 +66,6 @@ export class CommentService extends BaseHttpService { catchError(this.handleError<any>('updateComment')) ); } + + patchComment } diff --git a/src/app/services/websockets/ws-comment-service.service.ts b/src/app/services/websockets/ws-comment-service.service.ts index 696cf635b4b41625448d5a86d2eda1268ec4cd17..1e35adfdf2a8e3a3c1ae104afec7039b47df3433 100644 --- a/src/app/services/websockets/ws-comment-service.service.ts +++ b/src/app/services/websockets/ws-comment-service.service.ts @@ -1,6 +1,8 @@ import { Injectable } from '@angular/core'; import { Comment } from '../../models/comment'; import { RxStompService } from '@stomp/ng2-stompjs'; +import { CreateComment } from '../../models/messages/create-comment'; +import { PatchComment } from '../../models/messages/patch-comment'; @Injectable({ @@ -11,8 +13,48 @@ export class WsCommentServiceService { constructor(private rxStompService: RxStompService) { } add(comment: Comment): void { + const message = new CreateComment(comment.roomId, comment.userId, comment.body); + this.rxStompService.publish({ + destination: `/queue/comment.command`, + body: JSON.stringify(message), + headers: { + 'content-type': 'application/json' + } + }); + } + + toggleRead(comment: Comment): Comment { + comment.read = !comment.read; + const payload = new Map<string, any>(); + payload.set('mark', comment.read); + this.patchComment(comment, payload); + return comment; + } + toggleFavorite(comment: Comment): Comment { + comment.favorite = !comment.favorite; + const payload = new Map<string, any>(); + payload.set('correct', comment.read); + this.patchComment(comment, payload); + return comment; } + toggleCorrect(comment: Comment): Comment { + comment.correct = !comment.correct; + const payload = new Map<string, any>(); + payload.set('correct', comment.read); + this.patchComment(comment, payload); + return comment; + } + private patchComment(comment: Comment, changes: Map<string, any>): void { + const message = new PatchComment(comment.id, changes); + this.rxStompService.publish({ + destination: `/queue/comment.command`, + body: JSON.stringify(message), + headers: { + 'content-type': 'application/json' + } + }); + } }