find stock by name and count done
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
package fr.eni.demo.bll;
|
||||
|
||||
import fr.eni.demo.bo.Stock;
|
||||
import fr.eni.demo.bo.StockCount;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface StockService {
|
||||
void add(Stock game);
|
||||
Optional<Stock> findById(Long gameId);
|
||||
List<StockCount> findAllByName(String name);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package fr.eni.demo.bll;
|
||||
|
||||
import fr.eni.demo.bo.Stock;
|
||||
import fr.eni.demo.bo.StockCount;
|
||||
import fr.eni.demo.dal.StockRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class StockServiceImpl implements StockService{
|
||||
@@ -25,4 +28,15 @@ public class StockServiceImpl implements StockService{
|
||||
public Optional<Stock> findById(Long gameId) {
|
||||
return stockRepository.findById(gameId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<StockCount> findAllByName(String name){
|
||||
List<Stock> stocks = stockRepository.findByNameIsContainingIgnoreCase(name);
|
||||
List<StockCount> result = stocks.stream()
|
||||
.collect(Collectors.groupingBy(Stock::getName, Collectors.counting()))
|
||||
.entrySet().stream()
|
||||
.map(entry -> new StockCount(entry.getKey(), entry.getValue()))
|
||||
.toList();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user