use of org.haiku.haikudepotserver.api1.model.userrating.CreateUserRatingRequest in project haikudepotserver by haiku.
the class UserRatingApiImpl method createUserRating.
@Override
public CreateUserRatingResult createUserRating(CreateUserRatingRequest request) {
Preconditions.checkNotNull(request);
Preconditions.checkState(!Strings.isNullOrEmpty(request.naturalLanguageCode));
Preconditions.checkState(!Strings.isNullOrEmpty(request.pkgName));
Preconditions.checkState(!Strings.isNullOrEmpty(request.pkgVersionArchitectureCode));
Preconditions.checkArgument(!Strings.isNullOrEmpty(request.repositoryCode), "the repository code should be supplied");
Preconditions.checkArgument(null == request.rating || request.rating >= UserRating.MIN_USER_RATING, "the user rating " + request.rating + " is less than the minimum allowed of " + UserRating.MIN_USER_RATING);
Preconditions.checkArgument(null == request.rating || request.rating <= UserRating.MAX_USER_RATING, "the user rating " + request.rating + " is greater than the maximum allowed of " + UserRating.MIN_USER_RATING);
if (null != request.comment) {
request.comment = Strings.emptyToNull(request.comment.trim());
}
if (Strings.isNullOrEmpty(request.comment) && null == request.userRatingStabilityCode && null == request.rating) {
throw new IllegalStateException("it is not possible to create a user rating with no meaningful rating data");
}
final ObjectContext context = serverRuntime.newContext();
Optional<Pkg> pkgOptional = Pkg.tryGetByName(context, request.pkgName);
if (pkgOptional.isEmpty()) {
throw new ObjectNotFoundException(Pkg.class.getSimpleName(), request.pkgName);
}
Architecture architecture = getArchitecture(context, request.pkgVersionArchitectureCode);
NaturalLanguage naturalLanguage = getNaturalLanguage(context, request.naturalLanguageCode);
Repository repository = getRepository(context, request.repositoryCode);
User user = User.tryGetByNickname(context, request.userNickname).orElseThrow(() -> new ObjectNotFoundException(User.class.getSimpleName(), request.userNickname));
Optional<UserRatingStability> userRatingStabilityOptional = Optional.empty();
if (null != request.userRatingStabilityCode) {
userRatingStabilityOptional = UserRatingStability.getByCode(context, request.userRatingStabilityCode);
if (userRatingStabilityOptional.isEmpty()) {
throw new ObjectNotFoundException(UserRatingStability.class.getSimpleName(), request.userRatingStabilityCode);
}
}
// check authorization
Optional<User> authenticatedUserOptional = tryObtainAuthenticatedUser(context);
if (authenticatedUserOptional.isEmpty()) {
throw new AccessDeniedException("only authenticated users are able to add user ratings");
}
if (!authenticatedUserOptional.get().getNickname().equals(user.getNickname())) {
throw new AccessDeniedException("it is not allowed to add a user rating for another user");
}
if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), pkgOptional.get(), Permission.PKG_CREATEUSERRATING)) {
throw new AccessDeniedException("unable to create user ratings for [" + pkgOptional.get() + "]");
}
// check the package version
Optional<PkgVersion> pkgVersionOptional;
switch(request.pkgVersionType) {
case LATEST:
pkgVersionOptional = pkgService.getLatestPkgVersionForPkg(context, pkgOptional.get(), repository, Collections.singletonList(architecture));
break;
case SPECIFIC:
pkgVersionOptional = PkgVersion.getForPkg(context, pkgOptional.get(), repository, architecture, new VersionCoordinates(request.pkgVersionMajor, request.pkgVersionMinor, request.pkgVersionMicro, request.pkgVersionPreRelease, request.pkgVersionRevision));
break;
default:
throw new IllegalStateException("unsupported pkg version type; " + request.pkgVersionType.name());
}
if (pkgVersionOptional.isEmpty()) {
throw new ObjectNotFoundException(PkgVersion.class.getSimpleName(), pkgOptional.get().getName() + "_" + request.pkgVersionType.name());
}
if (!pkgVersionOptional.get().getIsLatest()) {
throw new IllegalStateException("it is not possible to add a user rating to a version other than the latest version.");
}
List<UserRating> legacyUserRatings = UserRating.findByUserAndPkg(context, user, pkgOptional.get());
for (UserRating legacyUserRating : legacyUserRatings) {
if (legacyUserRating.getPkgVersion().equals(pkgVersionOptional.get())) {
throw new IllegalStateException("an existing user rating '" + legacyUserRating.getCode() + "' already exists for this package version; it is not possible to add another one");
}
}
// now create the new user rating.
UserRating userRating = context.newObject(UserRating.class);
userRating.setCode(UUID.randomUUID().toString());
userRating.setUserRatingStability(userRatingStabilityOptional.orElse(null));
userRating.setUser(user);
userRating.setComment(Strings.emptyToNull(Strings.nullToEmpty(request.comment).trim()));
userRating.setPkgVersion(pkgVersionOptional.get());
userRating.setNaturalLanguage(naturalLanguage);
userRating.setRating(request.rating);
context.commitChanges();
LOGGER.info("did create user rating for user {} on package {}", user.toString(), pkgOptional.get().toString());
return new CreateUserRatingResult(userRating.getCode());
}
use of org.haiku.haikudepotserver.api1.model.userrating.CreateUserRatingRequest in project haikudepotserver by haiku.
the class UserRatingApiIT method testCreateUserRating.
@Test
public void testCreateUserRating() {
integrationTestSupportService.createStandardTestData();
{
ObjectContext context = serverRuntime.newContext();
User user = integrationTestSupportService.createBasicUser(context, "testuser", "password");
integrationTestSupportService.agreeToUserUsageConditions(context, user);
}
setAuthenticatedUser("testuser");
CreateUserRatingRequest request = new CreateUserRatingRequest();
request.naturalLanguageCode = NaturalLanguage.CODE_SPANISH;
request.userNickname = "testuser";
request.repositoryCode = "testrepo";
request.userRatingStabilityCode = UserRatingStability.CODE_VERYUNSTABLE;
request.comment = "The supermarket has gone crazy";
request.rating = (short) 5;
request.pkgName = "pkg1";
request.pkgVersionArchitectureCode = "x86_64";
request.pkgVersionType = PkgVersionType.LATEST;
// ------------------------------------
String code = userRatingApi.createUserRating(request).code;
// ------------------------------------
{
ObjectContext context = serverRuntime.newContext();
Optional<UserRating> userRatingOptional = UserRating.tryGetByCode(context, code);
Assertions.assertThat(userRatingOptional.isPresent()).isTrue();
Assertions.assertThat(userRatingOptional.get().getActive()).isTrue();
Assertions.assertThat(userRatingOptional.get().getComment()).isEqualTo("The supermarket has gone crazy");
Assertions.assertThat(userRatingOptional.get().getNaturalLanguage().getCode()).isEqualTo(NaturalLanguage.CODE_SPANISH);
Assertions.assertThat(userRatingOptional.get().getRating()).isEqualTo((short) 5);
Assertions.assertThat(userRatingOptional.get().getUser().getNickname()).isEqualTo("testuser");
Assertions.assertThat(userRatingOptional.get().getUserRatingStability().getCode()).isEqualTo(UserRatingStability.CODE_VERYUNSTABLE);
Assertions.assertThat(userRatingOptional.get().getPkgVersion().getPkg().getName()).isEqualTo("pkg1");
Assertions.assertThat(userRatingOptional.get().getPkgVersion().getMajor()).isEqualTo("1");
Assertions.assertThat(userRatingOptional.get().getPkgVersion().getMinor()).isNull();
Assertions.assertThat(userRatingOptional.get().getPkgVersion().getMicro()).isEqualTo("2");
Assertions.assertThat(userRatingOptional.get().getPkgVersion().getPreRelease()).isNull();
Assertions.assertThat(userRatingOptional.get().getPkgVersion().getRevision()).isEqualTo(4);
}
}
use of org.haiku.haikudepotserver.api1.model.userrating.CreateUserRatingRequest in project haikudepotserver by haiku.
the class UserRatingApiImpl method createUserRating.
@Override
public ResponseEntity<CreateUserRatingResponseEnvelope> createUserRating(@Valid CreateUserRatingRequestEnvelope createUserRatingRequestEnvelope) {
CreateUserRatingRequest requestV1 = new CreateUserRatingRequest();
requestV1.repositoryCode = createUserRatingRequestEnvelope.getRepositoryCode();
requestV1.naturalLanguageCode = createUserRatingRequestEnvelope.getNaturalLanguageCode();
requestV1.userNickname = createUserRatingRequestEnvelope.getUserNickname();
requestV1.userRatingStabilityCode = createUserRatingRequestEnvelope.getUserRatingStabilityCode();
requestV1.comment = createUserRatingRequestEnvelope.getComment();
requestV1.rating = Optional.ofNullable(createUserRatingRequestEnvelope.getRating()).map(Number::shortValue).orElse(null);
requestV1.pkgName = createUserRatingRequestEnvelope.getPkgName();
requestV1.pkgVersionArchitectureCode = createUserRatingRequestEnvelope.getPkgVersionArchitectureCode();
requestV1.pkgVersionMajor = createUserRatingRequestEnvelope.getPkgVersionMajor();
requestV1.pkgVersionMinor = createUserRatingRequestEnvelope.getPkgVersionMinor();
requestV1.pkgVersionMicro = createUserRatingRequestEnvelope.getPkgVersionMicro();
requestV1.pkgVersionPreRelease = createUserRatingRequestEnvelope.getPkgVersionPreRelease();
requestV1.pkgVersionRevision = createUserRatingRequestEnvelope.getPkgVersionRevision();
requestV1.pkgVersionType = Optional.ofNullable(createUserRatingRequestEnvelope.getPkgVersionType()).map(pvt -> PkgVersionType.valueOf(pvt.name())).orElse(null);
org.haiku.haikudepotserver.api1.model.userrating.CreateUserRatingResult resultV1 = userRatingApiV1.createUserRating(requestV1);
return ResponseEntity.ok(new CreateUserRatingResponseEnvelope().result(new CreateUserRatingResult().code(resultV1.code)));
}
Aggregations