use of org.wso2.carbon.apimgt.rest.api.store.dto.RatingListDTO 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();
}
}
use of org.wso2.carbon.apimgt.rest.api.store.dto.RatingListDTO 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;
}
Aggregations