Search in sources :

Example 1 with RatingDTO

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

the class ApisApiServiceImpl method apisApiIdRatingsRatingIdGet.

@Override
public Response apisApiIdRatingsRatingIdGet(String apiId, String ratingId, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        Rating rating = apiStore.getRatingByUUID(apiId, ratingId);
        RatingDTO ratingDTO = RatingMappingUtil.fromRatingToDTO(rating);
        return Response.ok().entity(ratingDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving rating for given API " + apiId;
        Map<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        paramList.put(APIMgtConstants.ExceptionsConstants.RATING_ID, ratingId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) Rating(org.wso2.carbon.apimgt.core.models.Rating) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) RatingDTO(org.wso2.carbon.apimgt.rest.api.store.dto.RatingDTO) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 2 with RatingDTO

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

the class ApisApiServiceImpl method apisApiIdRatingsGet.

/**
 * Retrieves a list of ratings for given API ID
 *
 * @param apiId   API ID
 * @param limit   response limit
 * @param offset  response offset
 * @param request msf4j request object
 * @return List of Ratings for API
 * @throws NotFoundException if failed to find method implementation
 */
@Override
public Response apisApiIdRatingsGet(String apiId, Integer limit, Integer offset, Request request) throws NotFoundException {
    double avgRating;
    String username = RestApiUtil.getLoggedInUsername(request);
    int userRatingValue = 0;
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        Rating userRating = apiStore.getRatingForApiFromUser(apiId, username);
        if (userRating != null) {
            userRatingValue = userRating.getRating();
        }
        avgRating = apiStore.getAvgRating(apiId);
        List<Rating> ratingListForApi = apiStore.getRatingsListForApi(apiId);
        List<RatingDTO> ratingDTOList = RatingMappingUtil.fromRatingListToDTOList(ratingListForApi);
        RatingListDTO ratingListDTO = RatingMappingUtil.fromRatingDTOListToRatingListDTO(avgRating, userRatingValue, offset, limit, ratingDTOList);
        return Response.ok().entity(ratingListDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving rating for given 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 : Rating(org.wso2.carbon.apimgt.core.models.Rating) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) RatingDTO(org.wso2.carbon.apimgt.rest.api.store.dto.RatingDTO) RatingListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.RatingListDTO) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 3 with RatingDTO

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

the class ApisApiServiceImpl method apisApiIdUserRatingPut.

/**
 * Add or update raating to an API
 *
 * @param apiId   APIID
 * @param body    RatingDTO object
 * @param request msf4j request
 * @return 201 response if successful
 * @throws NotFoundException if failed to find method implementation
 */
@Override
public Response apisApiIdUserRatingPut(String apiId, RatingDTO body, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    String ratingId;
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        Rating ratingFromPayload = RatingMappingUtil.fromDTOToRating(username, apiId, body);
        Rating existingRating = apiStore.getRatingForApiFromUser(apiId, username);
        if (existingRating != null) {
            String existingRatingUUID = existingRating.getUuid();
            apiStore.updateRating(apiId, existingRatingUUID, ratingFromPayload);
            Rating updatedRating = apiStore.getRatingByUUID(apiId, existingRatingUUID);
            RatingDTO updatedRatingDTO = RatingMappingUtil.fromRatingToDTO(updatedRating);
            return Response.ok().entity(updatedRatingDTO).build();
        } else {
            ratingId = apiStore.addRating(apiId, ratingFromPayload);
            Rating createdRating = apiStore.getRatingByUUID(apiId, ratingId);
            RatingDTO createdRatingDTO = RatingMappingUtil.fromRatingToDTO(createdRating);
            URI location = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + RestApiConstants.SUBRESOURCE_PATH_RATINGS + "/" + ratingId);
            return Response.status(Response.Status.CREATED).header(RestApiConstants.LOCATION_HEADER, location).entity(createdRatingDTO).build();
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while adding/updating rating for user " + username + " for given 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();
    } catch (URISyntaxException e) {
        String errorMessage = "Error while adding location header in response for comment";
        ErrorHandler errorHandler = ExceptionCodes.LOCATION_HEADER_INCORRECT;
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler);
        log.error(errorMessage, e);
        return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) Rating(org.wso2.carbon.apimgt.core.models.Rating) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) RatingDTO(org.wso2.carbon.apimgt.rest.api.store.dto.RatingDTO) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 4 with RatingDTO

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

