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

Feat/customer persistence #19

Merged
merged 3 commits into from
Jun 25, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/ci-code-quality.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Qodana
on:
workflow_dispatch:
pull_request:
push:
branches:
- main

jobs:
qodana:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
checks: write
steps:
- name: Checkout latest code
uses: actions/checkout@v4
- name: 'Qodana Scan'
uses: JetBrains/qodana-action@v2024.1
env:
QODANA_TOKEN: ${{ secrets.QODANA_TOKEN }}
6 changes: 5 additions & 1 deletion pta-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
<relativePath>../pta-parent/pom.xml</relativePath>
</parent>
<artifactId>pta-common</artifactId>
<version>0.3.0</version>
<version>0.4.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest</artifactId>
</dependency>
<dependency>
<groupId>io.smallrye.reactive</groupId>
<artifactId>mutiny</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
package ch.obya.pta.common.domain.repository;

import java.util.Collection;

import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;

import ch.obya.pta.common.domain.entity.Entity;
import ch.obya.pta.common.domain.vo.Identity;
import ch.obya.pta.common.util.search.FindCriteria;
import ch.obya.pta.common.util.search.AttributeFilter;
import io.smallrye.mutiny.Uni;

import java.util.List;
import java.util.Map;

public interface EntityRepository<E extends Entity<E, I, S>, I extends Identity, S> {

Uni<E> findOne(I id);

Multi<E> findByCriteria(Collection<FindCriteria> criteria);
Uni<List<E>> findByCriteria(Map<String, List<AttributeFilter>> criteria);

default Multi<E> findAll() {
return findByCriteria(FindCriteria.empty());
}
Uni<List<E>> findAll();

Uni<E> save(I id, S state);
Uni<I> save(I id, S state);

default Uni<E> save(Entity<E, I, S> entity) {
default Uni<I> save(Entity<E, I, S> entity) {
return save(entity.id(), entity.state());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package ch.obya.pta.common.domain.repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.function.BiFunction;

import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;

import ch.obya.pta.common.domain.entity.BaseEntity;
import ch.obya.pta.common.domain.entity.Entity;
import ch.obya.pta.common.domain.vo.Identity;
import ch.obya.pta.common.util.search.FindCriteria;
import ch.obya.pta.common.util.search.AttributeFilter;
import io.smallrye.mutiny.Uni;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.experimental.FieldDefaults;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;

import static java.util.stream.Collectors.toList;

@NoArgsConstructor
Expand All @@ -36,14 +35,19 @@ public Uni<E> findOne(I id) {
}

@Override
public Multi<E> findByCriteria(Collection<FindCriteria> criteria) {
return Multi.createFrom().iterable(store.entrySet().stream().map(e -> entityCreator.apply(e.getKey(), e.getValue())).collect(toList()));
public Uni<List<E>> findByCriteria(Map<String, List<AttributeFilter>> criteria) {
return findAll();
}

@Override
public Uni<List<E>> findAll() {
return Uni.createFrom().item(store.entrySet().stream().map(e -> entityCreator.apply(e.getKey(), e.getValue())).collect(toList()));
}

@Override
public Uni<E> save(I id, S state) {
public Uni<I> save(I id, S state) {
store.put(id, state);
return Uni.createFrom().item(entityCreator.apply(id, state));
return Uni.createFrom().item(entityCreator.apply(id, state)).map(Entity::id);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ch.obya.pta.common.infrastructure.web;

import ch.obya.pta.common.domain.vo.Name;
import ch.obya.pta.common.domain.vo.Quota;
import jakarta.ws.rs.ext.ParamConverter;
import jakarta.ws.rs.ext.ParamConverterProvider;
import jakarta.ws.rs.ext.Provider;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Map;

@Provider
public class CommonAttributeConverterProvider implements ParamConverterProvider {

private final Map<Class, ParamConverter> converters = Map.of(
Name.class, new NameConverter(),
Quota.class, new QuotaConverter()
);

@SuppressWarnings("unchecked")
@Override
public <T> ParamConverter<T> getConverter(Class<T> aClass, Type type, Annotation[] annotations) {
return converters.getOrDefault(aClass, null);
}

private static class NameConverter implements ParamConverter<Name> {
@Override
public Name fromString(String value) {
return new Name(value);
}

@Override
public String toString(Name value) {
return value.content();
}
}

private static class QuotaConverter implements ParamConverter<Quota> {
@Override
public Quota fromString(String value) {
var fields = value.split(",");
return new Quota(Integer.valueOf(fields[0]), Integer.valueOf(fields[1]));
}

@Override
public String toString(Quota value) {
return "%d,%d".formatted(value.min(), value.max());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package ch.obya.pta.common.util.search;

import jakarta.annotation.Nullable;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public record AttributeFilter(Operator operator, Object value) {

public interface Parser {
Object parse(String value);
}

public static List<AttributeFilter> from(List<String> filters, Parser parser) {
return filters.stream().map(filter -> from(filter, parser)).toList();
}

public static AttributeFilter from(String filter, Parser parser) {
Pattern pattern = Pattern.compile("(eq:|ne:|lt:|gt:|le:|ge:|like:)*(.+)");
Matcher matcher = pattern.matcher(filter);
if (matcher.find()) {
return matcher.groupCount() == 1 ?
new AttributeFilter(Operator.EQUALS, parser.parse(matcher.group(1))) :
new AttributeFilter(Operator.from(matcher.group(1)), parser.parse(matcher.group(2)));
}
throw new IllegalArgumentException("Invalid filter: " + filter);
}

public static AttributeFilter equal(Object value) {
return new AttributeFilter(Operator.EQUALS, value);
}

public static AttributeFilter notEqual(Object value) {
return new AttributeFilter(Operator.NOT_EQUALS, value);
}

public static AttributeFilter lessThan(Object value) {
return new AttributeFilter(Operator.LESS_THAN, value);
}

public static AttributeFilter lessThanOrEqual(Object value) {
return new AttributeFilter(Operator.LESS_THAN_EQUALS, value);
}

public static AttributeFilter greaterThan(Object value) {
return new AttributeFilter(Operator.GREATER_THAN, value);
}

public static AttributeFilter greaterThanOrEqual(Object value) {
return new AttributeFilter(Operator.GREATER_THAN_EQUALS, value);
}

public static AttributeFilter like(Object value) {
return new AttributeFilter(Operator.LIKE, value);
}

public enum Operator {
EQUALS("eq:"),
NOT_EQUALS("ne:"),
LESS_THAN("lt:"),
LESS_THAN_EQUALS("le:"),
GREATER_THAN("gt:"),
GREATER_THAN_EQUALS("ge:"),
LIKE("like:");

private final String symbol;

Operator(String symbol) {
this.symbol = symbol;
}

public String symbol() {
return symbol;
}

public static Operator from(@Nullable String symbol) {
if (symbol == null) {
return EQUALS;
}
for (Operator operator : Operator.values()) {
if (operator.symbol().equals(symbol)) {
return operator;
}
}
throw new IllegalArgumentException("Unknown filtering operation: " + symbol);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ch.obya.pta.common.util;

import ch.obya.pta.common.util.search.AttributeFilter;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class AttributeFilterTest {

@Test
void should_build_attribute_filter() {
assertThat(AttributeFilter.from("eq:albert", s -> s)).isEqualTo(AttributeFilter.equal("albert"));
assertThat(AttributeFilter.from("eq:1958-12-12", s -> s)).isEqualTo(AttributeFilter.equal("1958-12-12"));
}
}

This file was deleted.

Loading
Loading