Skip to content

Instantly share code, notes, and snippets.

View Raouf25's full-sized avatar

ABDERRAOUF MAKHLOUF Raouf25

View GitHub Profile
private InputStream extractInputStream(MultipartFile file) {
try {
return file.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@FunctionalInterface
interface FunctionWithException<T, R, E extends Exception> {
R apply(T t) throws E;
}
private <T, R, E extends Exception>
Function<T, R> wrapper(FunctionWithException<T, R, E> fun) {
return arg -> {
try {
return fun.apply(arg);
} catch (Exception e) {
throw new RuntimeException(e);
}
};
}
files.stream()
.parallel()
.map(wrapper(file->file.getInputStream()))
.forEach(inputStream -> carService.saveCars(inputStream));
public interface CarRepository extends JpaRepository<Car, Long> {
List<Car> findByManufacturer(String manufacturer, Pageable pageable);
List<Car> findByModel(String model, Pageable pageable);
List<Car> findByManufacturerAndModel(String manufacturer, String model, Pageable pageable);
// other combinations omitted for sanity
}
package com.mak.springbootefficientsearchapi.entity;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
@Transactional
@GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<List<Car>> get(
@And({
@Spec(path = "manufacturer", params = "manufacturer", spec = Like.class),
@Spec(path = "model", params = "model", spec = Like.class),
@Spec(path = "country", params = "country", spec = In.class),
@Spec(path = "type", params = "type", spec = Like.class),
@Spec(path = "createDate", params = "createDate", spec = Equal.class),
/**
* get element using Criteria.
*
* @param spec *
* @param headers pagination data
* @param sort sort criteria
* @return retrieve elements with pagination
*/
public PagingResponse get(Specification<Car> spec, HttpHeaders headers, Sort sort) {
if (isRequestPaged(headers)) {
<dependency>
<groupId>net.kaczmarzyk</groupId>
<artifactId>specification-arg-resolver</artifactId>
<version>2.1.1</version>
</dependency>
package com.mak.springbootefficientsearchapi.configuration;
import net.kaczmarzyk.spring.data.jpa.web.SpecificationArgumentResolver;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;