add spring

This commit is contained in:
Σlie *
2026-02-27 21:02:08 +01:00
parent 14fbdc348a
commit fcff7c1c23
7 changed files with 136 additions and 0 deletions

18
pom.xml
View File

@ -14,7 +14,25 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.2</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>

View File

@ -3,7 +3,12 @@ package fr.tetelie.crawler;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class Crawler {
@ -19,6 +24,8 @@ public class Crawler {
// Si pas de connexion à la base de donnée on s'arrête ici
if(!isConnected){return;};
SpringApplication.run(Crawler.class, args);
// On request les prix de tous les produits et on les inject dans la bdd

View File

@ -0,0 +1,30 @@
package fr.tetelie.crawler.web;
import jakarta.persistence.*;
@Entity
@Table(name = "products") // Correspond au nom de ta table SQL
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@Column(name = "link") // Le nom exact de ta colonne SQL
private String link;
@Column(name = "image_url")
private String imageUrl;
// Getters et Setters (indispensables pour Spring)
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getLink() { return link; }
public void setLink(String link) { this.link = link; }
public String getImageUrl() { return imageUrl; }
public void setImageUrl(String imageUrl) { this.imageUrl = imageUrl; }
}

View File

@ -0,0 +1,35 @@
package fr.tetelie.crawler.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class ProductController {
@Autowired
private ProductRepository productRepository; // Spring gère SQL tout seul !
@GetMapping("/")
public String listProducts(Model model) {
model.addAttribute("products", productRepository.findAll());
return "index"; // Ça va chercher src/main/resources/templates/index.html
}
@PostMapping("/add")
public String addProduct(@RequestParam String link) {
Product p = new Product();
p.setLink(link);
productRepository.save(p);
return "redirect:/";
}
@GetMapping("/delete/{id}")
public String deleteProduct(@PathVariable Long id) {
productRepository.deleteById(id);
return "redirect:/";
}
}

View File

@ -0,0 +1,9 @@
package fr.tetelie.crawler.web;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
// Tu as maintenant accès à : save(), findAll(), deleteById(), etc.
}

View File

@ -0,0 +1,5 @@
spring.datasource.url=${DB_URL}
spring.datasource.username=${DB_USER}
spring.datasource.password=${DB_PASS}
spring.jpa.hibernate.ddl-auto=update
server.port=8083

View File

@ -0,0 +1,32 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Iceberg Price Tracker</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body class="bg-gray-900 text-white p-10">
<h1 class="text-3xl font-bold mb-6">🧊 Iceberg Dashboard</h1>
<form action="/add" method="POST" class="mb-10">
<input type="text" name="link" placeholder="Lien Amazon..." class="p-2 rounded bg-gray-800 border border-gray-700 w-1/2">
<button type="submit" class="bg-blue-600 px-4 py-2 rounded">Ajouter</button>
</form>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<div th:each="product : ${products}" class="bg-gray-800 p-4 rounded-xl shadow-lg">
<div class="flex items-center gap-4 mb-4">
<img th:src="${product.imageUrl}" class="w-16 h-16 object-contain rounded">
<h2 class="flex-1 font-semibold text-sm" th:text="${product.name}">Nom du produit</h2>
<a th:href="@{'/delete/' + ${product.id}}" class="text-red-500">🗑️</a>
</div>
<canvas th:id="'chart-' + ${product.id}"></canvas>
<script th:inline="javascript">
/* Code pour générer le graphique Chart.js pour chaque produit */
</script>
</div>
</div>
</body>
</html>