Skip to content

Commit

Permalink
feat: Add interfaces for CRUD services
Browse files Browse the repository at this point in the history
These were previously in Hilla but are equally useful in Flow applications
  • Loading branch information
Artur- committed Dec 18, 2024
1 parent fb02577 commit 6dcba3a
Show file tree
Hide file tree
Showing 16 changed files with 708 additions and 0 deletions.
36 changes: 36 additions & 0 deletions flow-server/src/main/java/com/vaadin/flow/Nonnull.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2000-2022 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Annotation to mark either field, method, parameter or type parameter as
* non-nullable. It is used by Typescript Generator as a source of type
* nullability information.
*
* This annotation exists because the traditional
* `jakarta.annotation.Nonnull` annotation is not applicable to type parameters.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE_USE })
public @interface Nonnull {
}
37 changes: 37 additions & 0 deletions flow-server/src/main/java/com/vaadin/flow/Nullable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2000-2022 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Annotation to mark either field, method, parameter or type parameter as
* nullable. It is used by Typescript Generator as a source of type nullability
* information.
*
* This annotation exists because the traditional
* `jakarta.annotation.Nullable` annotation is not applicable to type
* parameters.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE_USE })
public @interface Nullable {
}
20 changes: 20 additions & 0 deletions vaadin-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,26 @@
<artifactId>spring-data-commons</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<scope>provided</scope>
<exclusions>
<exclusion>
<artifactId>aspectjrt</artifactId>
<groupId>org.aspectj</groupId>
</exclusion>
<exclusion>
<artifactId>jcl-over-slf4j</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>flow-data</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.vaadin.flow.spring.data;

import org.springframework.lang.Nullable;

import com.vaadin.flow.spring.data.filter.Filter;

/**
* A browser-callable service that can count the given type of objects with a
* given filter.
*/
public interface CountService {

/**
* Counts the number of entities that match the given filter.
*
* @param filter
* the filter, or {@code null} to use no filter
* @return
*/
public long count(@Nullable Filter filter);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.vaadin.flow.spring.data;

/**
* A browser-callable service that can create, read, update, and delete a given
* type of object.
*/
public interface CrudService<T, ID> extends ListService<T>, FormService<T, ID> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.vaadin.flow.spring.data;

import org.springframework.lang.Nullable;

/**
* A browser-callable service that can create, update, and delete a given type
* of object.
*/
public interface FormService<T, ID> {

/**
* Saves the given object and returns the (potentially) updated object.
* <p>
* If you store the object in a SQL database, the returned object might have
* a new id or updated consistency version.
*
* @param value
* the object to save
* @return the fresh object or {@code null} if no object was found to update
*/
@Nullable
T save(T value);

/**
* Deletes the object with the given id.
*
* @param id
* the id of the object to delete
*/
void delete(ID id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.vaadin.flow.spring.data;

import java.util.Optional;

/**
* A browser-callable service that can fetch the given type of object.
*/
public interface GetService<T, ID> {

/**
* Gets the object with the given id.
*
* @param id
* the id of the object
* @return the object, or an empty optional if no object with the given id
*/
Optional<T> get(ID id);

/**
* Checks if an object with the given id exists.
*
* @param id
* the id of the object
* @return {@code true} if the object exists, {@code false} otherwise
*/
default boolean exists(ID id) {
return get(id).isPresent();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.vaadin.flow.spring.data;

import java.util.List;

import com.vaadin.flow.Nullable;
import com.vaadin.flow.Nonnull;

import com.vaadin.flow.spring.data.filter.Filter;
import org.springframework.data.domain.Pageable;

/**
* A browser-callable service that can list the given type of object.
*/
public interface ListService<T> {
/**
* Lists objects of the given type using the paging, sorting and filtering
* options provided in the parameters.
*
* @param pageable
* contains information about paging and sorting
* @param filter
* the filter to apply or {@code null} to not filter
* @return a list of objects or an empty list if no objects were found
*/
@Nonnull
List<@Nonnull T> list(Pageable pageable, @Nullable Filter filter);

}
Loading

0 comments on commit 6dcba3a

Please sign in to comment.