Skip to content

Commit

Permalink
chore(sandside): #162 refactor QueryContext into record
Browse files Browse the repository at this point in the history
  • Loading branch information
Marthym committed Apr 21, 2024
1 parent c902822 commit cc3dc40
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@
import fr.ght1pc9kc.juery.api.PageRequest;
import fr.ght1pc9kc.juery.api.Pagination;
import lombok.Builder;
import lombok.Value;
import lombok.With;

import java.util.List;

import static fr.ght1pc9kc.baywatch.common.api.model.EntitiesProperties.ID;

@Value
@Builder
public final class QueryContext {
@Builder.Default
public final Pagination pagination = Pagination.ALL;
public final Criteria filter;
@With
public final String userId;

public final List<String> teamMates;
public record QueryContext(
Pagination pagination,
Criteria filter,
@With String userId,
List<String> teamMates
) {
public QueryContext {
pagination = Pagination.ALL;
}

public static QueryContext from(PageRequest pr) {
return new QueryContext(pr.pagination(), pr.filter(), null, List.of());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public Flux<Entity<ScrapingError>> persist(Collection<Entity<ScrapingError>> err

@Override
public Flux<Entity<ScrapingError>> list(QueryContext query) {
Condition conditions = query.filter.accept(JOOQ_CONDITION_VISITOR);
Condition conditions = query.filter().accept(JOOQ_CONDITION_VISITOR);
SelectQuery<FeedsErrorsRecord> select = dsl.selectQuery(FEEDS_ERRORS);
select.addConditions(conditions);

Expand All @@ -82,7 +82,7 @@ public Flux<Entity<ScrapingError>> list(QueryContext query) {

@Override
public Mono<Void> delete(QueryContext query) {
Condition conditions = query.filter.accept(JOOQ_CONDITION_VISITOR);
Condition conditions = query.filter().accept(JOOQ_CONDITION_VISITOR);

DeleteQuery<FeedsErrorsRecord> deleteQuery = dsl.deleteQuery(FEEDS_ERRORS);
deleteQuery.addConditions(conditions);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.ght1pc9kc.baywatch.security.infra.persistence;

import fr.ght1pc9kc.baywatch.common.domain.QueryContext;
import fr.ght1pc9kc.baywatch.common.infra.DatabaseQualifier;
import fr.ght1pc9kc.baywatch.common.infra.mappers.PropertiesMappers;
import fr.ght1pc9kc.baywatch.dsl.tables.records.UsersRecord;
Expand All @@ -9,7 +10,6 @@
import fr.ght1pc9kc.baywatch.security.domain.exceptions.ConstraintViolationPersistenceException;
import fr.ght1pc9kc.baywatch.security.domain.ports.UserPersistencePort;
import fr.ght1pc9kc.baywatch.security.infra.adapters.UserMapper;
import fr.ght1pc9kc.baywatch.common.domain.QueryContext;
import fr.ght1pc9kc.entity.api.Entity;
import fr.ght1pc9kc.juery.jooq.filter.JooqConditionVisitor;
import fr.ght1pc9kc.juery.jooq.pagination.JooqPagination;
Expand Down Expand Up @@ -66,9 +66,9 @@ public Mono<Entity<User>> get(String id) {

@Override
public Flux<Entity<User>> list(QueryContext qCtx) {
Condition conditions = qCtx.filter.accept(JOOQ_CONDITION_VISITOR);
Condition conditions = qCtx.filter().accept(JOOQ_CONDITION_VISITOR);
Select<Record> select = JooqPagination.apply(
qCtx.pagination, USER_PROPERTIES_MAPPING,
qCtx.pagination(), USER_PROPERTIES_MAPPING,
dsl.select(USERS.fields()).select(DSL.groupConcat(USERS_ROLES.USRO_ROLE).as(USERS_ROLES.USRO_ROLE.getName()))
.from(USERS)
.leftJoin(USERS_ROLES).on(USERS_ROLES.USRO_USER_ID.eq(USERS.USER_ID))
Expand Down Expand Up @@ -96,7 +96,7 @@ public Flux<Entity<User>> list() {

@Override
public Mono<Integer> count(QueryContext qCtx) {
Condition conditions = qCtx.getFilter().accept(JOOQ_CONDITION_VISITOR);
Condition conditions = qCtx.filter().accept(JOOQ_CONDITION_VISITOR);
return Mono.fromCallable(() -> dsl.fetchCount(dsl.selectFrom(USERS).where(conditions)))
.subscribeOn(databaseScheduler);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package fr.ght1pc9kc.baywatch.teams.infra.adapters;

import fr.ght1pc9kc.baywatch.common.domain.QueryContext;
import fr.ght1pc9kc.baywatch.common.infra.DatabaseQualifier;
import fr.ght1pc9kc.baywatch.dsl.tables.records.TeamsMembersRecord;
import fr.ght1pc9kc.baywatch.teams.api.model.TeamMember;
import fr.ght1pc9kc.baywatch.teams.domain.ports.TeamMemberPersistencePort;
import fr.ght1pc9kc.baywatch.teams.infra.mappers.TeamsMapper;
import fr.ght1pc9kc.baywatch.common.domain.QueryContext;
import fr.ght1pc9kc.entity.api.Entity;
import fr.ght1pc9kc.juery.jooq.filter.JooqConditionVisitor;
import fr.ght1pc9kc.juery.jooq.pagination.JooqPagination;
Expand Down Expand Up @@ -40,12 +40,12 @@ public class MembersPersistenceAdapter implements TeamMemberPersistencePort {
@Override
@SuppressWarnings("resource")
public Flux<Entity<TeamMember>> list(QueryContext qCtx) {
Condition conditions = qCtx.filter.accept(JOOQ_CONDITION_VISITOR);
Condition conditions = qCtx.filter().accept(JOOQ_CONDITION_VISITOR);
if (qCtx.isScoped()) {
conditions = conditions.and(TEAMS_MEMBERS.TEME_USER_ID.eq(qCtx.getUserId()));
conditions = conditions.and(TEAMS_MEMBERS.TEME_USER_ID.eq(qCtx.userId()));
}
Select<TeamsMembersRecord> select = JooqPagination.apply(
qCtx.pagination, TEAMS_MEMBERS_PROPERTIES_MAPPING,
qCtx.pagination(), TEAMS_MEMBERS_PROPERTIES_MAPPING,
dsl.selectFrom(TEAMS_MEMBERS)
.where(conditions));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package fr.ght1pc9kc.baywatch.teams.infra.adapters;

import fr.ght1pc9kc.baywatch.common.domain.QueryContext;
import fr.ght1pc9kc.baywatch.common.infra.DatabaseQualifier;
import fr.ght1pc9kc.baywatch.dsl.tables.records.TeamsRecord;
import fr.ght1pc9kc.baywatch.teams.api.model.Team;
import fr.ght1pc9kc.baywatch.teams.domain.ports.TeamPersistencePort;
import fr.ght1pc9kc.baywatch.teams.infra.mappers.TeamsMapper;
import fr.ght1pc9kc.baywatch.common.domain.QueryContext;
import fr.ght1pc9kc.entity.api.Entity;
import fr.ght1pc9kc.juery.jooq.filter.JooqConditionVisitor;
import fr.ght1pc9kc.juery.jooq.pagination.JooqPagination;
Expand Down Expand Up @@ -39,9 +39,9 @@ public class TeamPersistenceAdapter implements TeamPersistencePort {
@Override
@SuppressWarnings("resource")
public Flux<Entity<Team>> list(QueryContext qCtx) {
Condition conditions = qCtx.filter.accept(JOOQ_CONDITION_VISITOR);
Condition conditions = qCtx.filter().accept(JOOQ_CONDITION_VISITOR);
Select<TeamsRecord> select = JooqPagination.apply(
qCtx.pagination, TEAMS_PROPERTIES_MAPPING,
qCtx.pagination(), TEAMS_PROPERTIES_MAPPING,
dsl.selectFrom(TEAMS)
.where(conditions));

Expand All @@ -61,7 +61,7 @@ public Flux<Entity<Team>> list(QueryContext qCtx) {

@Override
public Mono<Integer> count(QueryContext qCtx) {
Condition conditions = qCtx.filter.accept(JOOQ_CONDITION_VISITOR);
Condition conditions = qCtx.filter().accept(JOOQ_CONDITION_VISITOR);
return Mono.fromCallable(() -> dsl.fetchCount(dsl.selectFrom(TEAMS).where(conditions)))
.subscribeOn(databaseScheduler);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.ght1pc9kc.baywatch.techwatch.domain;

import fr.ght1pc9kc.baywatch.common.domain.QueryContext;
import fr.ght1pc9kc.baywatch.common.domain.exceptions.BadRequestCriteria;
import fr.ght1pc9kc.baywatch.security.api.AuthenticationFacade;
import fr.ght1pc9kc.baywatch.security.api.model.Role;
Expand All @@ -11,7 +12,6 @@
import fr.ght1pc9kc.baywatch.techwatch.api.model.State;
import fr.ght1pc9kc.baywatch.techwatch.api.model.WebFeed;
import fr.ght1pc9kc.baywatch.techwatch.domain.filter.CriteriaModifierVisitor;
import fr.ght1pc9kc.baywatch.common.domain.QueryContext;
import fr.ght1pc9kc.baywatch.techwatch.domain.ports.FeedPersistencePort;
import fr.ght1pc9kc.baywatch.techwatch.domain.ports.NewsPersistencePort;
import fr.ght1pc9kc.baywatch.techwatch.domain.ports.StatePersistencePort;
Expand Down Expand Up @@ -90,13 +90,13 @@ public Mono<Integer> count(PageRequest pageRequest) {
}

public Mono<QueryContext> forgeAggregateQueryContext(QueryContext qCtx) {
List<String> props = qCtx.filter.accept(new ListPropertiesCriteriaVisitor());
List<String> props = qCtx.filter().accept(new ListPropertiesCriteriaVisitor());
if (props.size() == 1 && ID.equals(props.getFirst())) {
// Shortcut for get one News from id
return Mono.just(qCtx);
}
return teamServicePort.getTeamMates(qCtx.getUserId())
.concatWith(Mono.just(qCtx.getUserId()))
return teamServicePort.getTeamMates(qCtx.userId())
.concatWith(Mono.just(qCtx.userId()))
.distinct()
.collectList()
.flatMap(teamMates -> {
Expand All @@ -112,11 +112,11 @@ public Mono<QueryContext> forgeAggregateQueryContext(QueryContext qCtx) {
if (!contexts.getT1().isEmpty()) {
filters = Criteria.or(filters, Criteria.property(NEWS_ID).in(contexts.getT1()));
}
filters = Criteria.and(filters, qCtx.getFilter());
filters = Criteria.and(filters, qCtx.filter());

return QueryContext.builder()
.pagination(qCtx.getPagination())
.userId(qCtx.getUserId())
.pagination(qCtx.pagination())
.userId(qCtx.userId())
.teamMates(teamMates)
.filter(filters)
.build();
Expand All @@ -136,14 +136,14 @@ public Mono<QueryContext> forgeAggregateQueryContext(QueryContext qCtx) {
*/
public Mono<List<String>> getFeedFor(QueryContext qCtx, List<String> props) {
QueryContext feedQCtx = (props.contains(FEED_ID))
? QueryContext.all(qCtx.filter)
: QueryContext.all(qCtx.filter).withUserId(qCtx.userId);
? QueryContext.all(qCtx.filter())
: QueryContext.all(qCtx.filter()).withUserId(qCtx.userId());
return feedRepository.list(feedQCtx)
.map(Entity::id)
.collectList()
.map(feeds -> {
if (feedQCtx.isScoped()) {
feeds.add(feedQCtx.getUserId());
feeds.add(feedQCtx.userId());
}
return feeds;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package fr.ght1pc9kc.baywatch.techwatch.infra.persistence;

import fr.ght1pc9kc.baywatch.common.domain.QueryContext;
import fr.ght1pc9kc.baywatch.common.infra.DatabaseQualifier;
import fr.ght1pc9kc.baywatch.common.infra.adapters.PerformanceJooqListener;
import fr.ght1pc9kc.baywatch.common.infra.mappers.BaywatchMapper;
import fr.ght1pc9kc.baywatch.dsl.tables.records.FeedsRecord;
import fr.ght1pc9kc.baywatch.dsl.tables.records.FeedsUsersRecord;
import fr.ght1pc9kc.baywatch.techwatch.api.model.WebFeed;
import fr.ght1pc9kc.baywatch.common.domain.QueryContext;
import fr.ght1pc9kc.baywatch.techwatch.domain.ports.FeedPersistencePort;
import fr.ght1pc9kc.baywatch.techwatch.infra.model.FeedDeletedResult;
import fr.ght1pc9kc.entity.api.Entity;
Expand Down Expand Up @@ -199,20 +199,20 @@ public Flux<Entity<WebFeed>> persistUserRelation(Collection<Entity<WebFeed>> fee

@Override
public Mono<FeedDeletedResult> delete(QueryContext qCtx) {
Condition feedsUsersConditions = qCtx.filter.accept(FeedConditionsVisitors.feedUserIdVisitor());
Condition feedsUsersConditions = qCtx.filter().accept(FeedConditionsVisitors.feedUserIdVisitor());
final Optional<Query> deleteUserLinkQuery;
if (DSL.noCondition().equals(feedsUsersConditions)) {
deleteUserLinkQuery = Optional.empty();
} else {
var query = dsl.deleteQuery(FEEDS_USERS);
query.addConditions(feedsUsersConditions);
if (qCtx.isScoped()) {
query.addConditions(FEEDS_USERS.FEUS_USER_ID.eq(qCtx.userId));
query.addConditions(FEEDS_USERS.FEUS_USER_ID.eq(qCtx.userId()));
}
deleteUserLinkQuery = Optional.of(query);
}

Condition newsFeedConditions = qCtx.filter.accept(FeedConditionsVisitors.newsFeedIdVisitor());
Condition newsFeedConditions = qCtx.filter().accept(FeedConditionsVisitors.newsFeedIdVisitor());
final Optional<Query> deleteNewsFeedQuery;
if (DSL.noCondition().equals(newsFeedConditions)) {
deleteNewsFeedQuery = Optional.empty();
Expand All @@ -222,7 +222,7 @@ public Mono<FeedDeletedResult> delete(QueryContext qCtx) {
dsl.select(FEEDS_USERS.FEUS_FEED_ID).from(FEEDS_USERS).where(feedsUsersConditions))));
}

Condition feedsConditions = qCtx.filter.accept(FeedConditionsVisitors.feedIdVisitor());
Condition feedsConditions = qCtx.filter().accept(FeedConditionsVisitors.feedIdVisitor());
final Optional<Query> deleteFeedQuery;
if (DSL.noCondition().equals(feedsConditions)) {
deleteFeedQuery = Optional.empty();
Expand All @@ -243,7 +243,7 @@ public Mono<FeedDeletedResult> delete(QueryContext qCtx) {
}

private Select<Record> buildSelectQuery(QueryContext qCtx) {
Condition conditions = qCtx.filter.accept(JOOQ_CONDITION_VISITOR);
Condition conditions = qCtx.filter().accept(JOOQ_CONDITION_VISITOR);
SelectQuery<Record> select = dsl.selectQuery();
select.addSelect(FEEDS.fields());
select.addFrom(FEEDS);
Expand All @@ -252,16 +252,16 @@ private Select<Record> buildSelectQuery(QueryContext qCtx) {
if (qCtx.isScoped()) {
select.addSelect(FEEDS_USERS.FEUS_TAGS, FEEDS_USERS.FEUS_FEED_NAME);
select.addJoin(FEEDS_USERS, JoinType.JOIN,
FEEDS.FEED_ID.eq(FEEDS_USERS.FEUS_FEED_ID).and(FEEDS_USERS.FEUS_USER_ID.eq(qCtx.userId)));
FEEDS.FEED_ID.eq(FEEDS_USERS.FEUS_FEED_ID).and(FEEDS_USERS.FEUS_USER_ID.eq(qCtx.userId())));
} else {
select.addSelect(DSL.groupConcat(FEEDS_USERS.FEUS_USER_ID).as(FEEDS_USERS.FEUS_USER_ID));
select.addJoin(FEEDS_USERS, JoinType.LEFT_OUTER_JOIN,
FEEDS.FEED_ID.eq(FEEDS_USERS.FEUS_FEED_ID));
select.addGroupBy(FEEDS.fields());
Condition havings = qCtx.filter.accept(FeedConditionsVisitors.feedUserHavingVisitor());
Condition havings = qCtx.filter().accept(FeedConditionsVisitors.feedUserHavingVisitor());
select.addHaving(havings);
}

return JooqPagination.apply(qCtx.pagination, FEEDS_PROPERTIES_MAPPING, select);
return JooqPagination.apply(qCtx.pagination(), FEEDS_PROPERTIES_MAPPING, select);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import com.machinezoo.noexception.Exceptions;
import fr.ght1pc9kc.baywatch.common.api.model.EntitiesProperties;
import fr.ght1pc9kc.baywatch.common.domain.QueryContext;
import fr.ght1pc9kc.baywatch.common.infra.DatabaseQualifier;
import fr.ght1pc9kc.baywatch.common.infra.adapters.PerformanceJooqListener;
import fr.ght1pc9kc.baywatch.common.infra.mappers.BaywatchMapper;
import fr.ght1pc9kc.baywatch.dsl.tables.records.NewsFeedsRecord;
import fr.ght1pc9kc.baywatch.dsl.tables.records.NewsRecord;
import fr.ght1pc9kc.baywatch.techwatch.api.model.Flags;
import fr.ght1pc9kc.baywatch.techwatch.api.model.News;
import fr.ght1pc9kc.baywatch.common.domain.QueryContext;
import fr.ght1pc9kc.baywatch.techwatch.domain.ports.NewsPersistencePort;
import fr.ght1pc9kc.juery.basic.common.lang3.StringUtils;
import fr.ght1pc9kc.juery.basic.filter.ListPropertiesCriteriaVisitor;
Expand Down Expand Up @@ -161,10 +161,10 @@ public Mono<Integer> count(QueryContext qCtx) {
}

private static SelectQuery<Record> buildSelectQuery(Collection<Field<?>> fields, QueryContext qCtx, DSLContext dsl) {
Condition conditions = Optional.ofNullable(qCtx.filter).map(f -> f.accept(NEWS_CONDITION_VISITOR))
Condition conditions = Optional.ofNullable(qCtx.filter()).map(f -> f.accept(NEWS_CONDITION_VISITOR))
.orElse(DSL.noCondition());

List<String> properties = Optional.ofNullable(qCtx.filter).map(f -> f.accept(LIST_PROPERTIES_VISITOR))
List<String> properties = Optional.ofNullable(qCtx.filter()).map(f -> f.accept(LIST_PROPERTIES_VISITOR))
.orElse(List.of());

SelectQuery<Record> select = dsl.selectQuery();
Expand All @@ -176,23 +176,23 @@ private static SelectQuery<Record> buildSelectQuery(Collection<Field<?>> fields,
select.addJoin(NEWS_FEEDS, JoinType.LEFT_OUTER_JOIN, NEWS.NEWS_ID.eq(NEWS_FEEDS.NEFE_NEWS_ID));
select.addGroupBy(fields);

if (!StringUtils.isBlank(qCtx.userId)) {
if (!StringUtils.isBlank(qCtx.userId())) {
select.addSelect(NEWS_STATE.NURS_STATE);
select.addJoin(NEWS_STATE, JoinType.LEFT_OUTER_JOIN,
NEWS.NEWS_ID.eq(NEWS_STATE.NURS_NEWS_ID).and(NEWS_STATE.NURS_USER_ID.eq(qCtx.userId)));
NEWS.NEWS_ID.eq(NEWS_STATE.NURS_NEWS_ID).and(NEWS_STATE.NURS_USER_ID.eq(qCtx.userId())));

if (properties.contains(EntitiesProperties.POPULAR)) {
ArrayList<Condition> popularJoinConditions = new ArrayList<>();
popularJoinConditions.add(NEWS.NEWS_ID.eq(POPULAR.NURS_NEWS_ID));
popularJoinConditions.add(DSL.coalesce(POPULAR.NURS_STATE, Flags.NONE).bitAnd(Flags.SHARED).eq(Flags.SHARED));
if (!qCtx.teamMates.isEmpty()) {
popularJoinConditions.add(POPULAR.NURS_USER_ID.in(qCtx.teamMates));
if (!qCtx.teamMates().isEmpty()) {
popularJoinConditions.add(POPULAR.NURS_USER_ID.in(qCtx.teamMates()));
}
select.addJoin(POPULAR, JoinType.LEFT_OUTER_JOIN, DSL.condition(Operator.AND, popularJoinConditions));
}
}

SelectQuery<Record> paginateSelect = (SelectQuery<Record>) JooqPagination.apply(qCtx.pagination, NEWS_PROPERTIES_MAPPING, select);
SelectQuery<Record> paginateSelect = (SelectQuery<Record>) JooqPagination.apply(qCtx.pagination(), NEWS_PROPERTIES_MAPPING, select);
paginateSelect.addOrderBy(NEWS.NEWS_ID); // This avoids random order for records with same value in ordered fields
return paginateSelect;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package fr.ght1pc9kc.baywatch.techwatch.infra.persistence;

import fr.ght1pc9kc.baywatch.common.api.DefaultMeta;
import fr.ght1pc9kc.baywatch.common.domain.QueryContext;
import fr.ght1pc9kc.baywatch.common.infra.DatabaseQualifier;
import fr.ght1pc9kc.baywatch.common.infra.mappers.PropertiesMappers;
import fr.ght1pc9kc.baywatch.dsl.tables.records.NewsUserStateRecord;
import fr.ght1pc9kc.baywatch.techwatch.api.model.Flags;
import fr.ght1pc9kc.baywatch.techwatch.api.model.State;
import fr.ght1pc9kc.baywatch.common.domain.QueryContext;
import fr.ght1pc9kc.baywatch.techwatch.domain.ports.StatePersistencePort;
import fr.ght1pc9kc.entity.api.Entity;
import fr.ght1pc9kc.juery.api.Criteria;
Expand Down Expand Up @@ -45,9 +45,9 @@ public Mono<Entity<State>> get(QueryContext queryContext) {
@Override
public Flux<Entity<State>> list(QueryContext queryContext) {
var query = dsl.selectQuery(NEWS_USER_STATE);
query.addConditions(queryContext.getFilter().accept(STATE_CONDITION_VISITOR));
query.addConditions(queryContext.filter().accept(STATE_CONDITION_VISITOR));
if (queryContext.isScoped()) {
query.addConditions(NEWS_USER_STATE.NURS_USER_ID.eq(queryContext.getUserId()));
query.addConditions(NEWS_USER_STATE.NURS_USER_ID.eq(queryContext.userId()));
}
return Flux.<NewsUserStateRecord>create(sink -> {
Cursor<NewsUserStateRecord> cursor = query.fetchLazy();
Expand Down

0 comments on commit cc3dc40

Please sign in to comment.