Skip to content

Commit

Permalink
Merge pull request #4 from nhkhai/main
Browse files Browse the repository at this point in the history
SIS-22: Updated ER Diagram for the database.
  • Loading branch information
nhkhai authored Mar 19, 2024
2 parents 38696c2 + 26cf5a2 commit efcab67
Show file tree
Hide file tree
Showing 14 changed files with 141 additions and 94 deletions.
36 changes: 36 additions & 0 deletions Documentation/Diagrams/Entity Relationship Diagram.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
```mermaid
erDiagram
Customer {
long id PK
string first_name
string last_name
string country
string address
int postal_code
int mobile_number
string email
int review_id FK
}
Review {
long id PK
string category
string review_content
int rating
int customer_id FK
int product_id FK
}
Product {
long id PK
string category
string name
string description
double price
int stock_quantity
int review_id FK
}
Customer ||--O{ Review : "Has"
Product ||--O{ Review : "Has"
```
107 changes: 57 additions & 50 deletions src/main/java/sg/com/smartinventory/DataLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,69 @@
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;
import sg.com.smartinventory.entities.Customer;
import sg.com.smartinventory.entities.Product;
import sg.com.smartinventory.entities.Review;
import sg.com.smartinventory.repositories.CustomerRepository;
import sg.com.smartinventory.repositories.ProductRepository;
import sg.com.smartinventory.repositories.ReviewRepository;

@Component
public class DataLoader {
// Inject all repositories.
private CustomerRepository customerRepository;
private ProductRepository productRepository;
private ReviewRepository reviewRepository;
// 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;
}
// @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();
@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", 123456, 12345678, "[email protected]", 101));
customerRepository.save(
new Customer("Alice", "Smith", "Canada", "456 Maple Ave", 543210, 98765432, "[email protected]", 102));
customerRepository.save(
new Customer("Michael", "Johnson", "UK", "789 Oak Rd", 567890, 98761234, "[email protected]", 103));
customerRepository.save(
new Customer("Emily", "Brown", "Australia", "321 Elm St", 135790, 45678912, "[email protected]", 104));
customerRepository.save(
new Customer("David", "Wilson", "Germany", "654 Pine Rd", 987655, 36985214, "[email protected]", 105));
// Create fake data.
customerRepository
.save(new Customer("John", "Doe", "USA", "123 Main St", 123456, 12345678, "[email protected]", 101));
customerRepository.save(
new Customer("Alice", "Smith", "Canada", "456 Maple Ave", 543210, 98765432, "[email protected]",
102));
customerRepository.save(
new Customer("Michael", "Johnson", "UK", "789 Oak Rd", 567890, 98761234, "[email protected]",
103));
customerRepository.save(
new Customer("Emily", "Brown", "Australia", "321 Elm St", 135790, 45678912, "[email protected]",
104));
customerRepository.save(
new Customer("David", "Wilson", "Germany", "654 Pine Rd", 987655, 36985214, "[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"));
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));
}
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.springframework.web.bind.annotation.RestController;

import jakarta.validation.Valid;
import sg.com.smartinventory.entity.Customer;
import sg.com.smartinventory.entities.Customer;
import sg.com.smartinventory.services.CustomerService;

