find stock by name and count done

This commit is contained in:
Olivier PARPAILLON
2025-07-16 12:04:18 +02:00
parent 1535f6bf19
commit de61006d86
7 changed files with 68 additions and 23 deletions

View File

@@ -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);
}

View File

@@ -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;
}
}