diff --git a/pom.xml b/pom.xml index b510ae9e4b0742526658506215f3e9f6d0100e19..a4ef1768616e56939dd5e8489ca6bde629ee6ce6 100644 --- a/pom.xml +++ b/pom.xml @@ -59,6 +59,11 @@ <artifactId>jaxws-api</artifactId> <version>2.3.1</version> </dependency> + <dependency> + <groupId>com.github.javafaker</groupId> + <artifactId>javafaker</artifactId> + <version>1.0.2</version> + </dependency> </dependencies> <build> diff --git a/src/main/java/com/podcastify/main/Main.java b/src/main/java/com/podcastify/main/Main.java index 738c68a40eee2a24ab1a5cdb59b888a08aa393d1..1ceada2f549f6eed56230a7931c3b38160b9efd7 100644 --- a/src/main/java/com/podcastify/main/Main.java +++ b/src/main/java/com/podcastify/main/Main.java @@ -1,6 +1,7 @@ package com.podcastify.main; import com.podcastify.implementor.SubscribeServiceImpl; +import com.podcastify.utils.Seed; import javax.xml.ws.Endpoint; import io.github.cdimascio.dotenv.Dotenv; @@ -8,6 +9,11 @@ import io.github.cdimascio.dotenv.Dotenv; public class Main { public static void main(String[] args) { try { + // Seeding + Seed s = new Seed(); + s.seedSubscriptions(); + + // Publish endpoint after done seeding Dotenv dotenv = Dotenv.load(); String port = dotenv.get("PORT", "5555"); diff --git a/src/main/java/com/podcastify/utils/Seed.java b/src/main/java/com/podcastify/utils/Seed.java new file mode 100644 index 0000000000000000000000000000000000000000..bf69e5e66641aa843df3c0366e98fbf795932002 --- /dev/null +++ b/src/main/java/com/podcastify/utils/Seed.java @@ -0,0 +1,52 @@ +package com.podcastify.utils; + +import com.podcastify.db.Database; + +import com.github.javafaker.Faker; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.HashSet; +import java.util.Set; + +public class Seed { + private Database db; + private Connection conn; + + public Seed() { + this.db = new Database(); + this.conn = this.db.getConnection(); + } + + public void seedSubscriptions() { + Faker faker = new Faker(); + Set<String> existingSubscriptions = new HashSet<>(); + + String insertQuery = "INSERT INTO subscriptions (creator_id, creator_name, subscriber_id, subscriber_name, status_id) VALUES (?, ?, ?, ?, ?)"; + + System.out.println("Seeding subscriptions table..."); + try { + PreparedStatement preparedStatement = conn.prepareStatement(insertQuery); + + for (int i = 1; i <= 10; i++) { + for (int j = 1; j <= 10; j++) { + String subscriptionKey = i + "-" + j; + if (!existingSubscriptions.contains(subscriptionKey)) { + preparedStatement.setInt(1, i); + preparedStatement.setString(2, faker.name().fullName()); + preparedStatement.setInt(3, j); + preparedStatement.setString(4, faker.name().fullName()); + preparedStatement.setInt(5, faker.number().numberBetween(1, 4)); + preparedStatement.executeUpdate(); + + existingSubscriptions.add(subscriptionKey); + } + } + } + this.conn.commit(); + System.out.println("Seeding completed successfully."); + } catch (SQLException e) { + System.out.println("Error seeding data: " + e.getMessage()); + } + } +} \ No newline at end of file