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();
}
}
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();
}
}
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();
}
}
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());
}
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;
}
Aggregations