-
-
Save henesgokdag/d9f23a3591dfd1340018a4c3c938efc9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Service | |
public class ProductService { | |
@Autowired | |
private ProductRepository productRepository; | |
@Autowired | |
private ElasticsearchOperations elasticsearchOperations; | |
public List<Product> allProducts(){ | |
List<Product> products = new ArrayList<Product>(); | |
productRepository.findAll().forEach(product -> { | |
products.add(product); | |
}); | |
return products; | |
} | |
public void saveProduct(Product product){ | |
productRepository.save(product); | |
} | |
public Product getProductById(String id){ | |
return productRepository.findById(id).orElseThrow(()-> new RuntimeException("İlgili Kayıt Bulunamadı")); | |
} | |
public List<Product> findProductByName(String name){ | |
List<Product> products = new ArrayList<Product>(); | |
productRepository.findByName(name).forEach(product -> { | |
products.add(product); | |
}); | |
return products; | |
} | |
public Product updateProductById(String id,Product updateProduct){ | |
NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() | |
.withQuery(matchQuery("id", id).minimumShouldMatch("100%")) | |
.build(); | |
SearchHits<Product> products = | |
elasticsearchOperations.search(searchQuery, Product.class, IndexCoordinates.of("productindex")); | |
Product product= products.getSearchHit(0).getContent(); | |
product=updateProduct; | |
productRepository.save(product); | |
return product; | |
} | |
public void deleteProductById(String id){ | |
productRepository.deleteById(id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment