Skip to content

Commit

Permalink
Merge pull request #54 from python-sinhala-education-society/develop
Browse files Browse the repository at this point in the history
Merge develop to master
  • Loading branch information
SudharakaP authored Jul 28, 2019
2 parents ed2c3e4 + 0433ed1 commit d276596
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 22 deletions.
17 changes: 16 additions & 1 deletion src/main/java/com/asanka/tutor/domain/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public class Comment extends AbstractAuditingEntity implements Serializable {
@Field("replies")
private List<CommentReply> replies;

@Field("is_admin_comment")
private Boolean isAdminComment;

// jhipster-needle-entity-add-field - JHipster will add fields here, do not remove
public String getId() {
return id;
Expand Down Expand Up @@ -121,7 +124,19 @@ public void setReplies(List<CommentReply> replies) {
this.replies = replies;
}

// jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here, do not remove
public Boolean getIsAdminComment() {
return isAdminComment;
}

public Comment isAdminComment(Boolean isAdminComment) {
this.isAdminComment = isAdminComment;
return this;
}

public void setIsAdminComment(Boolean adminComment) {
isAdminComment = adminComment;
}
// jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here, do not remove

@Override
public boolean equals(Object o) {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/asanka/tutor/domain/CommentReply.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class CommentReply implements Serializable {
private String id;
private String commentID;
private String replyBody;
private boolean isAdminReply;
private String createdBy;
private boolean isApproved;
private Instant createdDate = Instant.now();
Expand Down Expand Up @@ -66,6 +67,14 @@ public void setApproved(boolean approved) {
isApproved = approved;
}

public boolean getIsAdminReply() {
return isAdminReply;
}

public void setIsAdminReply(boolean isAdminReply) {
this.isAdminReply = isAdminReply;
}

@Override
public String toString() {
return "CommentReply{" +
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/asanka/tutor/service/dto/CommentDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class CommentDTO implements Serializable {
@NotNull
private Boolean isApproved;

private Boolean isAdminComment;

private List<CommentReply> replies;

private Integer likesCount;
Expand Down Expand Up @@ -128,6 +130,14 @@ public void setReplies(List<CommentReply> replies) {
this.replies = replies;
}

public Boolean getIsAdminComment() {
return isAdminComment;
}

public void setIsAdminComment(Boolean adminComment) {
isAdminComment = adminComment;
}

// This constructor is only used for Jackson and should not be used for anything else.
public CommentDTO () {
// Empty constructor needed for Jackson.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public CommentDTO update(CommentDTO commentDTO) {
commentModified.setVideoID(commentDTO.getVideoID());
commentModified.setCommentBody(commentDTO.getCommentBody());
commentModified.setIsApproved(commentDTO.isIsApproved());
commentModified.setIsAdminComment(commentDTO.getIsAdminComment());
commentModified.setLikesCount(commentDTO.getLikesCount());
commentModified.setDislikesCount(commentDTO.getDislikesCount());
commentModified.setReplies(commentDTO.getReplies());
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/asanka/tutor/web/rest/CommentResource.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.asanka.tutor.web.rest;

import com.asanka.tutor.domain.Authority;
import com.asanka.tutor.domain.CommentReply;
import com.asanka.tutor.domain.User;
import com.asanka.tutor.repository.CustomAuditEventRepository;
Expand Down Expand Up @@ -67,6 +68,17 @@ public ResponseEntity<CommentDTO> createComment(@Valid @RequestBody CommentDTO c
if (commentDTO.getId() != null) {
throw new BadRequestAlertException("A new comment cannot already have an ID", ENTITY_NAME, "idexists");
}
Optional<User> currentUser = userService.getUserWithAuthorities();
if (!currentUser.isPresent()) {
throw new UserNotLoggedInException("The user seems not to be logged in.");
}
Authority adminAuthority = new Authority();
adminAuthority.setName(AuthoritiesConstants.ADMIN);
if (currentUser.get().getAuthorities().contains(adminAuthority)) {
commentDTO.setIsAdminComment(true);
} else {
commentDTO.setIsAdminComment(false);
}
CommentDTO result = commentService.save(commentDTO);
return ResponseEntity.created(new URI("/api/comments/" + result.getId()))
.headers(HeaderUtil.createEntityCreationAlert(applicationName, false, ENTITY_NAME, result.getId()))
Expand Down Expand Up @@ -124,6 +136,13 @@ public ResponseEntity<CommentDTO> addReplyToComment(@Valid @RequestBody CommentR
throw new UserNotLoggedInException("The user seems not to be logged in.");
}
commentReply.setCreatedBy(currentUser.get().getLogin());
Authority adminAuthority = new Authority();
adminAuthority.setName(AuthoritiesConstants.ADMIN);
if (currentUser.get().getAuthorities().contains(adminAuthority)) {
commentReply.setIsAdminReply(true);
} else {
commentReply.setIsAdminReply(false);
}
CommentDTO comment = oldComment.get();
if (comment.getReplies() != null) {
comment.getReplies().add(commentReply);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ <h3>{{comments.length}} Comments</h3>
<img class="img-fluid" [src]="profilePicURL(commentEle.createdBy)"
alt="Profile Picture">
<div class="media-body">
<h4 class="media-heading">{{commentEle.createdBy}}</h4>
<p>{{commentEle.commentBody}}</p>
<h4 class="media-heading">{{commentEle.createdBy}}<span *ngIf="commentEle.isAdminComment" class="badge badge-pill badge-primary admin_badge">Admin</span></h4>
<div [innerHTML]=sanitizedHtml(commentEle.commentBody)></div>
<ul class="list-unstyled list-inline media-detail float-md-left">
<li class="list-inline-item">
<fa-icon [icon]="'calendar'"></fa-icon>
Expand Down Expand Up @@ -138,8 +138,8 @@ <h3 class="float-md-left">New Reply</h3>
<img class="img-fluid" [src]="profilePicURL(commentReply.createdBy)"
alt="Profile Picture">
<div class="media-body">
<h4 class="media-heading">{{commentReply.createdBy}}<span *jhiHasAnyAuthority="'ROLE_ADMIN'" class="badge badge-pill badge-primary admin_badge">Admin</span></h4>
<p>{{commentReply.replyBody}}</p>
<h4 class="media-heading">{{commentReply.createdBy}}<span *ngIf="commentReply.isAdminReply" class="badge badge-pill badge-primary admin_badge">Admin</span></h4>
<div [innerHTML]=sanitizedHtml(commentReply.replyBody)></div>
<ul class="list-unstyled list-inline media-detail float-md-left">
<li class="list-inline-item">
<fa-icon [icon]="'calendar'"></fa-icon>
Expand Down
14 changes: 14 additions & 0 deletions src/main/webapp/app/entities/course/course-detail.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,18 @@ export class CourseDetailComponent implements OnInit, OnDestroy {
}
);
}

public sanitizedHtml(htmlText: string): string {
const urlRegex = new RegExp(
'(?:(?:https?|ftp|file):\\/\\/|www\\.|ftp\\.)(?:\\([-A-Z0-9+&@#\\/%=~_|$?!:,.]*\\)|[-A-Z0-9+&@#\\/%=~_|$?!:,.])*(?:\\([-A-Z0-9+&@#\\/%=~_|$?!:,.]*\\)|[A-Z0-9+&@#\\/%=~_|$])',
'igm'
);
const matches: RegExpMatchArray = htmlText.match(urlRegex);
if (matches != null) {
for (const match of matches) {
htmlText = htmlText.replace(match, `<a href=${match} rel="noopener noreferrer" target="_blank">${match}</a>`);
}
}
return htmlText;
}
}
13 changes: 1 addition & 12 deletions src/main/webapp/app/shared/model/comment-reply.model.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
export interface ICommentReply {
commentID?: string;
replyBody?: string;
isAdminReply?: boolean;
createdBy?: string;
createdDate?: Date;
likesCount?: number;
dislikesCount?: number;
approved?: boolean;
}

export class CommentReply implements ICommentReply {
constructor(
commentID?: string,
replyBody?: string,
createdBy?: string,
createdDate?: Date,
likesCount?: number,
dislikesCount?: number,
approved?: boolean
) {}
}
2 changes: 2 additions & 0 deletions src/main/webapp/app/shared/model/comment.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface IComment {
likesCount?: number;
dislikesCount?: number;
isApproved?: boolean;
isAdminComment?: boolean;
createdBy?: string;
createdDate?: Date;
lastModifiedBy?: string;
Expand All @@ -23,6 +24,7 @@ export class Comment implements IComment {
public likesCount?: number,
public dislikesCount?: number,
public isApproved?: boolean,
public isAdminComment?: boolean,
public createdBy?: string,
public createdDate?: Date,
public lastModifiedBy?: string,
Expand Down
30 changes: 25 additions & 5 deletions src/test/java/com/asanka/tutor/web/rest/CommentResourceIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import com.asanka.tutor.OpenLearnrApp;
import com.asanka.tutor.domain.Comment;
import com.asanka.tutor.domain.User;
import com.asanka.tutor.repository.CommentRepository;
import com.asanka.tutor.repository.CustomAuditEventRepository;
import com.asanka.tutor.service.CommentService;
import com.asanka.tutor.service.UserService;
import com.asanka.tutor.service.dto.CommentDTO;
import com.asanka.tutor.service.dto.UserDTO;
import com.asanka.tutor.service.mapper.CommentMapper;
import com.asanka.tutor.web.rest.errors.ExceptionTranslator;

import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockitoAnnotations;
Expand All @@ -18,6 +21,7 @@
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.validation.Validator;
Expand Down Expand Up @@ -52,6 +56,9 @@ public class CommentResourceIT {
private static final Boolean DEFAULT_IS_APPROVED = false;
private static final Boolean UPDATED_IS_APPROVED = true;

private static final Boolean DEFAULT_IS_ADMINCOMMENT = false;
private static final Boolean UPDATED_IS_ADMINCOMMENT = true;

@Autowired
private CommentRepository commentRepository;

Expand Down Expand Up @@ -107,7 +114,8 @@ public static Comment createEntity() {
.commentBody(DEFAULT_COMMENT_BODY)
.likesCount(DEFAULT_LIKES_COUNT)
.dislikesCount(DEFAULT_DISLIKES_COUNT)
.isApproved(DEFAULT_IS_APPROVED);
.isApproved(DEFAULT_IS_APPROVED)
.isAdminComment(DEFAULT_IS_ADMINCOMMENT);
return comment;
}
/**
Expand All @@ -122,7 +130,8 @@ public static Comment createUpdatedEntity() {
.commentBody(UPDATED_COMMENT_BODY)
.likesCount(UPDATED_LIKES_COUNT)
.dislikesCount(UPDATED_DISLIKES_COUNT)
.isApproved(UPDATED_IS_APPROVED);
.isApproved(UPDATED_IS_APPROVED)
.isAdminComment(UPDATED_IS_ADMINCOMMENT);
return comment;
}

Expand All @@ -133,8 +142,14 @@ public void initTest() {
}

@Test
@WithMockUser("create-comment")
public void createComment() throws Exception {
int databaseSizeBeforeCreate = commentRepository.findAll().size();
UserDTO user = new UserDTO();
user.setLogin("create-comment");
user.setEmail("[email protected]");

userService.createUser(user);

// Create the Comment
CommentDTO commentDTO = commentMapper.toDto(comment);
Expand All @@ -152,6 +167,7 @@ public void createComment() throws Exception {
assertThat(testComment.getLikesCount()).isEqualTo(DEFAULT_LIKES_COUNT);
assertThat(testComment.getDislikesCount()).isEqualTo(DEFAULT_DISLIKES_COUNT);
assertThat(testComment.isIsApproved()).isEqualTo(DEFAULT_IS_APPROVED);
assertThat(testComment.getIsAdminComment()).isEqualTo(DEFAULT_IS_ADMINCOMMENT);
}

@Test
Expand Down Expand Up @@ -242,7 +258,8 @@ public void getAllComments() throws Exception {
.andExpect(jsonPath("$.[*].commentBody").value(hasItem(DEFAULT_COMMENT_BODY)))
.andExpect(jsonPath("$.[*].likesCount").value(hasItem(DEFAULT_LIKES_COUNT)))
.andExpect(jsonPath("$.[*].dislikesCount").value(hasItem(DEFAULT_DISLIKES_COUNT)))
.andExpect(jsonPath("$.[*].isApproved").value(hasItem(DEFAULT_IS_APPROVED)));
.andExpect(jsonPath("$.[*].isApproved").value(hasItem(DEFAULT_IS_APPROVED)))
.andExpect(jsonPath("$.[*].isAdminComment").value(hasItem(DEFAULT_IS_ADMINCOMMENT)));
}

@Test
Expand All @@ -259,7 +276,8 @@ public void getComment() throws Exception {
.andExpect(jsonPath("$.commentBody").value(DEFAULT_COMMENT_BODY))
.andExpect(jsonPath("$.likesCount").value(DEFAULT_LIKES_COUNT))
.andExpect(jsonPath("$.dislikesCount").value(DEFAULT_DISLIKES_COUNT))
.andExpect(jsonPath("$.isApproved").value(DEFAULT_IS_APPROVED));
.andExpect(jsonPath("$.isApproved").value(DEFAULT_IS_APPROVED))
.andExpect(jsonPath("$.isAdminComment").value(DEFAULT_IS_ADMINCOMMENT));
}

@Test
Expand All @@ -283,7 +301,8 @@ public void updateComment() throws Exception {
.commentBody(UPDATED_COMMENT_BODY)
.likesCount(UPDATED_LIKES_COUNT)
.dislikesCount(UPDATED_DISLIKES_COUNT)
.isApproved(UPDATED_IS_APPROVED);
.isApproved(UPDATED_IS_APPROVED)
.isAdminComment(UPDATED_IS_ADMINCOMMENT);
CommentDTO commentDTO = commentMapper.toDto(updatedComment);

restCommentMockMvc.perform(put("/api/comments")
Expand All @@ -300,6 +319,7 @@ public void updateComment() throws Exception {
assertThat(testComment.getLikesCount()).isEqualTo(UPDATED_LIKES_COUNT);
assertThat(testComment.getDislikesCount()).isEqualTo(UPDATED_DISLIKES_COUNT);
assertThat(testComment.isIsApproved()).isEqualTo(UPDATED_IS_APPROVED);
assertThat(testComment.getIsAdminComment()).isEqualTo(UPDATED_IS_ADMINCOMMENT);
}

@Test
Expand Down

0 comments on commit d276596

Please sign in to comment.