@RestController
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sg.com.smartinventory.entity;
package sg.com.smartinventory.entities;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sg.com.smartinventory.entity;
package sg.com.smartinventory.entities;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -39,13 +39,13 @@ public class Product {
private int stockQuantity;

@Column(name = "review_id")
private String reviewId; // FK.
private int reviewId; // FK.

public Product() {
}

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

this.category = category;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sg.com.smartinventory.entity;
package sg.com.smartinventory.entities;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package sg.com.smartinventory.repository;
package sg.com.smartinventory.repositories;

import org.springframework.data.jpa.repository.JpaRepository;

import sg.com.smartinventory.entity.Customer;
import sg.com.smartinventory.entities.Customer;

//CustomerRepository simply extends JpaRepository, giving us a lot of default methods to access db without the hardwork
public interface CustomerRepository extends JpaRepository<Customer, Long> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package sg.com.smartinventory.repository;
package sg.com.smartinventory.repositories;

import org.springframework.data.jpa.repository.JpaRepository;

import sg.com.smartinventory.entity.Product;
import sg.com.smartinventory.entities.Product;

public interface ProductRepository extends JpaRepository<Product, Long> {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package sg.com.smartinventory.repository;
package sg.com.smartinventory.repositories;

import org.springframework.data.jpa.repository.JpaRepository;

import sg.com.smartinventory.entity.Review;
import sg.com.smartinventory.entities.Review;

public interface ReviewRepository extends JpaRepository<Review, Long> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import org.springframework.stereotype.Service;

import sg.com.smartinventory.entity.Customer;
import sg.com.smartinventory.repository.CustomerRepository;
import sg.com.smartinventory.entities.Customer;
import sg.com.smartinventory.repositories.CustomerRepository;
import sg.com.smartinventory.services.CustomerService;

@Service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package sg.com.smartinventory.services;

import sg.com.smartinventory.entity.Customer;
import sg.com.smartinventory.entities.Customer;

public interface CustomerService {
Customer createCustomer(Customer customer);
Expand Down
57 changes: 30 additions & 27 deletions src/test/java/sg/com/smartinventory/CustomerControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,38 @@

import com.fasterxml.jackson.databind.ObjectMapper;

import sg.com.smartinventory.entity.Customer;
import sg.com.smartinventory.entities.Customer;

@SpringBootTest
@AutoConfigureMockMvc
public class CustomerControllerTest {
@Autowired
private MockMvc mockMvc;

@Autowired
private ObjectMapper objectMapper;

@DisplayName("Create customer")
@Test
public void createCustomerTest() throws Exception {
// Step 1: Create a Customer object
Customer newCustomer = Customer.builder().firstName("Jackie").lastName("Chan").country("Hong Kong")
.address("123 HK St")
.postalCode(654321).mobileNumber(87654321).email("[email protected]").reviewId(110).build();

// Step 2: Convert the Java object to JSON using ObjectMapper.
String newCustomerAsJSON = objectMapper.writeValueAsString(newCustomer);

// Step 3: Build the request.
RequestBuilder request = MockMvcRequestBuilders.post("/customers").contentType(MediaType.APPLICATION_JSON)
.content(newCustomerAsJSON);

// Step 4: Perform the request and get the response and assert.
mockMvc.perform(request).andExpect(status().isCreated())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.firstName").value("Jackie")).andExpect(jsonPath("$.lastName").value("Chan"));
}
@Autowired
private MockMvc mockMvc;

@Autowired
private ObjectMapper objectMapper;

@DisplayName("Create customer")
@Test
public void createCustomerTest() throws Exception {
// Step 1: Create a Customer object
Customer newCustomer = Customer.builder().firstName("Jackie").lastName("Chan").country("Hong Kong")
.address("123 HK St")
.postalCode(654321).mobileNumber(87654321).email("[email protected]")
.reviewId(110).build();

// Step 2: Convert the Java object to JSON using ObjectMapper.
String newCustomerAsJSON = objectMapper.writeValueAsString(newCustomer);

// Step 3: Build the request.
RequestBuilder request = MockMvcRequestBuilders.post("/customers")
.contentType(MediaType.APPLICATION_JSON)
.content(newCustomerAsJSON);

// Step 4: Perform the request and get the response and assert.
mockMvc.perform(request).andExpect(status().isCreated())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.firstName").value("Jackie"))
.andExpect(jsonPath("$.lastName").value("Chan"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import org.mockito.Mock;
import org.springframework.boot.test.context.SpringBootTest;

import sg.com.smartinventory.entity.Customer;
import sg.com.smartinventory.repository.CustomerRepository;
import sg.com.smartinventory.entities.Customer;
import sg.com.smartinventory.repositories.CustomerRepository;
import sg.com.smartinventory.serviceImpls.CustomerServiceImpl;

@SpringBootTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
class SmartInventoryApplicationTests {
@Test
void contextLoads() {

}
}

0 comments on commit efcab67

Please sign in to comment.