add graph
This commit is contained in:
25
src/main/java/fr/tetelie/crawler/web/PriceHistory.java
Normal file
25
src/main/java/fr/tetelie/crawler/web/PriceHistory.java
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package fr.tetelie.crawler.web;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "price_history")
|
||||||
|
public class PriceHistory {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
private Float price;
|
||||||
|
|
||||||
|
@Column(name = "date_check")
|
||||||
|
private LocalDateTime dateCheck;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "product_id")
|
||||||
|
private Product product;
|
||||||
|
|
||||||
|
// Getters & Setters
|
||||||
|
public Float getPrice() { return price; }
|
||||||
|
public LocalDateTime getDateCheck() { return dateCheck; }
|
||||||
|
}
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
package fr.tetelie.crawler.web;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface PriceHistoryRepository extends JpaRepository<PriceHistory, Integer> {
|
||||||
|
List<PriceHistory> findByProductIdOrderByDateCheckAsc(Integer productId);
|
||||||
|
}
|
||||||
@ -34,4 +34,13 @@ public class ProductController {
|
|||||||
productRepository.deleteById(id);
|
productRepository.deleteById(id);
|
||||||
return "redirect:/";
|
return "redirect:/";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private PriceHistoryRepository priceHistoryRepository;
|
||||||
|
|
||||||
|
@GetMapping("/api/prices/{id}")
|
||||||
|
@ResponseBody // Renvoie du JSON au lieu d'une page HTML
|
||||||
|
public List<PriceHistory> getPriceData(@PathVariable Integer id) {
|
||||||
|
return priceHistoryRepository.findByProductIdOrderByDateCheckAsc(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -3,7 +3,8 @@ package fr.tetelie.crawler.web;
|
|||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface ProductRepository extends JpaRepository<Product, Integer> {
|
public interface ProductRepository extends JpaRepository<Product, Integer> {
|
||||||
// Tu as maintenant accès à : save(), findAll(), deleteById(), etc.
|
|
||||||
}
|
}
|
||||||
@ -29,5 +29,50 @@
|
|||||||
</script>
|
</script>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
||||||
|
|
||||||
|
<div class="p-4 bg-gray-800 rounded-lg shadow-md mb-4">
|
||||||
|
<h3 th:text="${product.name}" class="text-xl font-bold mb-2"></h3>
|
||||||
|
|
||||||
|
<div style="height: 200px;">
|
||||||
|
<canvas th:id="'chart-' + ${product.id}"></canvas>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script th:inline="javascript">
|
||||||
|
(function() {
|
||||||
|
const productId = [[${product.id}]];
|
||||||
|
const ctx = document.getElementById('chart-' + productId).getContext('2d');
|
||||||
|
|
||||||
|
// On va chercher les données via l'API qu'on a créée
|
||||||
|
fetch('/api/prices/' + productId)
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
const labels = data.map(entry => new Date(entry.dateCheck).toLocaleDateString());
|
||||||
|
const prices = data.map(entry => entry.price);
|
||||||
|
|
||||||
|
new Chart(ctx, {
|
||||||
|
type: 'line',
|
||||||
|
data: {
|
||||||
|
labels: labels,
|
||||||
|
datasets: [{
|
||||||
|
label: 'Prix (€)',
|
||||||
|
data: prices,
|
||||||
|
borderColor: '#3b82f6', // Bleu Tailwind
|
||||||
|
backgroundColor: 'rgba(59, 130, 246, 0.1)',
|
||||||
|
fill: true,
|
||||||
|
tension: 0.4 // Courbe arrondie
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
options: {
|
||||||
|
responsive: true,
|
||||||
|
maintainAspectRatio: false,
|
||||||
|
plugins: { legend: { display: false } }
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
})();
|
||||||
|
</script>
|
||||||
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Reference in New Issue
Block a user