com.minlessika.membership.integration.RestSequences Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of membership-integration Show documentation
Show all versions of membership-integration Show documentation
It's a library to help developers to integration membership services to another project.
package com.minlessika.membership.integration;
import com.google.common.collect.ImmutableMap;
import com.minlessika.http.client.RestClient;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import javax.json.Json;
public final class RestSequences implements Sequences {
private final Organization organization;
private final RestClient client;
public RestSequences(final RestClient client, final Organization organization) {
this.organization = organization;
this.client = client;
}
@Override
public Sequence get(final UUID uid) {
return new RestSequence(
this.client,
this.client.get(
"/api/organization/sequence",
ImmutableMap.of(
"id", uid,
"org", organization.uid()
)
).asJsonObject(),
this.organization
);
}
@Override
public Sequence get(final String code) {
return new RestSequence(
this.client,
this.client.get(
"/api/organization/sequence/by-code",
ImmutableMap.of(
"org", this.organization.uid(),
"code", code
)
).asJsonObject(),
this.organization
);
}
@Override
public Iterable iterate() {
final List items = new LinkedList<>();
this.client.get(
"/api/organization/sequences",
ImmutableMap.of("org", this.organization.uid())
).asJsonArray().forEach(
json -> items.add(
new RestSequence(this.client, json.asJsonObject(), this.organization)
)
);
return items;
}
@Override
public Sequence add(
final String code, final String name, final String prefix, final String suffix,
final int size, final int step, final long nextNumber
) {
return this.get(
UUID.fromString(
this.client.post(
"/api/organization/sequence",
ImmutableMap.of(
"org", this.organization.uid()
),
Json.createObjectBuilder()
.add("code", code)
.add("name", name)
.add("prefix", prefix)
.add("suffix", suffix)
.add("size", size)
.add("step", step)
.add("nextNumber", nextNumber)
.build()
).get().asJsonObject().getString("id")
)
);
}
@Override
public void delete(final Sequence sequence) {
this.client.delete(
"/api/organization/sequence",
ImmutableMap.of(
"org", this.organization.uid(),
"id", sequence.uid()
)
);
}
@Override
public boolean has(final String code) {
return this.client.get(
"/api/organization/sequence/has/by-code",
ImmutableMap.of(
"org", this.organization.uid(),
"code", code
)
).asJsonObject().getBoolean("value");
}
}