the class ApisApiServiceImplTestCase method testApisApiIdUserRatingPutErrorCase.

@Test
public void testApisApiIdUserRatingPutErrorCase() throws APIManagementException, NotFoundException {
    printTestMethodName();
    String apiId = UUID.randomUUID().toString();
    String rateId = UUID.randomUUID().toString();
    ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
    APIStore apiStore = Mockito.mock(APIStoreImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
    Request request = getRequest();
    PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
    Mockito.doThrow(new APIRatingException("Error Occured", ExceptionCodes.RATING_NOT_FOUND)).when(apiStore).getRatingForApiFromUser(apiId, USER);
    Rating rating = new Rating();
    rating.setApiId(apiId);
    rating.setRating(5);
    rating.setUsername(USER);
    rating.setUuid(rateId);
    rating.setCreatedUser(USER);
    rating.setCreatedTime(LocalDateTime.now().minusHours(2));
    rating.setLastUpdatedUser(USER);
    rating.setLastUpdatedTime(LocalDateTime.now());
    RatingDTO ratingDTO = RatingMappingUtil.fromRatingToDTO(rating);
    Response response = apisApiService.apisApiIdUserRatingPut(apiId, ratingDTO, request);
    Assert.assertEquals(404, response.getStatus());
}
Also used : APIRatingException(org.wso2.carbon.apimgt.core.exception.APIRatingException) Response(javax.ws.rs.core.Response) Rating(org.wso2.carbon.apimgt.core.models.Rating) Request(org.wso2.msf4j.Request) RatingDTO(org.wso2.carbon.apimgt.rest.api.store.dto.RatingDTO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with RatingDTO

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

the class RatingMappingUtil method fromRatingDTOListToRatingListDTO.

/**
 * Constructs a RatingListDTO from a list of RatingDTO objects and other information
 *
 * @param avgRating     Average Rating of the API
 * @param userRating    User Rating for the API
 * @param offset        starting index
 * @param limit         maximum number of ratings to return
 * @param ratingList    List of RatingDTO Objects available for the API
 * @return a new RatingLIstDTO object
 */
public static RatingListDTO fromRatingDTOListToRatingListDTO(double avgRating, double userRating, Integer offset, Integer limit, List<RatingDTO> ratingList) {
    RatingListDTO ratingListDTO = new RatingListDTO();
    List<RatingDTO> ratingDTOs = ratingListDTO.getList();
    DecimalFormat decimalFormat = new DecimalFormat("#.#");
    ratingListDTO.setAvgRating(String.valueOf(decimalFormat.format(avgRating)));
    ratingListDTO.setUserRating(String.valueOf(decimalFormat.format(userRating)));
    if (ratingList == null) {
        ratingList = new ArrayList<>();
        ratingListDTO.setList(ratingList);
    }
    // add the required range of objects to be returned
    int start = offset < ratingList.size() && offset >= 0 ? offset : Integer.MAX_VALUE;
    int end = offset + limit - 1 <= ratingList.size() - 1 ? offset + limit - 1 : ratingList.size() - 1;
    for (int i = start; i <= end; i++) {
        ratingDTOs.add(ratingList.get(i));
    }
    ratingListDTO.setCount(ratingDTOs.size());
    return ratingListDTO;
}
Also used : DecimalFormat(java.text.DecimalFormat) RatingDTO(org.wso2.carbon.apimgt.rest.api.store.dto.RatingDTO) RatingListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.RatingListDTO)

Aggregations

RatingDTO (org.wso2.carbon.apimgt.rest.api.store.dto.RatingDTO)7 Rating (org.wso2.carbon.apimgt.core.models.Rating)6 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)5 HashMap (java.util.HashMap)3 Map (java.util.Map)3 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)3 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)3 Response (javax.ws.rs.core.Response)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 RatingListDTO (org.wso2.carbon.apimgt.rest.api.store.dto.RatingListDTO)2 Request (org.wso2.msf4j.Request)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 DecimalFormat (java.text.DecimalFormat)1 APIRatingException (org.wso2.carbon.apimgt.core.exception.APIRatingException)1 ErrorHandler (org.wso2.carbon.apimgt.core.exception.ErrorHandler)1