Search in sources :

Example 1 with CommentListDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.CommentListDTO in project carbon-apimgt by wso2.

the class CommentMappingUtil method fromCommentListToDTO.

/**
 * Wraps a List of Comments to a CommentListDTO
 *
 * @param commentList list of comments
 * @param limit maximum comments to return
 * @param offset  starting position of the pagination
 * @return CommentListDTO
 */
public static CommentListDTO fromCommentListToDTO(List<Comment> commentList, int limit, int offset) {
    CommentListDTO commentListDTO = new CommentListDTO();
    List<CommentDTO> listOfCommentDTOs = new ArrayList<>();
    commentListDTO.setCount(commentList.size());
    int start = offset < commentList.size() && offset >= 0 ? offset : Integer.MAX_VALUE;
    int end = offset + limit - 1 <= commentList.size() - 1 ? offset + limit - 1 : commentList.size() - 1;
    for (int i = start; i <= end; i++) {
        listOfCommentDTOs.add(fromCommentToDTO(commentList.get(i)));
    }
    commentListDTO.setList(listOfCommentDTOs);
    return commentListDTO;
}
Also used : ArrayList(java.util.ArrayList) CommentDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO) CommentListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CommentListDTO)

Example 2 with CommentListDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.CommentListDTO in project carbon-apimgt by wso2.

the class CommentMappingUtilTestCase method testFromCommentListToDTO.

@Test
public void testFromCommentListToDTO() {
    Comment comment1 = new Comment();
    comment1.setUuid(UUID.randomUUID().toString());
    comment1.setCommentedUser("commentedUser1");
    comment1.setCommentText("this is a comment 1");
    comment1.setCreatedUser("createdUser1");
    comment1.setUpdatedUser("updatedUser1");
    comment1.setCreatedTime(LocalDateTime.now().minusHours(1));
    comment1.setUpdatedTime(LocalDateTime.now());
    Comment comment2 = new Comment();
    comment2.setUuid(UUID.randomUUID().toString());
    comment2.setCommentedUser("commentedUser2");
    comment2.setCommentText("this is a comment 2");
    comment2.setCreatedUser("createdUser2");
    comment2.setUpdatedUser("updatedUser2");
    comment2.setCreatedTime(LocalDateTime.now().minusHours(1));
    comment2.setUpdatedTime(LocalDateTime.now());
    List<Comment> commentList = new ArrayList<>();
    commentList.add(comment1);
    commentList.add(comment2);
    CommentListDTO commentListDTO = commentMappingUtil.fromCommentListToDTO(commentList, 10, 0);
    Assert.assertEquals(commentListDTO.getList().get(0).getUsername().toString(), "commentedUser1");
}
Also used : Comment(org.wso2.carbon.apimgt.core.models.Comment) ArrayList(java.util.ArrayList) CommentListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CommentListDTO) Test(org.junit.Test)

Example 3 with CommentListDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.CommentListDTO in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdCommentsGet.

/**
 * Retrives A list of comments for a given API ID
 *
 * @param apiId   API ID
 * @param limit   Max number of comments to return
 * @param offset  Starting point of pagination
 * @param request msf4j request object
 * @return CommentListDTO object
 * @throws NotFoundException if this method is not defined in ApisApiServiceImpl
 */
@Override
public Response apisApiIdCommentsGet(String apiId, Integer limit, Integer offset, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        List<Comment> commentList = apiStore.getCommentsForApi(apiId);
        CommentListDTO commentListDTO = CommentMappingUtil.fromCommentListToDTO(commentList, limit, offset);
        return Response.ok().entity(commentListDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving comments for api : " + apiId;
        Map<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : Comment(org.wso2.carbon.apimgt.core.models.Comment) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) CommentListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CommentListDTO) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Aggregations

CommentListDTO (org.wso2.carbon.apimgt.rest.api.store.dto.CommentListDTO)3 ArrayList (java.util.ArrayList)2 Comment (org.wso2.carbon.apimgt.core.models.Comment)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.junit.Test)1 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)1 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)1 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)1 CommentDTO (org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO)1