Skip to content
Snippets Groups Projects
Commit b25a263e authored by Piotr Gawron's avatar Piotr Gawron
Browse files

api for removing comments

parent 1bd63549
No related branches found
No related tags found
2 merge requests!115Resolve "admin panel should use API",!114Resolve "admin panel should use API"
......@@ -35,6 +35,15 @@ public class CommentController extends BaseController {
return commentController.getCommentList(token, projectId, columns, "", "", removed);
}
@RequestMapping(value = "/projects/{projectId}/comments/{commentId}/", method = { RequestMethod.DELETE }, produces = { MediaType.APPLICATION_JSON_VALUE })
public Map<String, Object> removeComment(//
@CookieValue(value = Configuration.AUTH_TOKEN) String token, //
@PathVariable(value = "projectId") String projectId, //
@PathVariable(value = "commentId") String commentId //
) throws SecurityException, QueryException {
return commentController.removeComment(token, projectId, commentId);
}
@RequestMapping(value = "/projects/{projectId}/comments/models/{modelId}/bioEntities/reactions/{reactionId}", method = { RequestMethod.GET },
produces = { MediaType.APPLICATION_JSON_VALUE })
public List<Map<String, Object>> getCommentsByReaction(//
......
......@@ -51,7 +51,7 @@ public class CommentRestImpl extends BaseRestImpl {
AuthenticationToken authenticationToken = getUserService().getToken(token);
Project project = getProjectService().getProjectByProjectId(projectId, authenticationToken);
if (project == null) {
throw new QueryException("Project with given id doesn't exist");
throw new ObjectNotFoundException("Project with given id doesn't exist");
}
boolean isAdmin = getUserService().userHasPrivilege(authenticationToken, PrivilegeType.EDIT_COMMENTS_PROJECT, project);
Set<String> columnsSet = createCommentColumnSet(columns, isAdmin);
......@@ -323,4 +323,19 @@ public class CommentRestImpl extends BaseRestImpl {
return preparedComment(comment, createCommentColumnSet("", isAdmin), isAdmin);
}
public Map<String, Object> removeComment(String token, String projectId, String commentId) throws SecurityException, QueryException {
AuthenticationToken authenticationToken = getUserService().getToken(token);
Project project = getProjectService().getProjectByProjectId(projectId, authenticationToken);
if (project == null) {
throw new ObjectNotFoundException("Project with given id doesn't exist");
}
Comment comment = commentService.getCommentById(commentId);
if (comment == null || comment.getModelData().getProject().getId() != project.getId()) {
throw new ObjectNotFoundException("Comment with given id doesn't exist");
}
commentService.deleteComment(comment, authenticationToken);
return okStatus();
}
}
......@@ -422,4 +422,26 @@ public class CommentService implements ICommentService {
return comments;
}
@Override
public void deleteComment(Comment comment, AuthenticationToken token) throws UserAccessException {
Project project = comment.getModelData().getProject();
boolean editComments = userService.userHasPrivilege(token, PrivilegeType.EDIT_COMMENTS_PROJECT, project);
if (editComments || userService.getUserByToken(token).equals(comment.getUser())) {
comment.setDeleted(true);
commentDao.update(comment);
} else {
throw new UserAccessException("You have no privileges to remove the comment");
}
}
@Override
public Comment getCommentById(String commentId) {
int id = -1;
try {
id = Integer.parseInt(commentId);
} catch (NumberFormatException e) {
}
return commentDao.getById(id);
}
}
......@@ -59,6 +59,8 @@ public interface ICommentService {
* why user wants to remove the comment
*/
void deleteComment(User loggedUser, String commentId, String reason);
void deleteComment(Comment comment, AuthenticationToken token) throws UserAccessException;
/**
* Method returns all comments for a given map.
......@@ -115,4 +117,6 @@ public interface ICommentService {
*/
List<ElementIdentifierDetails> getElementInformationForResult(ElementIdentifier element, Model model);
Comment getCommentById(String commentId);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment