find stock by ref added
This commit is contained in:
@@ -10,5 +10,6 @@ public interface StockService {
|
||||
void add(Stock game);
|
||||
Optional<Stock> findById(Long gameId);
|
||||
List<StockCount> findAllByName(String name);
|
||||
Stock findByRef(String ref);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package fr.eni.demo.bll;
|
||||
import fr.eni.demo.bo.Stock;
|
||||
import fr.eni.demo.bo.StockCount;
|
||||
import fr.eni.demo.dal.StockRepository;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -39,4 +40,13 @@ public class StockServiceImpl implements StockService{
|
||||
.toList();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stock findByRef(String ref) {
|
||||
Stock stock = stockRepository.findByRefEQ(ref);
|
||||
if (stock == null) {
|
||||
throw new EntityNotFoundException("Ref non valide");
|
||||
}
|
||||
return stock;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +41,12 @@ public class StockController {
|
||||
return buildResponse("List stock fetched", true, result);
|
||||
}
|
||||
|
||||
@GetMapping("/{ref}")
|
||||
public ResponseEntity<Map<String, Object>> findByRef(@PathVariable String ref) {
|
||||
Stock result = stockService.findByRef(ref);
|
||||
return buildResponse("Stock fetched", true, result);
|
||||
}
|
||||
|
||||
private ResponseEntity<Map<String, Object>> buildResponse(String message, boolean status, Object data) {
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("message", message);
|
||||
|
||||
@@ -9,4 +9,5 @@ import java.util.List;
|
||||
@Repository
|
||||
public interface StockRepository extends MongoRepository<Stock,Long> {
|
||||
List<Stock> findByNameIsContainingIgnoreCase(String name);
|
||||
Stock findByRefEQ(String ref);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user