Skip to content

Commit

Permalink
Merge pull request #2 from kiblykat/SIS-5
Browse files Browse the repository at this point in the history
SIS-5: Populate DataLoader with @PostConstruct
  • Loading branch information
kiblykat authored Mar 18, 2024
2 parents 044fd8f + 3092d2e commit dbea324
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 5 deletions.
53 changes: 52 additions & 1 deletion src/main/java/sg/com/smartinventory/DataLoader.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,56 @@
package sg.com.smartinventory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import jakarta.annotation.PostConstruct;
import sg.com.smartinventory.entity.Customer;
import sg.com.smartinventory.entity.Product;
import sg.com.smartinventory.entity.Review;
import sg.com.smartinventory.repository.CustomerRepository;
import sg.com.smartinventory.repository.ProductRepository;
import sg.com.smartinventory.repository.ReviewRepository;

@Component
public class DataLoader {

//Inject all repositories
private CustomerRepository customerRepository;
private ProductRepository productRepository;
private ReviewRepository reviewRepository;

@Autowired
public DataLoader(CustomerRepository customerRepository, ProductRepository productRepository, ReviewRepository reviewRepository){
this.customerRepository = customerRepository;
this.productRepository = productRepository;
this.reviewRepository = reviewRepository;
}

@PostConstruct
public void loadData(){
//Clear all data
customerRepository.deleteAll();
productRepository.deleteAll();
reviewRepository.deleteAll();

//Create fake data
customerRepository.save(new Customer("John", "Doe", "USA", "123 Main St", 12345, 1234567890, "[email protected]", 101));
customerRepository.save(new Customer("Alice", "Smith", "Canada", "456 Maple Ave", 54321, 987654321, "[email protected]", 102));
customerRepository.save(new Customer("Michael", "Johnson", "UK", "789 Oak Rd", 67890, 987612345, "[email protected]", 103));
customerRepository.save(new Customer("Emily", "Brown", "Australia", "321 Elm St", 13579, 456789123, "[email protected]", 104));
customerRepository.save(new Customer("David", "Wilson", "Germany", "654 Pine Rd", 98765, 369852147, "[email protected]", 105));

productRepository.save(new Product("Electronics", "Smartphone", "High-end smartphone with advanced features", 999.99, 100, "101"));
productRepository.save(new Product("Clothing", "Men's T-Shirt", "Comfortable cotton t-shirt for everyday wear", 29.99, 500, "102"));
productRepository.save(new Product("Home & Kitchen", "Coffee Maker", "Automatic coffee maker with programmable settings", 49.99, 50, "103"));
productRepository.save(new Product("Beauty", "Perfume", "Elegant fragrance with floral and citrus notes", 79.99, 200, "104"));
productRepository.save(new Product("Books", "Science Fiction Novel", "Bestselling sci-fi novel set in a dystopian future", 14.99, 300, "105"));

reviewRepository.save(new Review("Electronics", "Great smartphone with excellent features.", 5, 101, 201));
reviewRepository.save(new Review("Clothing", "Very comfortable t-shirt, fits perfectly.", 4, 102, 202));
reviewRepository.save(new Review("Home & Kitchen", "Makes delicious coffee, easy to use.", 4, 103, 203));
reviewRepository.save(new Review("Beauty", "Lovely fragrance, long-lasting.", 5, 104, 204));
reviewRepository.save(new Review("Books", "Intriguing plot, couldn't put it down.", 5, 105, 205));

}

}
19 changes: 18 additions & 1 deletion src/main/java/sg/com/smartinventory/entity/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

Expand All @@ -17,8 +18,9 @@ public class Customer {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
@Column(name = "id")
private Long id; //PK

@Column(name="first_name")
private String firstName;
@Column(name="last_name")
Expand All @@ -35,4 +37,19 @@ public class Customer {
private String email;
@Column(name="review_id")
private int reviewId; //FK

//Define Constructor for dataLoader
public Customer(String firstName, String lastName, String country, String address, int postalCode, int mobileNumber,
String email, int reviewId) {
this.firstName = firstName;
this.lastName = lastName;
this.country = country;
this.address = address;
this.postalCode = postalCode;
this.mobileNumber = mobileNumber;
this.email = email;
this.reviewId = reviewId;
}


}
14 changes: 14 additions & 0 deletions src/main/java/sg/com/smartinventory/entity/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity
@Table(name="product")
@AllArgsConstructor
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -30,4 +32,16 @@ public class Product {
private int stockQuantity;
@Column(name="review_id")
private String reviewId; //FK

//Define Constructor for dataLoader
public Product(String category, String name, String description, double price, int stockQuantity, String reviewId) {
this.category = category;
this.name = name;
this.description = description;
this.price = price;
this.stockQuantity = stockQuantity;
this.reviewId = reviewId;
}


}
12 changes: 12 additions & 0 deletions src/main/java/sg/com/smartinventory/entity/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity
@Table(name="review")
@AllArgsConstructor
public class Review {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -28,4 +30,14 @@ public class Review {
private int customerId; //FK
@Column(name="product_id")
private int productId; //FK

// Define Constructor for dataLoader
public Review(String category, String reviewContent, int rating, int customerId, int productId) {
this.category = category;
this.reviewContent = reviewContent;
this.rating = rating;
this.customerId = customerId;
this.productId = productId;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import sg.com.smartinventory.entity.Customer;

public interface CustomerRepository extends JpaRepository<Customer,Long> {

//CustomerRepository simply extends JpaRepository, giving us a lot of default methods to access db without the hardwork
public interface CustomerRepository extends JpaRepository<Customer, Long> {

}
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ spring.application.name=smart-inventory

server.port=9090
# PostgreSQL
spring.datasource.url=jdbc:postgresql://localhost:5432/simple_crm
spring.datasource.url=jdbc:postgresql://localhost:5432/smart_inventory
# for WSL, use postgres
# for Mac, use your Mac username
spring.datasource.username=postgres
Expand Down

0 comments on commit dbea324

Please sign in to comment.