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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,3 +39,4 @@ public class Stock {
|
||||
@Field(name = "LOCATIONS")
|
||||
private List<Location> locations;
|
||||
}
|
||||
|
||||
|
||||
13
src/main/java/fr/eni/demo/bo/StockCount.java
Normal file
13
src/main/java/fr/eni/demo/bo/StockCount.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package fr.eni.demo.bo;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
public class StockCount {
|
||||
private String name;
|
||||
private long count;
|
||||
}
|
||||
@@ -2,11 +2,13 @@ package fr.eni.demo.controller;
|
||||
|
||||
import fr.eni.demo.bll.StockService;
|
||||
import fr.eni.demo.bo.Stock;
|
||||
import fr.eni.demo.bo.StockCount;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -15,31 +17,41 @@ import java.util.Optional;
|
||||
@RequiredArgsConstructor
|
||||
public class StockController {
|
||||
|
||||
private final StockService stockService;
|
||||
private final StockService stockService;
|
||||
|
||||
// Ajouter un stock
|
||||
@PostMapping
|
||||
public ResponseEntity<Map<String, Object>> create(@RequestBody Stock stock) {
|
||||
stockService.add(stock);
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("message", "Stock added");
|
||||
response.put("status", true);
|
||||
response.put("data", new HashMap<>());
|
||||
// Ajouter un stock
|
||||
@PostMapping
|
||||
public ResponseEntity<Map<String, Object>> create(@RequestBody Stock stock) {
|
||||
stockService.add(stock);
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("message", "Stock added");
|
||||
response.put("status", true);
|
||||
response.put("data", new HashMap<>());
|
||||
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
// Chercher des clients par nom
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<Map<String, Object>> findById(@PathVariable Long id) {
|
||||
Optional<Stock> result = stockService.findById(id);
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("message", "List of Clients found");
|
||||
response.put("status", true);
|
||||
response.put("data", result);
|
||||
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
// Chercher des stocks par id
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<Map<String, Object>> findById(@PathVariable Long id) {
|
||||
Optional<Stock> result = stockService.findById(id);
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("message", "List of Clients found");
|
||||
response.put("status", 200);
|
||||
response.put("data", result);
|
||||
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
// Trouver les jeux par nom et compter le result par nom
|
||||
// ACCESS PUBLIC
|
||||
@GetMapping("/{name}")
|
||||
public ResponseEntity<Map<String, Object>> findByName(@PathVariable String name) {
|
||||
List<StockCount> result = stockService.findAllByName(name);
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("message", "List of Clients found");
|
||||
response.put("status", 200);
|
||||
response.put("data", result);
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface ClientRepository extends MongoRepository<Client, Long> {
|
||||
|
||||
@@ -4,6 +4,9 @@ import fr.eni.demo.bo.Stock;
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface StockRepository extends MongoRepository<Stock,Long> {
|
||||
List<Stock> findByNameIsContainingIgnoreCase(String name);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user