use of org.haiku.haikudepotserver.api1.model.repository.CreateRepositoryResult in project haikudepotserver by haiku.
the class RepositoryApiImpl method createRepository.
@Override
public CreateRepositoryResult createRepository(CreateRepositoryRequest createRepositoryRequest) {
Preconditions.checkNotNull(createRepositoryRequest);
final ObjectContext context = serverRuntime.newContext();
if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), null, Permission.REPOSITORY_ADD)) {
throw new AccessDeniedException("unable to add a repository");
}
if (Strings.isNullOrEmpty(createRepositoryRequest.code)) {
throw new ValidationException(new ValidationFailure(Repository.CODE.getName(), "required"));
}
// check to see if there is an existing repository with the same code; non-unique.
{
Optional<Repository> repositoryOptional = Repository.tryGetByCode(context, createRepositoryRequest.code);
if (repositoryOptional.isPresent()) {
throw new ValidationException(new ValidationFailure(Repository.CODE.getName(), "unique"));
}
}
Repository repository = context.newObject(Repository.class);
repository.setCode(createRepositoryRequest.code);
repository.setName(createRepositoryRequest.name);
repository.setInformationUrl(createRepositoryRequest.informationUrl);
context.commitChanges();
return new CreateRepositoryResult();
}
Aggregations