Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paginated services #301

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public String getPath() {
@Override
public void addEndpoints() {
post("/", this::create, ActorRoles.anyAdmin());
get("/", this::getByDomain, ActorRoles.adminClient());

get("/:id", this::getById, ActorRoles.adminClient());
get("/externalId/:id", this::getByExternalId, ActorRoles.adminClient());
Expand All @@ -26,6 +27,7 @@ public void addEndpoints() {
}

public abstract void create(final Context context);
public abstract void getByDomain(final Context context);

public abstract void getById(final Context context);

Expand Down
47 changes: 41 additions & 6 deletions api/src/main/resources/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,19 @@ paths:
description: Bad request
$ref: "#/components/responses/ErrorResponse"

get:
operationId: getClientsByDomain
description: Get all clients for a domain
tags:
- Clients
parameters:
- $ref: "#/components/parameters/DomainParameter"
- $ref: "#/components/parameters/CursorQueryParameter"
responses:
200:
description: Success
$ref: "#/components/responses/ClientsArrayResponse"

/domains/{domain}/clients/{id}:
get:
operationId: getClientById
Expand Down Expand Up @@ -817,10 +830,11 @@ paths:
- Roles
parameters:
- $ref: "#/components/parameters/DomainParameter"
- $ref: "#/components/parameters/CursorQueryParameter"
responses:
201:
description: Success
$ref: "#/components/responses/domains/{domain}/rolesArrayResponse"
$ref: "#/components/responses/RolesArrayResponse"
400:
description: Bad request
$ref: "#/components/responses/ErrorResponse"
Expand Down Expand Up @@ -903,10 +917,11 @@ paths:
- Permissions
parameters:
- $ref: "#/components/parameters/DomainParameter"
- $ref: "#/components/parameters/CursorQueryParameter"
responses:
200:
description: Success
$ref: "#/components/responses/domains/{domain}/permissionsArrayResponse"
$ref: "#/components/responses/PermissionsArrayResponse"

/domains/{domain}/permissions/group/{group}:
get:
Expand All @@ -917,10 +932,11 @@ paths:
parameters:
- $ref: "#/components/parameters/DomainParameter"
- $ref: "#/components/parameters/GroupParameter"
- $ref: "#/components/parameters/CursorQueryParameter"
responses:
200:
description: Success
$ref: "#/components/responses/domains/{domain}/permissionsArrayResponse"
$ref: "#/components/responses/PermissionsArrayResponse"

# ----------------- auth -----------------
/domains/{domain}/auth/exchange:
Expand Down Expand Up @@ -1254,6 +1270,13 @@ components:
schema:
type: string

CursorQueryParameter:
name: cursor
in: query
required: false
schema:
type: string

responses:
ErrorResponse:
description: _
Expand Down Expand Up @@ -1290,6 +1313,15 @@ components:
schema:
$ref: "#/components/schemas/Client"

ClientsArrayResponse:
description: _
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/Client"

ApiKeyResponse:
description: _
content:
Expand Down Expand Up @@ -1560,15 +1592,18 @@ components:
forClient:
type: boolean
createdAt:
type: date-time
type: string
format: date-time
expiresAt:
type: date-time
type: string
format: date-time

ExchangeAttempt:
type: object
properties:
createdAt:
type: date-time
type: string
format: date-time
entityId:
type: string
fromExchange:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
)
@NamedQuery(
name = "apps.getByAccountId",
query = "SELECT app FROM AppDO app WHERE app.parentAccountId = :parentAccountId AND app.deleted = false"
query = "SELECT app FROM AppDO app WHERE app.parentAccountId = :parentAccountId " +
"AND app.deleted = false AND app.id > :cursor " +
"ORDER BY app.id "
)
public class AppDO extends AbstractDO {
private String externalId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,21 @@
)
@NamedQuery(
name = "clients.getByAccountId",
query = "SELECT client FROM ClientDO client WHERE client.accountId = :parentAccountId AND client.deleted = false"
query = "SELECT client FROM ClientDO client WHERE client.accountId = :parentAccountId " +
"AND client.deleted = false AND client.id > :cursor " +
"ORDER BY client.id "
)
@NamedQuery(
name = "clients.getByClientType",
query = "SELECT client FROM ClientDO client WHERE client.clientType = :clientType AND client.deleted = false"
query = "SELECT client FROM ClientDO client WHERE client.clientType = :clientType " +
"AND client.deleted = false AND client.id > :cursor " +
"ORDER BY client.id "
)
@NamedQuery(
name = "clients.getByDomain",
query = "SELECT client FROM ClientDO client WHERE client.domain = :domain AND client.deleted = false"
query = "SELECT client FROM ClientDO client WHERE client.domain = :domain " +
"AND client.deleted = false AND client.id > :cursor " +
"ORDER BY client.id "
)
public class ClientDO extends AbstractDO {
private String externalId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
@NamedQuery(
name = "permissions.getAll",
query = "SELECT permission FROM PermissionDO permission " +
"WHERE permission.deleted = false AND permission.domain = :domain"
"WHERE permission.deleted = false " +
"AND permission.domain = :domain AND permission.id > :cursor " +
"ORDER BY permission.id"
)
@NamedQuery(
name = "permissions.getByGroupAndName",
Expand All @@ -34,7 +36,9 @@
@NamedQuery(
name = "permissions.getByGroup",
query = "SELECT permission FROM PermissionDO permission " +
"WHERE permission.group = :group AND permission.domain = :domain AND permission.deleted = false"
"WHERE permission.group = :group AND permission.domain = :domain " +
"AND permission.deleted = false AND permission.id > :cursor " +
"ORDER BY permission.id"
)
public class PermissionDO extends AbstractDO {
@Column(name = "\"group\"")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
)
@NamedQuery(
name = "roles.getAll",
query = "SELECT role FROM RoleDO role WHERE role.deleted = false AND role.domain = :domain"
query = "SELECT role FROM RoleDO role WHERE role.deleted = false " +
"AND role.domain = :domain AND role.id > :cursor " +
"ORDER BY role.id "
)
@NamedQuery(
name = "roles.getByName",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ void getByExternalId() {
@Test
void getByParentAccountId() {
final TypedQuery<AppDO> query = entityManager.createNamedQuery("apps.getByAccountId", AppDO.class)
.setParameter("parentAccountId", createdApp.getParentAccountId());
.setParameter("parentAccountId", createdApp.getParentAccountId())
.setParameter("cursor", 0L);

final List<AppDO> retrieved = query.getResultList();
Assertions.assertThat(retrieved).containsExactly(createdApp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ void getById() {
@Test
void getAll() {
final TypedQuery<PermissionDO> query = entityManager.createNamedQuery("permissions.getAll", PermissionDO.class)
.setParameter("domain", first.getDomain());
.setParameter("domain", first.getDomain())
.setParameter("cursor", 0L);

final List<PermissionDO> retrieved = query.getResultList();
Assertions.assertThat(retrieved).containsExactly(first, second);
Expand All @@ -89,7 +90,8 @@ void getByGroupAndName() {
void getByGroup() {
final TypedQuery<PermissionDO> query = entityManager.createNamedQuery("permissions.getByGroup", PermissionDO.class)
.setParameter("group", first.getGroup())
.setParameter("domain", first.getDomain());
.setParameter("domain", first.getDomain())
.setParameter("cursor", 0L);

final List<PermissionDO> retrieved = query.getResultList();
Assertions.assertThat(retrieved).containsExactly(first, second);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ void getById() {
@Test
void getAll() {
final TypedQuery<RoleDO> query = entityManager.createNamedQuery("roles.getAll", RoleDO.class)
.setParameter("domain", first.getDomain());
.setParameter("domain", first.getDomain())
.setParameter("cursor", 0L);

final List<RoleDO> retrieved = query.getResultList();
Assertions.assertThat(retrieved).containsExactly(first, second);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

public interface ApplicationsRepository extends Repository<AppDO> {
CompletableFuture<Optional<AppDO>> getByExternalId(String externalId);
CompletableFuture<List<AppDO>> getAllForAccount(long accountId);
CompletableFuture<List<AppDO>> getAllForAccount(long accountId, Page page);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public interface ClientsRepository extends Repository<ClientDO> {
CompletableFuture<Optional<ClientDO>> getByExternalId(String externalId);
CompletableFuture<List<ClientDO>> getAllForAccount(long accountId);
CompletableFuture<List<ClientDO>> getByType(String externalId);
CompletableFuture<List<ClientDO>> getByDomain(String externalId);
CompletableFuture<List<ClientDO>> getAllForAccount(long accountId, Page page);
CompletableFuture<List<ClientDO>> getByType(String type, Page page);
CompletableFuture<List<ClientDO>> getByDomain(String domain, Page page);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.nexblocks.authguard.dal.persistence;

import java.util.Objects;

public class Page {
private final long cursor;
private final int count;

private Page(long cursor, int count) {
this.cursor = cursor;
this.count = count;
}

public static Page of(Long cursor, int count) {
return new Page(cursor != null ? cursor : 0, count);
}

public long getCursor() {
return cursor;
}

public int getCount() {
return count;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Page page = (Page) o;
return cursor == page.cursor && count == page.count;
}

@Override
public int hashCode() {
return Objects.hash(cursor, count);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

public interface PermissionsRepository extends ImmutableRecordRepository<PermissionDO> {
CompletableFuture<Optional<PermissionDO>> search(String group, String name, String domain);
CompletableFuture<Collection<PermissionDO>> getAll(String domain);
CompletableFuture<Collection<PermissionDO>> getAllForGroup(String group, String domain);
CompletableFuture<Collection<PermissionDO>> getAll(String domain, Page page);
CompletableFuture<Collection<PermissionDO>> getAllForGroup(String group, String domain, Page page);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.concurrent.CompletableFuture;

public interface RolesRepository extends ImmutableRecordRepository<RoleDO> {
CompletableFuture<Collection<RoleDO>> getAll(String domain);
CompletableFuture<Collection<RoleDO>> getAll(String domain, Page page);
CompletableFuture<Optional<RoleDO>> getByName(String name, String domain);
CompletableFuture<Collection<RoleDO>> getMultiple(Collection<String> rolesNames, String domain);
}
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ public void getApps(final Context context) {
throw new RequestValidationException(Collections.singletonList(new Violation("id", ViolationType.INVALID_VALUE)));
}

CompletableFuture<List<AppDTO>> apps = applicationsService.getByAccountId(accountId.get(), Domain.fromContext(context))
Long cursor = context.queryParam("cursor", Long.class).getOrNull();

CompletableFuture<List<AppDTO>> apps = applicationsService.getByAccountId(accountId.get(), Domain.fromContext(context), cursor)
.thenApply(list -> list.stream()
.map(restMapper::toDTO)
.collect(Collectors.toList()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ public void create(final Context context) {
context.status(201).json(created);
}

@Override
public void getByDomain(Context context) {
Long cursor = context.queryParam("cursor", Long.class).getOrNull();

CompletableFuture<List<ClientDTO>> clients = clientsService.getByDomain(Domain.fromContext(context), cursor)
.thenApply(list -> list.stream().map(restMapper::toDTO).collect(Collectors.toList()));

context.json(clients);
}

public void getById(final Context context) {
Validator<Long> clientId = context.pathParam("id", Long.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ public void deleteById(final Context context) {
public void getByGroup(final Context context) {
String group = context.pathParam("group");
String domain = context.pathParam("domain");
Long cursor = context.queryParam("cursor", Long.class).getOrNull();

CompletableFuture<List<PermissionDTO>> permissions = permissionsService.getAllForGroup(group, domain)
CompletableFuture<List<PermissionDTO>> permissions = permissionsService.getAllForGroup(group, domain, cursor)
.thenApply(list -> list.stream()
.map(restMapper::toDTO)
.collect(Collectors.toList()));
Expand All @@ -89,8 +90,9 @@ public void getByGroup(final Context context) {

public void getAll(final Context context) {
String domain = context.pathParam("domain");
Long cursor = context.queryParam("cursor", Long.class).getOrNull();

CompletableFuture<List<PermissionDTO>> permissions = permissionsService.getAll(domain)
CompletableFuture<List<PermissionDTO>> permissions = permissionsService.getAll(domain, cursor)
.thenApply(list -> list.stream()
.map(restMapper::toDTO)
.collect(Collectors.toList()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ public void getByName(final Context context) {
@Override
public void getAll(final Context context) {
String domain = context.pathParam("domain");
Long cursor = context.queryParam("cursor", Long.class).getOrNull();

CompletableFuture<List<RoleDTO>> roles = rolesService.getAll(domain)
CompletableFuture<List<RoleDTO>> roles = rolesService.getAll(domain, cursor)
.thenApply(list -> list.stream()
.map(restMapper::toDTO)
.collect(Collectors.toList()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ public interface ApplicationsService extends IdempotentCrudService<AppBO> {
CompletableFuture<Optional<AppBO>> getByExternalId(long externalId, String domain);
CompletableFuture<AppBO> activate(long id, String domain);
CompletableFuture<AppBO> deactivate(long id, String domain);
CompletableFuture<List<AppBO>> getByAccountId(long accountId, String domain);
CompletableFuture<List<AppBO>> getByAccountId(long accountId, String domain, Long cursor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public interface ClientsService extends IdempotentCrudService<ClientBO> {
CompletableFuture<Optional<ClientBO>> getByExternalId(String externalId, String domain);
CompletableFuture<ClientBO> activate(long id, String domain);
CompletableFuture<ClientBO> deactivate(long id, String domain);
CompletableFuture<List<ClientBO>> getByAccountId(long accountId, String domain);
CompletableFuture<List<ClientBO>> getByAccountId(long accountId, String domain, Long cursor);
CompletableFuture<List<ClientBO>> getByDomain(String domain, Long cursor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

public interface PermissionsService extends CrudService<PermissionBO> {
List<PermissionBO> validate(List<PermissionBO> permissions, String domain);
CompletableFuture<List<PermissionBO>> getAll(String domain);
CompletableFuture<List<PermissionBO>> getAllForGroup(String group, String domain);
CompletableFuture<List<PermissionBO>> getAll(String domain, Long cursor);
CompletableFuture<List<PermissionBO>> getAllForGroup(String group, String domain, Long cursor);
}